A
A
AlmazKayum2017-12-12 22:52:51
Python
AlmazKayum, 2017-12-12 22:52:51

How to bind Amazon AWS Lambda handlers to PyTelegramBotApi handlers?

This video describes in detail how to make a webhook for a telegram bot using Amazon AWS https://www.youtube.com/watch?v=8aeoTryJqyo
without certificates, hosting and quite simply.
A handler is created in the code, for example, entry.point, and updates from telegrams are sent there through Amazon ABC.
Question: how to connect all this with the handlers of the PyTelegramBotApi library?
If there is anyone who understands, please throw off code examples.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nmelis, 2019-06-23
@nmelis

Hello, probably the answer is not relevant to you, but I will answer for others who will be looking for an answer to a similar question!
So: I chose Flask and PyTelegramBotApi, Flask can be omitted, but it was easier for me

# main.py
from flask import Flask
import telebot

app = Flask(__name__)
bot = telebot.TeleBot(os.getenv('TELEGRAM_BOT_TOKEN'),  threaded=False)

@app.route('/' + os.getenv('TELEGRAM_BOT_TOKEN'), methods=['GET', 'POST'])
def telegram_bot_handler():
    if flask.request.headers.get('content-type') == 'application/json':
        json_string = flask.request.get_data().decode('utf-8')
        update = telebot.types.Update.de_json(json_string)
        bot.process_new_updates([update])
        return json_string, 200
    else:
        print('You NOT made it!')
        flask.abort(403)

@app.route('/set_webhook')
def set_webhook(): # чтобы легко можно было установить веб хук
    bot.remove_webhook()
    sleep(1)
    try:
        return str(bot.set_webhook('{}{}'.format(os.getenv('WEBHOOK_TELEGRAM'), os.getenv( 'TELEGRAM_BOT_TOKEN')))), 200
    except telebot.apihelper.ApiException:
        pass
    return '', 200

@bot.message_handler(func=lambda message: True, commands=['help'])
def hello_command(message):
    tg.bot.reply_to(message, "Help text")

Specify main:app in the AWS Lambda handler

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question