I am trying to develop simple telegram bot. When just started, I used polling to test the bot. When I tried to do something wrong, bot crashed with error message in console:
>> bot.send_message(1, '')
A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: message text is empty
(I should say, bot.send_message(1, ”) is wrong, message can’t be empty)
But when I switched to flask, I noticed, that bot no longer prints any errors, moreover, it just passes exceptions as if try: ... except: pass
, or sometimes just try: ... except: return
. Bot works, tho.
I tried everything. Tried to switch back to polling not touching any other code, and indeed, I got my errors back. If I put
try:
...
except Exception as e:
print(e)
over wrong function, it does report me an error.
Also it reports an error if I called function:
@bot.message_handler(content_types=['text'])
def on_text(message):
print('s')
bot.send_message(1, '')
this WON’T report an error on my message, even though it will print “1”.
But
@bot.message_handler(content_types=['text'])
def on_text(message):
bot.send_message(1, '')
on_text
WILL report an error.
Why? How do i fix it? I can’t even imagine why is that.
Full test bot code:
from telebot import telebot, types
import flask
bot = telebot.TeleBot(token)
app = flask.Flask(__name__)
if bot.get_webhook_info().url != hookurl:
bot.remove_webhook()
bot.set_webhook(hookurl)
@app.route('/tovc', methods=['POST'])
def tovc_webhook():
if flask.request.headers.get('content-type') == 'application/json':
json_string = flask.request.get_data().decode('utf-8')
update = types.Update.de_json(json_string)
bot.process_new_updates([update])
return ''
else:
flask.abort(403)
@bot.message_handler(content_types=['text'])
def on_text(message):
bot.send_message(1, '')
if __name__ == '__main__':
app.run()
heavennborn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.