├── requirements.txt ├── README.md ├── LICENSE └── main.py /requirements.txt: -------------------------------------------------------------------------------- 1 | fastapi 2 | aiogram==2.22.2 3 | gunicorn 4 | uvicorn -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Aiogram FastAPI template with webhooks to deploy to Render 2 | 3 | Start command: 4 | ``` 5 | gunicorn -k uvicorn.workers.UvicornWorker main:app --timeout 15 6 | ``` 7 | 8 | Set telegram webhook: 9 | ``` 10 | https://api.telegram.org/bot/setWebhook?url=https://.onrender.com/bot/ 11 | ``` -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 codemurt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from fastapi import FastAPI 2 | import time 3 | import logging 4 | import os 5 | 6 | from aiogram import Bot, Dispatcher, types 7 | 8 | TOKEN = os.getenv('TOKEN') 9 | 10 | WEBHOOK_PATH = f"/bot/{TOKEN}" 11 | RENDER_WEB_SERVICE_NAME = "" 12 | WEBHOOK_URL = "https://" + RENDER_WEB_SERVICE_NAME + ".onrender.com" + WEBHOOK_PATH 13 | 14 | logging.basicConfig(filemode='a', level=logging.INFO) 15 | bot = Bot(token=TOKEN) 16 | dp = Dispatcher(bot=bot) 17 | 18 | app = FastAPI() 19 | 20 | @app.on_event("startup") 21 | async def on_startup(): 22 | webhook_info = await bot.get_webhook_info() 23 | if webhook_info.url != WEBHOOK_URL: 24 | await bot.set_webhook( 25 | url=WEBHOOK_URL 26 | ) 27 | 28 | @dp.message_handler(commands=['start']) 29 | async def start_handler(message: types.Message): 30 | user_id = message.from_user.id 31 | user_full_name = message.from_user.full_name 32 | logging.info(f'Start: {user_id} {user_full_name} {time.asctime()}. Message: {message}') 33 | await message.reply(f"Hello, {user_full_name}!") 34 | 35 | @dp.message_handler() 36 | async def main_handler(message: types.Message): 37 | try: 38 | user_id = message.from_user.id 39 | user_full_name = message.from_user.full_name 40 | logging.info(f'Main: {user_id} {user_full_name} {time.asctime()}. Message: {message}') 41 | await message.reply("Hello world!") 42 | except: 43 | logging.info(f'Main: {user_id} {user_full_name} {time.asctime()}. Message: {message}. Error in main_handler') 44 | await message.reply("Something went wrong...") 45 | 46 | @app.post(WEBHOOK_PATH) 47 | async def bot_webhook(update: dict): 48 | telegram_update = types.Update(**update) 49 | Dispatcher.set_current(dp) 50 | Bot.set_current(bot) 51 | await dp.process_update(telegram_update) 52 | 53 | @app.on_event("shutdown") 54 | async def on_shutdown(): 55 | await bot.get_session().close() 56 | 57 | @app.get("/") 58 | def main_web_handler(): 59 | return "Everything ok!" --------------------------------------------------------------------------------