Build notes
Telegram getUpdates Conflict: Two Processes, One Bot Token
Batdorj Byambadorj · · 5 min read
My project sends alerts to traders through Telegram. In two months it went from one service that held a bot token to several, and twice a new feature ran into the same rule. Both times the rule decided the design:
Any number of processes can send with one bot token. Only one can receive.
I have not watched this failure happen in production. Both times I saw it coming and went round it.
Sending is shared, receiving is not
Sending a message is an ordinary HTTP call. At one point three of my services held the same token, and the two that only sent never got in anyone's way.
Receiving is different. A bot that has to read what a user types, a /start, a /stop, a connection code, must ask Telegram for updates. With long polling that is a getUpdates call that is held open, and whoever holds it gets the next update.
Put two processes on the same token and each update goes to one of them. The other never sees it. Telegram can also answer overlapping calls with a 409. Either way the failure is quiet: a user's connection code can land in a service that does not know what it is, and from that service's side nothing went wrong.
October: the feature that needed to hear /start
On 20 October I was planning Telegram delivery for the MetaTrader EA. To connect a trader's chat, the bot has to receive the user's /start, so the EA's backend needed a poller.
The project already had a bot. Its token was already being polled, by the alert engine of the older browser version of the product.
That left two options. Send the EA's traffic through the old engine, or create a second bot. I had made the EA's backend a separate service for one reason: the day the old system was switched off, the EA must not go down with it. Tying its Telegram to the old engine would have thrown that away. So, a second bot.
It was not a free decision. The bot's name is written into the user guide, the FAQ and the EA's own messages, which makes it about as permanent as a domain name.
The poller that went with it is small. It starts once when the service boots, long polls with a 30 second timeout, and keeps its offset in the database. The rule hides in one more place here, the process manager. Mine is pm2, and this service runs in fork mode as a single instance. In cluster mode the same code would start once per instance, and I would have built the two-poller problem myself out of one token and one codebase.
Three days later the old system was switched off for good, for reasons that had nothing to do with Telegram. Its bot stayed valid, with nothing listening.
November: the bot nobody was using
On 7 November I started on alerts that run on the website itself: session opens, and reminders a trader schedules. They also needed to receive, because a visitor connects by sending the bot an eight character code.
The first plan was to reuse the EA's bot and its connection flow. That meant changing the EA's backend, which is live and sits under every EA user: its connection codes are tied to an MT5 login and server, and a website visitor has neither.
Before any code I took an inventory of what was actually on the server. It found two bots with valid tokens: the EA's, and the old one.
So the website took the old bot. Two tokens are two separate update queues on Telegram's side. Each service polls its own, nothing is coordinated, and the EA's backend was not touched. After the work, git diff in its directory was empty.
When a second service needs to receive, the answer is not a lock or a shared poller. It is a second bot.
Stopped is not gone
The old alert engine was stopped, but it was still in pm2's process list. So the first step, before anything else was built:
pm2 delete alert-engine
pm2 save
The second line matters as much as the first. pm2 restores its saved list when the server reboots. Without the save, the engine would have come back with the next reboot and started polling the token the new service was using. Updates would have been split between the two, quietly, and nothing in the new code would have explained it.
No code was deleted. The old system just cannot start by accident any more.
Who else holds the token
The inventory also found the old token in the environment files of two more services. Neither calls getUpdates: they only send, and both are stopped, so they cannot cause this conflict. If they are ever started again, the right thing is still to give them a token of their own.
The cost, and one gap
A trader who uses both the EA and the website alerts now connects to two bots. I accepted that.
The regression check came on 9 November: two EA alerts fired and arrived through the EA's bot while the web service was polling its own.
One gap is left, and it is mine. The web poller's error handler is empty. On 9 November getUpdates failed 17 times, and the log cannot say why. For a failure that is quiet by nature, an empty catch is the wrong thing to have. It is on the list.
If more than one process uses your Telegram bot
- Count the processes that call
getUpdateson each token. The answer has to be one. - Senders do not count. Any number of processes can send with the same token.
- When a second service needs to receive, give it a second bot. Do not coordinate two pollers.
- Check the process manager. No cluster mode for the poller, and no stopped process that can come back holding the same token. After
pm2 delete, runpm2 save. - Log why
getUpdatesfailed, not only that it did.
Batdorj Byambadorj
Batdorj Byambadorj has been trading forex for his own account since 2007, mostly gold. He is the sole developer of fx-alert and writes every article on this site himself.