eternnoir / pyTelegramBotAPI

Python Telegram bot api.
GNU General Public License v2.0
8.02k stars 2.02k forks source link

process_new_messages fails #155

Closed bryzgaloff closed 7 years ago

bryzgaloff commented 8 years ago

Hi, @eternnoir, first of all thank you for creation of a good tool :+1: I found a problem with a process_new_messages(...) method. I'm using django + uwsgi + nginx with this code (webhook is being set):

def process_post_body(body):
    update = telebot.types.Update.de_json(body)
    bot.process_new_messages([update.message])

@bot.message_handler()
def reply(message):
    bot.send_message(message.chat.id, message.text)

uwsgi logs look like process_post_body(...) method and process_new_messages(...) are invoked every time I get a POST request from Telegram webhook but send_message(...) method is invoked once, gets no reply from Telegram server and is being stucked. And I see nothing in my Telegram chat with bot.

I tried to use send_message() directly:

def process_post_body(body):
    update = telebot.types.Update.de_json(body)
    reply([update.message])

def reply(message):
    bot.send_message(message.chat.id, message.text)

And it works fine that way. I receive messages in the chat and get replies from Telegram.

I am also confused with the fact that using cherrypy both ways everything works fine too. Please can you help me find the solution for django? Thank you in advance.

bryzgaloff commented 8 years ago

Thanks to @sbongain's #132 I found a solution setting bot.TeleBot(TOKEN,threaded=False), but is there really any other way?

ms82494 commented 8 years ago

Just wanted to echo the comment by bryzgaloff in all respects. Thanks for the great tool. I spent a lot of time trying out different frameworks, and this one really seems to make operation under a WSGI framework most intuitive. Also, if there were a way to make threaded operation feasible under Django/Flask/... it would be wonderful. Short of that, I'd propose a logging entry to help users debug the issue..

mamashin commented 8 years ago

Hello ! Some problems - docker containers (django+gunicorn and nginx):

@csrf_exempt
def t-bot(request):
    received_json_data = json.loads(request.body)
    update = telebot.types.Update.de_json(received_json_data)
    bot.process_new_messages([update.message])
    ret.status_code = 200
    return ret

@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
    bot.reply_to(message, "Howdy, how are you doing?")

debug:

django_1 | 2016-05-12 00:44:28,993 (apihelper.py:28 WorkerThread1) DEBUG - TeleBot: "Request: method=post url=https://api.telegram.org/botTOKEN/sendMessage params={'text': 'Howdy, how are you doing?', 'chat_id': '123456789', 'reply_to_message_id': 224} files=None"
django_1 | 2016-05-12 00:44:29,006 (util.py:60 WorkerThread1) DEBUG - TeleBot: "Exception occurred"

and no luck with threaded=False - Internal Server Error :(

eternnoir commented 8 years ago

Could you add try except for bot.process_new_messages([update.message]) to log exception information?

def process_post_body(body):
    update = telebot.types.Update.de_json(body)
    try:
        bot.process_new_messages([update.message])
    except Exception as e:
        log.error(e)

@bot.message_handler()
def reply(message):
    bot.send_message(message.chat.id, message.text)
mamashin commented 8 years ago

@eternnoir , no exception at this line, changed code:

@bot.message_handler()
def reply(message):
    try:
        bot.send_message(message.chat.id, message.text)
    except Exception as e:
        print "except!"
        print e

debug:

django_1 | 2016-05-12 10:19:21,240 (util.py:51 WorkerThread1) DEBUG - TeleBot: "Received task"
django_1 | 2016-05-12 10:19:21,242 (apihelper.py:28 WorkerThread1) DEBUG - TeleBot: "Request: method=post url=https://api.telegram.org/botTOKEN/sendMessage params={'text': u'/help', 'chat_id': '123456789'} files=None"
django_1 | except!
django_1 | Timeout value connect was (3.5, 9999), but it must be an int or float.
django_1 | 2016-05-12 10:19:21,258 (util.py:55 WorkerThread1) DEBUG - TeleBot: "Task complete"

and one more thing, when my gunicorn reloading, I'd recive this message:

django_1 | Exception in thread WorkerThread1 (most likely raised during interpreter shutdown):
django_1 | Traceback (most recent call last):
django_1 |   File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
django_1 |   File "/usr/local/lib/python2.7/dist-packages/telebot/util.py", line 57, in run
django_1 | <type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'Empty'

can be a problem is this?

eternnoir commented 8 years ago

@mamashin Yes, this error is from requests package. What version about requests you use? Or you can upgrade requests package.

pip install -U requests

ref. http://stackoverflow.com/questions/28787086/python-requests-timeout-for-the-whole-request

mamashin commented 8 years ago

After upgrade requests all works fine ! Cool ! Thanks !

azam10x commented 4 years ago

Thank you @bryzgaloff !