Подобный тестовый пример для юзербота Telethon 1.24.0 и веб-сервера aiohttp 3.8.1.
pip install telethon
pip install aiohttp
Тестировалось на Python 3.6.9 Ubuntu 18.04.5 LTS и Python 3.8.6 Windows 7 x64
import asyncio
from telethon import TelegramClient, events
from aiohttp import web
session_name = 'test'
api_id = 6
api_hash = 'eb06d4abfb49dc3eeb1aeb98ae0f581e'
async def main_userbot():
client = TelegramClient(session_name, api_id, api_hash)
@client.on(events.NewMessage(pattern='(?i)hellox'))
async def handler(event):
print(event.stringify())
await event.respond('Hi admin!')
await client.start()
me = await client.get_me()
print(me.stringify())
await client.run_until_disconnected()
async def main_web():
app = web.Application()
routes = web.RouteTableDef()
@routes.get('/')
@routes.get('/{name}')
async def handle(request):
name = request.match_info.get('name', "Anonymous")
print('get from ' + name)
text = "Hello, " + name
return web.Response(text=text)
app.add_routes(routes)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, host='0.0.0.0', port=8000)
await site.start()
print('running web server...')
await asyncio.Event().wait()
async def main():
await asyncio.gather(
main_userbot(),
main_web(),
)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Помогли материалы по ссылкам
https://docs.telethon.dev/en/latest/concepts/async...
https://docs.aiohttp.org/en/stable/web_quickstart....
https://docs.aiohttp.org/en/stable/web_advanced.ht...
https://stackoverflow.com/questions/53465862/pytho...
https://stackoverflow.com/questions/49978396/detai...