Created
January 14, 2025 00:30
-
-
Save makarworld/d8a132eeaad558c947ad7dc53f6fe7e6 to your computer and use it in GitHub Desktop.
Telegram bot on python (aiogram 3.x) with webhook via xtunnel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aiogram import Bot, Dispatcher, types | |
from aiohttp import web | |
API_TOKEN = 'BOT_TOKEN' | |
# for run xtunnel https: | |
# xtunnel 3000 | |
WEBHOOK_URL = 'https://xtunnel_link_here/webhook' # Замените на ваш URL | |
bot = Bot(token=API_TOKEN) | |
dp = Dispatcher() | |
async def on_startup(app): | |
await bot.set_webhook(WEBHOOK_URL) | |
webhook_info = await bot.get_webhook_info() | |
print(webhook_info) | |
async def on_shutdown(app): | |
#await bot.delete_webhook() | |
pass | |
@dp.message() | |
async def echo(message: types.Message): | |
await message.answer(message.text) | |
async def handle_webhook(request: web.Request): | |
update = types.Update(**await request.json()) | |
await dp.feed_webhook_update(bot, update) | |
return web.Response() | |
async def index(request: web.Request): | |
return web.Response(text="Hello, world!") | |
app = web.Application() | |
app.router.add_post('/webhook', handle_webhook) | |
app.router.add_get('/', index) | |
app.on_startup.append(on_startup) | |
app.on_shutdown.append(on_shutdown) | |
if __name__ == '__main__': | |
web.run_app(app, port=3000) # Укажите нужный порт |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment