mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
Added example of running serverless on AWS Lambda
Example of using of PyTelegramBotAPI in Amazon AWS Lambda
This commit is contained in:
parent
ebec3bf5c1
commit
1d8dc78c87
50
examples/serverless/lambda_function.py
Normal file
50
examples/serverless/lambda_function.py
Normal file
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user