Skip to content

Commit

Permalink
Merge pull request eternnoir#1772 from sijokun/master
Browse files Browse the repository at this point in the history
Added example of running serverless on AWS Lambda
  • Loading branch information
Badiboy committed Oct 29, 2022
2 parents ebec3bf + 507d53e commit 2037616
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions examples/serverless/aws_lambda_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""
Example of running PyTelegramBotAPI serverless in Amazon AWS Lambdaю
You have to set your lambda's url as telegram webhook manually https://core.telegram.org/bots/api#setwebhook
"""

import logging

import telebot
import json
import os

API_TOKEN = os.environ['TELEGRAM_TOKEN']


logger = telebot.logger
telebot.logger.setLevel(logging.INFO)

bot = telebot.TeleBot(API_TOKEN, threaded=False)


def process_event(event):
# Get telegram webhook json from event
request_body_dict = json.loads(event['body'])
# Parse updates from json
update = telebot.types.Update.de_json(request_body_dict)
# Run handlers and etc for updates
bot.process_new_updates([update])


def lambda_handler(event, context):
# Process event from aws and respond
process_event(event)
return {
'statusCode': 200
}


# Handle '/start' and '/help'
@bot.message_handler(commands=['help', 'start'])
def send_welcome(message):
bot.reply_to(message,
("Hi there, I am EchoBot.\n"
"I am here to echo your kind words back to you."))


# Handle all other messages
@bot.message_handler(func=lambda message: True, content_types=['text'])
def echo_message(message):
bot.reply_to(message, message.text)

0 comments on commit 2037616

Please sign in to comment.