bostrot / telegram-support-bot

A Telegram ticketing/supporting system.
https://botspace.bostrot.com
GNU General Public License v3.0
425 stars 152 forks source link

Messages are not sent to the chat #56

Closed kultpro closed 3 years ago

kultpro commented 3 years ago

I ran into a problem again. The app is running in dev mode. After some long time of work, I see a message from the user in the console, but there is no message in the chat. There are no error messages. Restarting the app - new messages are sent to the chat again. So far, there are no ideas on how to quickly reproduce the problem.

kultpro commented 3 years ago

Please tell me, is there a limit on the total number of messages from the user? There is a suspicion of this. That is, it is not a temporary ban for too frequent messages.

"spam_time: 5 60 1000, // time (in MS) in which user may send 5 messages" Here we are talking about 5 messages per minute and a ban for exceeding 1000 milliseconds? But it seems that a notification about the ban appears in the console. In my case, there is no information, I just see messages in the console, but they do not reach the chat.

kultpro commented 3 years ago

Hmm, suddenly there were errors. Whether there were any actions from the user I can not say, from the technical support was not.

Error:
Could not send error msg to chat:
Error:
Could not send error msg to chat:

For now, it remains to summarize that the application needs a more detailed error output..

bostrot commented 3 years ago

Yes but unfortunately I can only output the errors I get but if errors are not forwarded from e.g. the telegraf framework or Telegram's API I can not output the error. Everytime you get an "Error:" and there is no message behind it it means that the error object is empty which means there is no way for me to output the error unfortunately.

bostrot commented 3 years ago

There is no message limitation except the 5 messages per 5 minutes. Did you hit that limit?

If not do you have a screenshot or the log file of this behavior?

Also this Could not send error msg to chat: means that the bot couldn't send the error message to the user, probably because it was blocked by him.

kultpro commented 3 years ago

Thank you for the information! Unfortunately, I noticed this in the console, compared it purely by accident, the log was not saved. This is not the first time this has happened. I'll keep watching.

bostrot commented 3 years ago

There should be a debug.log file in the config folder. Could you check that?

kultpro commented 3 years ago

There are errors in the log, but they do not apply to a situation where messages are not sent and there are no error messages about it. That is, now I observe several situations.

Error Unclear situation

Error Could not send error msg to chat: Indeed, the user blocks the bot. In the console, I additionally see an error that does not get into the log. 'Tue Jun 01 2021 19:37:15 GMT+0300 (GMT+03:00): Error: 403: Forbidden: bot was blocked by the user\n at buildConfig.then.then.then.then (/root/telegram-support-bot/node_modules/telegraf/core/network/client.js:281:17)\n at process._tickCallback (internal/process/next_tick.js:68:7)'

undefined It is not clear what this is and in connection with what. Sometimes together with the "Error" sometimes alone

Failed to fetch updates. Waiting: 1s request to https://api.telegram.org/botхххх:ххх/getUpdates?offset=012334&limit=100&timeout=30 failed, reason: socket hang up An understandable error, does not affect the operation of the application, is the log - "Error: 403: Forbidden: bot was blocked by the user".

bostrot commented 3 years ago

Some information on undefined:

That comes up everytime when the bot outputs something that is empty. e.g. when the bot tries to output a ticket id which is not in the database. There might not be a real error behind this other then I should check if the variable is defined before using it.

Some background stuff about

Error This may come up every time any error happens that actually triggers the error event from NodeJS.

Error Could not send error msg to chat: So here I trigger an error with the error message 'Could not send...' which itself might include another error for more detailed info.

As the last commit was just a hotfix for the bot crashing I just created an error when the bot is blocked. Hiding this or just outputting what it actually does would be smarter.

The message 'Failed to fetch updates' and 'Forbidden bot was blocked' and similar messages unfortunately don't trigger that error event. There was a post about this a while ago in the telegraf frameworks repo (I'll search for it later). There might be a workaround here.

kultpro commented 3 years ago

Thank you for monitoring these issues. Your app has helped so many people!

kultpro commented 3 years ago

Maybe this will help For many days, the bot worked. At some point, I see new messages in the console, but they are not in the chat. Restarting the application - messages are sent to the chat.

bostrot commented 3 years ago

Is there any error message when that happens? Perhaps a "403 Forbidden" error? I am guessing that even if that doesn't show it is probably something like that.

Although it is a very strange behavior. I am running multiple bots in a docker VM and they have been up for 76 days now without problems.

image

As far as I remember correctly you are running them directly via npm right? Have you tried running the bot in docker yet?

kultpro commented 3 years ago

Tell me, what OS and with what characteristics should the VM be in order for the image from your repository to work according to your instructions? So far, we have only managed to build a container using docker-compose, but I would like to use your image specifically.

I again noticed messages that did not reach the chat, it turns out that uptime does not play a role, it's something else.

kultpro commented 3 years ago

Users in their nicknames can simultaneously have Latin, Cyrillic characters and various emoticons, can this be the reason? You solved a similar problem not so long ago. The first message from a new user probably always comes. It's difficult for me to track the dependence yet, it's like an assumption.

bostrot commented 3 years ago

I normally just use those commands to build the image and run it directly in background:

cd telegram-support-bot 
docker build -t bostrot/telegram-support-bot:latest .
docker-compose up -d

(make sure to edit the config file in docker-compose.yml file before that)

The special characters might actually be the reason. It is weird that it doesn't show an error message though. As far as I remember correctly the messages are parsed using regex or at least some splitting at a specific character to extract the ticket ID. It might be possible that some character is messing with that so that the ticket ID is not recognized. Although again, this should throw an error..

I just tested putting an emoji in my name and it still works. Could you test this behavior with a Cyrillic character in your name while writing to the bot?

kultpro commented 3 years ago

I am successfully using docker, but the situation has not become clearer.

bostrot commented 3 years ago

Weird. Could you try checking Telegram's getUpdates method in the browser to check when the bot stopped working and maybe see whether there are messages with special characters or something like that hanging in there?

Should be something like following:

https://api.telegram.org/bot<bot-token>/getUpdates

For reference: https://core.telegram.org/bots/api#getting-updates

At this point I can only assume that there might be an unhandled error caused by some message. In fact there were some issues with Cyrillic characters when I started working on the bot. I do not remember how I fixed it back then though.

kultpro commented 3 years ago

the bot is installed on VDS, I run it through curl, the answer is:

{"ok":false,"error_code":409,"description":"Conflict: can't use getUpdates method while webhook is active; use deleteWebhook to delete the webhook first"}

bostrot commented 3 years ago

Oh I forgot about that. I guess deleting the webhook would be too much effort.

I have not tried it yet but maybe export DEBUG='telegraf:* before starting the bot with npm would enable the debugging mode for the framework. (https://github.com/telegraf/telegraf/discussions/1249)

This could show more details.

kultpro commented 3 years ago

It looks like the problem is solved. After I set "spam_time" in the configuration: 0 0 0", the messages stopped "disappearing".

Features of communication with people involve a lot of messages. But most of them are not so frequent that the user is temporarily banned. Or I didn't figure out the settings.

bostrot commented 3 years ago

This setting describes in what time the user can write 5 messages. If a user writes more messages in that period he will get a notification that he wrote too many messages (in config the spam text) and new messages will not get forwarded to the staff.

So setting it higher should result into faster "spam blocks".

Interesting that this problem comes from that. I guess I should declare it clearer and give it a log message so we can debug that behavior in the future.

bostrot commented 3 years ago

Closing for now.

TODO:

EDIT:

This should clarify how spam_time works: Wiki spam_time