Forums

pyTelegramBotApi and PythonAnywhere

Hi!

I am trying to set telegram bot using pyTelegramBotApi (https://github.com/eternnoir/pyTelegramBotAPI). But I am stuck with using decorators. So, my code is like this:

from flask import Flask, request
import telebot
from telebot import types

secret = "c04a4995-a7e2-4bf5-b8ab-d7599105d1d1"
bot = telebot.TeleBot('mytoken')

bot.remove_webhook()
bot.set_webhook(url="https://myusername.pythonanywhere.com/{}".format(secret))

app = Flask(__name__)
@app.route('/{}'.format(secret), methods=["POST"])
def lololo():
    update = request.get_json()
    if "message" in update:
        text = update["message"]["text"]
        chat_id = update["message"]["chat"]["id"]
        bot.send_message(chat_id, update["message"]["chat"]["first_name"])
    return "ok"


@bot.message_handler(commands=['start', 'help'])
def startCommand(update):
    bot.send_message(update["message"]["chat"]["id"], 'Test string for /start and /help')

So, this code is only returning me my name despite what I send - whether it is just a message or /start command - response is the same. So how should I use decorator to handle certain commands?

Also I tried code from here (https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/webhook_examples/webhook_flask_echo_bot.py). I copied and pasted the code and replaced all necessary fields with my actual data. But it doen't work either.

Please, could you help me? Any suggestions will be very appreciated. Thanks

P.S. In the meantime I am I am using version of my bot without webhooks. Just Nonstop polling, which I started in the pythonanywhere console. But I am not sure whether it is right to do so

Looks like someone else has tried to copy-paste exactly the same code and needed some help in the forums. You'll probably find something useful there.

Thanks Glenn! Actually I read that thread two times already. I tried to do what PythonAnywhere staff suggested to do but I have no luck (

Also wanted to clarify something: while I am not able to set up webhooking, I am running none-stop polling from pythoneAnywhere console. Obviously, my 100 seconds runs out pretty quickly (like literally 100 real seconds). But after that my bot is still running and responding normally and quickly. Even after I get email about usage 10 time more than my daily allowance, the bot and the console is still up and running. Is it normal? Can I continue doing that while I figuring out webhooks?

Okay, I figured it out. Basically Glenn was wright! :) I Should have read the mentioned thread more carefully. This solution works best for me:

bot = telebot.TeleBot(TELEGRAM_TOKEN, threaded=False)
bot.remove_webhook()
bot.set_webhook(url='https://USER_NAME.pythonanywhere.com/')

app = Flask(__name__)

@app.route('/', methods=["POST"])
def webhook():
    bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
    return "!", 200

And after this I can make any handlers or decorators from pyTelegramBotApi like:

@bot.message_handler(commands=['start', 'help'])
def startCommand(message):
    bot.send_message(message.chat.id, 'Hi *' + message.chat.first_name + '*!' , parse_mode='Markdown', reply_markup=types.ReplyKeyboardRemove())

PythonAnywhere team, thanks for the greatest service!:)

:)