Skip to content

codin-eric/lambda-telegram

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Telegram bot with Amazon Lambdas and API Gateway

Diagram

Why you should deploy Telegram bot with lambdas

  • functional programing
  • It's cheaper my dude. 1 million requests per month, and 400,000 GB-seconds of compute time on a monthly basis.

Set the lambda function

  1. Create a function
    • Author from scratch
    • give it a name
    • Select Runtime: Python
  2. Copy or uplad a Zip File
  3. Setup enviromental variables
    • TELEGRAM_TOKEN: 1381220064:AAFKrPea0aBMhAjlXwkmbZLkOm3PIlqDp0g
    • CHAT_ID: 516150003
  4. Test!
  5. Deploy!

Generate a zip file

pip install --target ./package requests
cd package/
zip -r9 ${OLDPWD}/function.zip .
zip -g function.zip lambda_function.py

Example test

{
  "update_id": 1234,
  "message": {
    "message_id": 1,
    "from": {
      "id": 516150003,
      "is_bot": false,
      "first_name": "Eric",
      "last_name": "R",
      "username": "CodinEric",
      "language_code": "en"
    },
    "chat": {
      "id": 1234,
      "first_name": "Eric",
      "last_name": "R",
      "username": "CodinEric",
      "type": "private"
    },
    "date": 1601136815,
    "text": "/start",
    "entities": [
      {
        "offset": 0,
        "length": 6,
        "type": "bot_command"
      }
    ]
  }
}

Set the API Gateway

  • Create a REST API
  • Click the actions drop-down and select create method.
  • Click on the green POST after clicking the check-mark, and select Lambda function as the integration type.
  • Setup test on the “Client” box. Add the same test as in the lambda
  • Deploy API
  • If something is not working you may need to setup CORS

Set Telegram Webhook

  • Replace <ACCESS_TOKEN> with your Telegram HTTP API access token obtained in the first step.
  • Replace <INVOKE_URL> with your Invoke URL obtained in the previous step.
    Run:
$ curl --data "url=<INVOKE_URL>" "https://api.telegram.org/bot<ACCESS_TOKEN>/setWebhook"

You should get back a response similar to this:

$ {"ok":true,"result":true,"description":"Webhook was set"}

refs

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages