Run aiohttp alongside another event loop in the same code

Viewed 30

I have a bot that runs Telethon and aiohttp.server in the same code, working at the same time

This is a simplified example of how i ran it in aiohttp version 3.7.4.post0

from aiohttp import web
from telethon import TelegramClient

api_id="1234567"
api_hash="1234567890adcbefgh"
bot_token="1234567:abcdefgh0987654321"

bot=TelegramClient("my",api_id,api_hash).start(bot_token=bot_token)

# Simple TG handler
async def tg_handler(event):
    print(event.raw_text)
    await event.reply("HEY")

# Simple GET handler at /
async def http_handler_main(request):
    return web.Response(body="Hi",content_type="text/plain",charset="utf-8",status=200)

async def build_app():
    app=web.Application()
    app.add_routes([web.get("/",http_handler_main)])
    return app

bot.add_event_handler(tg_handler,events.NewMessage())
web.run_app(build_app(),port="80")

Since I updated to 3.8.1 this no longer works correctly I am using python 3.9.6 btw and Telethon version is still is the same as before this happened

EDIT1: EXPECTED BEHAVIOR: Run the webserver and the telegram bot

0 Answers
Related