How can I use this inside an AWS Lambda function?
As far as I can tell, it works as a server, but I need it to run only once.
You might be able to use telegram.Bot (and related classes like ReplyKeyboardMarkup etc) to make API requests, but you can probably not use the framework in telegram.ext.
What is telegram.ext?
It's a multi-threaded bot framework that is part of our library, in addition to the "basic" API implementation.
Since AWS Lambda is stateless, you can only use it in a limited fashion, if at all.
If you want to try it, I'd go on about it like this:
telegram.ext.Dispatcher instance on every request, ignore telegram.ext.Updater completelybot.setWebhook() (do this only once, from your PC)telegram.Update.de_json(json.loads(request_body.decode())) to decode every updateprocess_update method of your Dispatcher object to process the decoded updateThanks a bunch for the detailed instructions! I'll close this for now until (if) I have more questions.
@jh0ker Hi. I am just curious about possibility to integrate Lambda with your telegram python bot. Do you have any news since this?
@dev2060 It should be pretty much the same process
For anyone wandering around here again here is an excerpt from my lambda function's code:
import json
import os
from telegram.ext import Dispatcher
from telegram import Update, Bot
from bot.handlers import set_up
bot = Bot(token=os.getenv('TELEGRAM_TOKEN'))
dispatcher = Dispatcher(bot, None, use_context=True)
set_up(dispatcher)
def lambda_handler(event, context):
try:
dispatcher.process_update(
Update.de_json(json.loads(event["body"]), bot)
)
except Exception as e:
print(e)
return {"statusCode": 500}
return {"statusCode": 200}
The set_up function basically calls dispatcher.add_handler as usual with this library.
@ergoz thanks for your solutions 👍
I am new to this wonderful package. It may sound a nobody question, does 'bot.handlers.set_up' belong to the python-telegram-bot package? I cannot find it in the documentation.
from bot.handlers import set_up
If not, what extra package do I need to install?
The set_up function basically calls dispatcher.add_handler as usual with this library.
The OP propbably has a self-written module bot, which implements a specific telegram bot based on PTB, where bot.handlers is some sort of collection of Handlers and bot.handlers.set_up(disptacher) runs dispatcher.add_handler(handler) for all handler objects in bot.handlers
I'm a bit confused. That means the 'bot.handlers.set_up' is a custom
function which is not documented in the official doc, right?
If so, could any guru please rewrite those lines with the out-of-the-box
functions to make a clear template for AWS lambda use cases?
Deploying chat bots to the AWS lambda is cost effective and popular. Please
offer the help. Thx.
在 2020年3月11日週三 15:34,Bibo-Joshi notifications@github.com 寫道:
The set_up function basically calls dispatcher.add_handler as usual with
this library.The OP propbably has a self-written module bot, which implements a
specific telegram bot based on PTB, where bot.handlers is some sort of
collection of Handlers and bot.handlers.set_up(disptacher) runs
dispatcher.add_handler(handler) for all handler objects in bot.handlers—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/python-telegram-bot/python-telegram-bot/issues/534?email_source=notifications&email_token=ABZGEMAYLRB3UD4THBSMH63RG45GVA5CNFSM4DBXDHHKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEOOONJQ#issuecomment-597485222,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ABZGEMA2TJL5ODDEWCJFZADRG45GVANCNFSM4DBXDHHA
.
I'm a bit confused. That means the 'bot.handlers.set_up' is a custom function which is not documented in the official doc, right?
yes
If so, could any guru please rewrite those lines with the out-of-the-box functions to make a clear template for AWS lambda use cases?
handlers = [handler1, handler2, …] # Or some other iterable
...
# "set_up(disptacher) runs dispatcher.add_handler(handler) for all
# handler objects in bot.handlers" magically becomes:
def setup_handler(dispatcher):
for handler in handlers:
dispatcher.add_handler(handler)
This is not only for a AWS lambda use case btw but a rather common approach ;)
Most helpful comment
Since AWS Lambda is stateless, you can only use it in a limited fashion, if at all.
If you want to try it, I'd go on about it like this:
telegram.ext.Dispatcherinstance on every request, ignoretelegram.ext.Updatercompletelybot.setWebhook()(do this only once, from your PC)telegram.Update.de_json(json.loads(request_body.decode()))to decode every updateprocess_updatemethod of yourDispatcherobject to process the decoded update