forked from eternnoir/pyTelegramBotAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eternnoir#1772 from sijokun/master
Added example of running serverless on AWS Lambda
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|