Package: telegram-envelope
Telegram EnvelopeMicro-helper for AWS Telegram Bots written in PythonWhat is the telegram-envelope projectIt's a small Python library to manage requests and responses for Telegram Bots on AWS Lambda, invoked by web hooks through AWS Api Gateway.It parses the incoming request in order to handle to the bot a simplified and digested request. It also manages the response creation, leaving out all the related boilerplate and allowing the developers to focus on their main task: the bot logic.How to use itWe strongly recommend to useServerless frameworkto create Telegram bots to work withAWS Lambda.Look at the"Hello world" Python Serverless guidefor more information. However, we'll collect here the important steps to use telegram-envelope.Create a serverless python projectCreate it using something like this commandsls create --template aws-python --path my-project-nameInstall the required dependenciesCreate arequirements.txt file, writetelegram-envelopeon it and install the package via invokingpip install -r requirements.txt. You can find the package on it'sPypi page.Start using telegram-envelope packageOn yourhandler.pyfile you'll find ahello()function. There's is where telegram-envelope package is going to be used. You can rename this function to another name (and also thehandler.pyfile), but don't forget to change the reference later on theserverless.ymlfile.We can use it this way. In this case we rename thehello()function toapp()function, and we implement a function with our bot logic calledmy_app_logic():fromtelegram_envelope.simple_text_botimportWebHookTextBot,InputMessagedefmy_app_logic(input_message:InputMessage)->(str,bool):"""This is your telegram bot logic.It receives an InputMessage object. It contains:- input_message.chat_id (int): The id of the chat where the message comes from- input_message.text (str): The raw incoming text- input_message.message_id (int): The id of the incoming message (for us to decide to reply to it or not)It must return a tuple with:- A str: bot response text- A bool: whether to reply to the incoming message or not"""returnf"You said '{input_message.text}'",Truedefapp(event,context):"""This is your entry point.We must initialize the WebHookTextBot with the raw event,and then we can run our logic, handling a function with a specificsignature. See the my_app_logic() function above."""returnWebHookTextBot(event).run(my_app_logic)Preparing to go live!Once we developed our logic (and tested it!) we can make some tweaks to ourserverless.ymlfile:# Welcome to Serverless!## This file is the main config file for your service.# It's very minimal at this point and uses default values.# You can always add more config options for more control.# We've included some commented out config examples here.# Just uncomment any of them to get that config option.## For full config options, check the docs:#    docs.serverless.com## Happy Coding!service:my-project-name# Change it with your project nameprovider:name:awsruntime:python3.8# you can overwrite defaults herestage:pro# Your stage name (dev, test, prod...)region:eu-west-3# Your deployment region on AWSfunctions:myapp:# Change it with your Lambda function namehandler:handler.app# Change it with your `python file`.`python function`plugins:-serverless-python-requirementscustom:pythonRequirements:dockerizePip:falseAs you can see we are using a serverless plugin to package and ship our dependencies along with our telegram bot logic. You can read more about this plugin in theofficial docs.Once all the steps are done, we can deploy our solution usingsls deploy. Read more aboutdeploying our functionsanddeployment best practices.And... That's all, folks!We wish you a happy coding. If you have doubts, suggestions, improvements, share your experience or just want to say "Hi!" don't hesitate to contact us ^_^