I am trying to develop simple telegram bot. When just started, I used polling to test the bot.
import telebot
bot = telebot.TeleBot(token)
# decorator indicates that on_text() should be called when
# user sends text message to the bot
@bot.message_handler(content_types=['text'])
def on_text(message):
# send message to user with uid "1" and with text '' (wrong, should cause an error)
bot.send_message(1, '')
bot.polling()
When i send text to bot, it crashes:
A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: message text is empty
That is the intended behavior, I want my code to report errors in console.
But when I switched to flask, I noticed, that bot no longer prints any errors, moreover, it just returns on errors as if try: <sum code> except: return
.
import flask
bot = telebot.TeleBot(token)
app = flask.Flask(__name__)
@app.route('/', methods=['POST'])
def 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()
This WILL NOT give any errors on text message, even tho it should. Just prints ‘1’ and returns.
Only way to get errors to show in console is to put
try:
...code...
except Exception as e:
print(e)
over everything.
Also it reports an error if I call function manually (not by decorating it with @bot.commands and etc)
I have no idea..
heavennborn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2