-
I'm creating a webhook bot running on AWS Lambda, referring to @jojo786's sample here: ptb/ptb_lambda.py. I added a custom persistence class import json
import asyncio
from telegram import Update
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters
from persistence import DdbPersistence
ddb_persistence = DdbPersistence()
app = ApplicationBuilder().token("BOT-TOKEN").persistence(ddb_persistence).build()
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.text)
def lambda_handler(event, context):
return asyncio.get_event_loop().run_until_complete(main(event, context))
async def main(event, context):
echo_handler = MessageHandler(filters.TEXT & (~filters.COMMAND), echo)
app.add_handler(echo_handler)
try:
await app.initialize()
await app.process_update(
Update.de_json(json.loads(event["body"]), application.bot)
)
return {
'statusCode': 200,
'body': 'Success'
}
except Exception as exc:
return {
'statusCode': 500,
'body': 'Failure'
} The problem is So what is the best approach to save the data? I added |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
you probably want to interact with DynamoDB in one of your handlers, eg, in the echo handler, you will retrieve or save to DynamoDB, then call flush(). |
Beta Was this translation helpful? Give feedback.
-
Adding ...
async with app:
await app.initialize()
await app.process_update(
Update.de_json(json.loads(event["body"]), application.bot)
)
... Would suggest enhance documentation and all examples with such usage. |
Beta Was this translation helpful? Give feedback.
Adding
async with app:
will getapp.shutdown()
called and data gets flushed:Would suggest enhance documentation and all examples with such usage.