From 0cba266f8ac5c708057be4e635b6b2bc9582dee7 Mon Sep 17 00:00:00 2001 From: Oleksandr Gavryliuk Date: Thu, 7 Dec 2023 11:27:28 +0200 Subject: [PATCH] #5 solution to deploy to aws as a lambda with a webhook - not working yet --- .gitignore | 29 ++++++++++++++++++++++++----- README.md | 16 ++++++++++++++++ handler.py | 28 ++++++++++++++++++++++++++++ serverless.yml | 20 ++++++++++++++++++++ 4 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 handler.py create mode 100644 serverless.yml diff --git a/.gitignore b/.gitignore index 0825557..a5c2e69 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,25 @@ -.idea -*.iml - -venv +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg .venv -.env + +# Serverless directories +.serverless + +#Intellij IDEA files +.idea +*.iml \ No newline at end of file diff --git a/README.md b/README.md index 5654e91..80e5e2e 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,19 @@ name: daily-dragon username: daily_dragon_bot + +To deploy with `serverless`: + +```serverless deploy --aws-profile havryliuk``` + +The response will show you the webhook url. Set up a webhook: + +```curl --request POST --url https://api.telegram.org/bot/setWebhook --header 'content-type: application/json' --data '{"url": "https://u3ir5tjcsf.execute-api.us-east-1.amazonaws.com/dev/my-custom-url"}'``` + +To undeploy: + +```serverless remove --aws-profile havryliuk``` + +Use this article if available: + +https://medium.com/hackernoon/serverless-telegram-bot-on-aws-lambda-851204d4236c \ No newline at end of file diff --git a/handler.py b/handler.py new file mode 100644 index 0000000..0bce277 --- /dev/null +++ b/handler.py @@ -0,0 +1,28 @@ +import logging +from dotenv import load_dotenv +import os + +from telegram import Update +from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler + +logging.basicConfig( + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + level=logging.INFO +) + +load_dotenv() +TOKEN = os.getenv('TOKEN') +if TOKEN is None: + raise ValueError('TOKEN not found in .env file') + + +async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): + await context.bot.send_message(chat_id=update.effective_chat.id, text="Hello! I am 每日龙!\nLauching soon!") + +if __name__ == '__main__': + application = ApplicationBuilder().token(TOKEN).build() + + start_handler = CommandHandler('start', start) + application.add_handler(start_handler) + + application.run_polling() diff --git a/serverless.yml b/serverless.yml new file mode 100644 index 0000000..e788e33 --- /dev/null +++ b/serverless.yml @@ -0,0 +1,20 @@ +service: daily-dragon + +provider: + name: aws + runtime: python3.11 + stage: dev + region: us-east-1 + environment: + TELEGRAM_TOKEN: ${env:TELEGRAM_TOKEN} + + + +functions: + post: + handler: handler.py + events: + - http: + path: daily-dragon + method: post + cors: true \ No newline at end of file