aiogram/examples/proxy_and_emojize.py

70 lines
2.5 KiB
Python
Raw Normal View History

import logging
2018-04-23 00:02:13 +03:00
import aiohttp
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.types import ParseMode
from aiogram.utils.emoji import emojize
2017-11-21 00:53:53 +02:00
from aiogram.utils.executor import start_polling
from aiogram.utils.markdown import bold, code, italic, text
# Configure bot here
2019-08-11 15:49:27 +03:00
API_TOKEN = 'BOT_TOKEN_HERE'
PROXY_URL = 'http://PROXY_URL' # Or 'socks5://host:port'
2019-08-11 15:49:27 +03:00
# NOTE: If authentication is required in your proxy then uncomment next line and change login/password for it
2017-07-25 18:11:43 +03:00
# PROXY_AUTH = aiohttp.BasicAuth(login='login', password='password')
2019-08-11 15:49:27 +03:00
# And add `proxy_auth=PROXY_AUTH` argument in line 30, like this:
# >>> bot = Bot(token=API_TOKEN, proxy=PROXY_URL, proxy_auth=PROXY_AUTH)
# Also you can use Socks5 proxy but you need manually install aiohttp_socks package.
2017-07-25 18:11:43 +03:00
# Get my ip URL
GET_IP_URL = 'http://bot.whatismyipaddress.com/'
logging.basicConfig(level=logging.INFO)
2019-07-15 14:00:38 +07:00
bot = Bot(token=API_TOKEN, proxy=PROXY_URL)
2019-08-11 15:49:27 +03:00
# If auth is required:
# bot = Bot(token=API_TOKEN, proxy=PROXY_URL, proxy_auth=PROXY_AUTH)
dp = Dispatcher(bot)
2019-08-11 15:49:27 +03:00
async def fetch(url, session):
async with session.get(url) as response:
return await response.text()
2018-04-23 00:02:13 +03:00
2017-07-25 18:11:43 +03:00
@dp.message_handler(commands=['start'])
async def cmd_start(message: types.Message):
2019-08-11 15:49:27 +03:00
# fetching urls will take some time, so notify user that everything is OK
await types.ChatActions.typing()
content = []
# Make request (without proxy)
2019-08-11 15:49:27 +03:00
async with aiohttp.ClientSession() as session:
ip = await fetch(GET_IP_URL, session)
2018-04-23 00:02:13 +03:00
content.append(text(':globe_showing_Americas:', bold('IP:'), code(ip)))
# This line is formatted to '🌎 *IP:* `YOUR IP`'
2019-08-11 15:49:27 +03:00
# Make request through bot's proxy
ip = await fetch(GET_IP_URL, bot.session)
2018-04-23 00:02:13 +03:00
content.append(text(':locked_with_key:', bold('IP:'), code(ip), 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)
# In this example you can see emoji codes: ":globe_showing_Americas:" and ":locked_with_key:"
2018-02-21 19:09:44 +03:00
# You can find full emoji cheat sheet at https://www.webpagefx.com/tools/emoji-cheat-sheet/
# For representing emoji codes into real emoji use emoji util (aiogram.utils.emoji)
# (you have to install emoji module)
2018-02-21 19:09:44 +03:00
# For example emojize('Moon face :new_moon_face:') is transformed to 'Moon face 🌚'
if __name__ == '__main__':
2019-07-15 14:00:38 +07:00
start_polling(dp, skip_updates=True)