Skip to main content

Micro-helper for AWS Telegram Bots written in Python

Project description

Telegram Envelope

Micro-helper for AWS Telegram Bots written in Python

What is the telegram-envelope project

It'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 it

We strongly recommend to use Serverless framework to create Telegram bots to work with AWS Lambda.

Look at the "Hello world" Python Serverless guide for more information. However, we'll collect here the important steps to use telegram-envelope.

Create a serverless python project

Create it using something like this command sls create --template aws-python --path my-project-name

Install the required dependencies

Create a requirements.txt file, write telegram-envelope on it and install the package via invoking pip install -r requirements.txt. You can find the package on it's Pypi page.

Start using telegram-envelope package

On your handler.py file you'll find a hello() function. There's is where telegram-envelope package is going to be used. You can rename this function to another name (and also the handler.py file), but don't forget to change the reference later on the serverless.yml file.

We can use it this way. In this case we rename the hello() function to app() function, and we implement a function with our bot logic called my_app_logic():

from telegram_envelope.simple_text_bot import WebHookTextBot, InputMessage

def my_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
    """
    return f"You said '{input_message.text}'", True


def app(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 specific 
    signature. See the my_app_logic() function above.
    """
    return WebHookTextBot(event).run(my_app_logic)

Preparing to go live!

Once we developed our logic (and tested it!) we can make some tweaks to our serverless.yml file:

# 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 name

provider:
  name: aws
  runtime: python3.8

# you can overwrite defaults here
  stage: pro  # Your stage name (dev, test, prod...)
  region: eu-west-3  # Your deployment region on AWS

functions:
  myapp:  # Change it with your Lambda function name
    handler: handler.app # Change it with your `python file`.`python function`

plugins:
  - serverless-python-requirements

custom:
  pythonRequirements:
    dockerizePip: false

As 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 the official docs.

Once all the steps are done, we can deploy our solution using sls deploy. Read more about deploying our functions and deployment 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 ^_^

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

telegram-envelope-0.1.3.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

telegram_envelope-0.1.3-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file telegram-envelope-0.1.3.tar.gz.

File metadata

  • Download URL: telegram-envelope-0.1.3.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.2

File hashes

Hashes for telegram-envelope-0.1.3.tar.gz
Algorithm Hash digest
SHA256 35f40d296b06d301efcc81ec0ace45477e2bd962ecb9a0c8e16e78f093f279a1
MD5 d478431155dbc6f643af951453c628cb
BLAKE2b-256 e727b808afc587d64252e596027259f6ab1b08a58d13b683a1f0210c145e859e

See more details on using hashes here.

File details

Details for the file telegram_envelope-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: telegram_envelope-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.6.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.2

File hashes

Hashes for telegram_envelope-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8a243691d74d8da1ef0dda38ab3378c58decfad1f08ad86d085d6f05317ff697
MD5 8d3d295afff0a7e48bf604915ebfe83a
BLAKE2b-256 0bb1fcaf54fffc2961823c1a9f198ae8d1a8e4b4f8140dd449355fff0f14109b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page