Forums

telegram bot telepot package and set web hook

I set web hook for my telegram bot script,and my bot works finely.so i want to use of telepot and handle(msg) function to handle my messages from telegram.How can i do that?Is there example to illustrate it. this is my script that works finely: from flask import Flask, request import telepot import urllib3

proxy_url = "http://proxy.server:3128"
telepot.api._pools = {
    'default': urllib3.ProxyManager(proxy_url=proxy_url, num_pools=3, maxsize=10, retries=False, timeout=30),
}
telepot.api._onetime_pool_spec = (urllib3.ProxyManager, dict(proxy_url=proxy_url, num_pools=1, maxsize=1, 
retries=False, timeout=30))

secret = "A_SECRET_NUMBER"
bot = telepot.Bot('YOUR_AUTHORIZATION_TOKEN')
bot.setWebhook("https://YOUR_PYTHONANYWHERE_USERNAME.pythonanywhere.com/{}".format(secret), 
max_connections=1)

app = Flask(__name__)

@app.route('/{}'.format(secret), methods=["POST"])
def telegram_webhook():
    update = request.get_json()
    if "message" in update:
        text = update["message"]["text"]
        chat_id = update["message"]["chat"]["id"]
        bot.sendMessage(chat_id, "From the web: you said '{}'".format(text))
    return "OK"

I want to add this code to my script but it doesnt work:

  def handle(msg):
        print(msg)

have you seen this blog post on building a telegram bot on pythonanywhere? it may have some useful pointers...

yes, i have seen that, i want to set webhook and i use handle(msg) function

The handling of messages and webhooks needs to be in 2 different places. Webhooks are sent to a web app, but message handling needs to be done in a script running in a console. Follow the tutorial linked to earlier to get message handling working.

Thank you glenn. How can i associated message handling script to web hook web app?I read the tutorial but i still don't know how do it .thank you for your guide.

Just to clarify: there are two kinds of bots that you can write using Telepot. One is webhook-based, the other is based on a polling loop.

  • Webhook-based apps are the kind that the blog post describes. They run as a kind of website, and messages are sent to them by Telegram via requests to the site -- that is, Telegram initiates the connection to your site when a message comes in. They do not use the handle(msg) pattern -- everything happens inside the app.
  • Polling loop-based apps are different. They don't have an associated web app. Instead, you run code from a console on PythonAnywhere, or from a scheduled task. The code initiates the connection to Telegram, and then Telegram sends messages down that connection. The connection is managed by the Telepot library, which passes incoming messages to your code using the handle(msg) pattern.

So your existing bot is the first kind. If you want to use the handle(msg) pattern, you'll need to re-write it completely to be the second kind of bot. This will be quite a large change.

Why do you want to use that pattern, in particular?

thank you, your explain was complete.I made my script base on handle(msg) pattern and now i want to set web hook for it. Should i re-write it completely to the web app kind? Do you know about orderwebhook() method in telepot? thank you for your reply

If you want to use a single script for all of your messaging and you want to use a webhook then you'll have to rewrite the script to be entirely webhook-based.

I couldn't find any reference to orderwebhook in the telepot docs.

Thank you. I re-write the script base on webhook, and i didnt use handle(msg),i replace msg in my script with update["message"] and my script worked base on web hook.

Excellent -- so it's all working OK now?

Yes it's working finely.thank you very much.Just i used inline keyboard button in my old script (in handle(msg) pattern) and now i dont know how can i retrieve query_id in my new script(base on webhook).because when i click on inline keyboard in my bot for reply i need to use query_id

For keyboards, you need to add a new webhook specifically for inline queries (as opposed to normal messages). This sample from the Telepot documentation is a good starting point.

thank you giles.