Example for proxy_auth

This commit is contained in:
Alex Root Junior 2017-07-25 18:11:43 +03:00
parent 6c21270ccb
commit a866c8be28

View file

@ -1,6 +1,8 @@
import asyncio
import logging
import aiohttp
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.types import ParseMode
@ -11,6 +13,11 @@ from aiogram.utils.markdown import text, bold, italic, code
API_TOKEN = 'BOT TOKEN HERE'
PROXY_URL = 'http://PROXY_URL'
# If authentication is required in your proxy then uncomment next line and change login/password for that
# PROXY_AUTH = aiohttp.BasicAuth(login='login', password='password')
# And add `proxy_auth=PROXY_AUTH` argument in line 25, like that:
# >>> bot = Bot(token=API_TOKEN, loop=loop, proxy=PROXY_URL, proxy_auth=PROXY_AUTH)
# Get my ip URL
GET_IP_URL = 'http://bot.whatismyipaddress.com/'
@ -21,7 +28,7 @@ bot = Bot(token=API_TOKEN, loop=loop, proxy=PROXY_URL)
dp = Dispatcher(bot)
@dp.message_handler(commands=['test'])
@dp.message_handler(commands=['start'])
async def cmd_start(message: types.Message):
# Create temporary session
session = bot.create_temp_session()
@ -31,10 +38,12 @@ async def cmd_start(message: types.Message):
# Make request (without proxy)
async with session.get(GET_IP_URL) as response:
content.append(text(':globe_showing_Americas:', bold('IP:'), code(await response.text())))
# This line is formatted to '🌎 *IP:* `YOUR IP`'
# Make request with proxy
async with session.get(GET_IP_URL, proxy=bot.proxy, proxy_auth=bot.proxy_auth) as response:
content.append(text(':locked_with_key:', bold('IP:'), code(await response.text()), italic('via proxy')))
# This line is formatted to '🔐 *IP:* `YOUR IP` _via proxy_'
# Send content
await bot.send_message(message.chat.id, emojize(text(*content, sep='\n')), parse_mode=ParseMode.MARKDOWN)