Send messages from 1C to Telegram: code, chat_id and pitfalls
Notifications from 1C need a bot, a token and a call to the sendMessage method over HTTPConnection. The hard part is mapping a 1C user to their chat_id, not the sending code. Never keep the token in module text, and move sending to a background job.
Notifications from 1C to Telegram can be set up without paid extensions: you need a bot and a few dozen lines of code. Here is the scheme, the code and the places where people most often go wrong.
How it works
- In Telegram, create a bot through @BotFather and get a token.
- A user writes to the bot and Telegram assigns them a chat_id.
- 1C calls the sendMessage method of the Telegram Bot API over HTTPS, passing the token and chat_id.
The hard part is not sending but mapping: Telegram lets a bot write only to someone who started the conversation, and only by their chat_id. So 1C must store a “user or employee → chat_id” link.
Sending a message: code
Функция ОтправитьВTelegram(Токен, ИдЧата, Текст) Экспорт
Данные = Новый Структура("chat_id, text, parse_mode", ИдЧата, Текст, "HTML");
Запись = Новый ЗаписьJSON;
Запись.УстановитьСтроку();
ЗаписатьJSON(Запись, Данные);
Тело = Запись.Закрыть();
Запрос = Новый HTTPЗапрос("/bot" + Токен + "/sendMessage");
Запрос.Заголовки.Вставить("Content-Type", "application/json; charset=utf-8");
Запрос.УстановитьТелоИзСтроки(Тело, КодировкаТекста.UTF8, ИспользованиеByteOrderMark.НеИспользовать);
Соединение = Новый HTTPСоединение("api.telegram.org", , , , , 30, Новый ЗащищенноеСоединениеOpenSSL());
Ответ = Соединение.ОтправитьДляОбработки(Запрос);
Если Ответ.КодСостояния <> 200 Тогда
ВызватьИсключение "Telegram вернул код " + Ответ.КодСостояния + ": " + Ответ.ПолучитьТелоКакСтроку();
КонецЕсли;
Возврат Ответ.ПолучитьТелоКакСтроку();
КонецФункции
(The code is in the 1C language: HTTPConnection is HTTPСоединение, HTTPRequest is HTTPЗапрос.) Test it on your platform version: method names and constructor parameters can differ in older releases.
How to find a user's chat_id
There are two working ways:
- Manually for yourself or an admin: write to the bot, then open
https://api.telegram.org/bot<token>/getUpdatesin a browser and findchat.idin the response. - For employees: the bot offers a “Share contact” button. Telegram sends a phone number, 1C finds the user by it and saves the chat_id in an information register. Always check that the contact belongs to the sender: in the contact data
user_idmust equalfrom.id. Otherwise anyone can supply someone else's number and receive their notifications.
Pitfalls
- The token. Do not keep it in module text. Put it in a constant with restricted access or a secure store so it does not end up in a configuration dump or Git.
- Limits. According to Telegram's FAQ, a bot can send about 30 messages per second overall and about one message per second to a single chat. For mass mailings add pauses and a queue.
- Do not block posting. Move sending to a background job or a scheduled job, otherwise a failing internet connection slows down documents. Write errors to the event log.
- Undelivered messages. If a user blocked the bot, Telegram returns an error: mark such a recipient and do not retry.
- Text format. With
parse_mode=HTML, escape<,>and&in data taken from the database.
Incoming messages: webhook or polling
If the bot must receive commands (for example “Approve document”), there are two ways:
- Webhook: Telegram pushes updates to your HTTPS address. It needs a publicly reachable address with a certificate, usually a 1C HTTP service or a separate server.
- Polling getUpdates from a scheduled job: simpler and needs no public address, but with a delay.
When it is easier not to write it yourself
If you need approvals in the bot, document cards, several databases or a link between a site and a bot, it is easier to move the logic to a separate service (Python or n8n) and keep 1C as the source of events and data. More on integrating 1C with Telegram, sites and other systems in 1C and accounting systems and Telegram bots and Mini Apps.
FAQ
Can I send a message to a group or channel?
Yes. Add the bot to the group or channel as an administrator and use the group's chat_id (it starts with a minus) or `@channel_name`.
Do I need an external server to send?
No. To send, the 1C server just needs internet access to `api.telegram.org`. An external address is needed only to receive messages through a webhook.
What if the request from 1C does not go through?
Check the 1C server's internet access and proxy, the platform version, the token and the body encoding (UTF-8). Telegram's error text usually says what is missing.
How do I store the token safely?
In a constant or register restricted by roles, not in module text. If the token got into a dump or a repository, reissue it in @BotFather.
Plain version for AI and reading without styling: Markdown.