JungDev / django-telegrambot

Simple app for Telegram bot in Django
BSD 3-Clause "New" or "Revised" License
237 stars 88 forks source link

Request for not found token : `bot_token` #6

Open mrabedini opened 7 years ago

mrabedini commented 7 years ago

First, I should thank you for your great app. In polling mode on my local machine the module works like charm. However when I use the web hook mode, sometimes I get the

Request for not found token : bot_token

error and no response is given by the bot. I didn't setup the cert file in my settings.py . I am using nginx with combination of gunicorn to serve my web app. The server os is ubuntu 16.04. Below is the settings I've used:

DJANGO_TELEGRAMBOT = {
    'MODE' : 'WEBHOOK', #(Optional [str]) # The default value is WEBHOOK,
                        # otherwise you may use 'POLLING'
                        # NB: if use polling you must provide to run
                        # a management command that starts a worker
    'WEBHOOK_SITE' : 'https://www.shipup.ir',
    'WEBHOOK_PREFIX' : '/telegram-bot/', # (Optional[str]) # If this value is specified,
                                  # a prefix is added to webhook url
   # 'WEBHOOK_CERTIFICATE' : '', # If your site use self-signed
                         #certificate, must be set with location of your public key
                         #certificate.(More info at https://core.telegram.org/bots/self-signed )
    'BOTS' : [
        {
           'TOKEN': 'bot_token', #Your bot token.
        },
    ],
}

The thing that intrigues is the point that sometimes my bot works. Any advice to solve the issue is appreciated.

mrabedini commented 7 years ago

with some logging, I've found that in some requests that there is no response of the bot, the cls.bot_tokens is empty.

mrabedini commented 7 years ago

I found the problem. In gunicorn the number of workers were set to 3. Just one of them could have bot_tokens and two of them didn't have the tokens. When I set the number of workers to 1, everything is ok. Now the question is what should I do to get it to work with number of workers 3 in gunicorn.

JungDev commented 7 years ago

Can you try to comment this line https://github.com/JungDev/django-telegrambot/blob/master/django_telegrambot/apps.py#L126 , please?

mrabedini commented 7 years ago

Yes, it solved the problem. What the line is for?!

JungDev commented 7 years ago

It's a flag to prevent re-running code which should be executed exactly one time.

mrbf commented 7 years ago

I have the same problem. The settings is:

DJANGO_TELEGRAMBOT = {
    'MODE' : 'WEBHOOK', 
    'WEBHOOK_SITE' : 'https://example.com/',
    'BOTS' : [
        {
           'TOKEN': 'botToken',
        },
    ],
}

I commented the apps.py 126th line and restarted server. But problem still exist. Logging says that 'Not Found: /botToken/'. How can I solve the issue?

marqueewinq commented 6 years ago

@JungDev @mrabedini Ok, that's what i found

This error is mainly caused by TelegramError (Flood control), which is caused by rapid setWebhook queries. Perhaps, if bot.getWebhookInfo().url != hookurl: <set webhook> check will help.

mrabedini commented 6 years ago

It seems that the problem still remains. When I use gunicorn with supervisor, I have the same issue. I tried to regenerate the issue with docker. Using gunicorn with --bind option and nginx serving the requests didn't have any problem. My next try would be to add supervisor to the simulation. However, as I mentioned earlier in the current comment, on the server I still have the problem.

aliaksandrmelnik commented 6 years ago

This error raise with uWSGI too.

Looks like MR #10 fix this problem.

rafis commented 6 years ago

Agree with @aliaksandrmelnik, most commonly encountered issue is telegram.error.RetryAfter: Flood control exceeded. Retry in 1 seconds.

heykarimoff commented 6 years ago

@mrabedini Had the same problem with gunicorn. Setting --workers=1 helped.

@JungDev Is PR #8 going to be the only solution?