Telegram Channel

34 مشاهدة Markdown

The eight endpoints that connect a Telegram bot, switch the channel and manage the chats notifications reach.

Overview

Telegram is a notification channel beside e-mail and SMS. A bot token connects the channel, and the channel switch turns the copies on or off.

Messages go to chats. Shared chats are groups and channels that receive template notifications, and one of them is the default. Personal chats belong to staff members.

A chat is linked in the panel, with a one-time link opened in Telegram. These endpoints read, adjust and remove linked chats; they cannot link a new one.

Reference

Reading the Channel Settings

get/api/v1/admin/notifications/telegram
Notifications/GetTelegramSettings admin

Returns the channel state, the bot behind the token and how many chats are linked. The token is write-only and comes back masked.

Response fields data — 6
configuredboolA bot token is saved.
enabledboolThe channel switch is on and a token is saved.
availableboolThe channel is enabled and at least one chat is still active. Telegram copies go out only then.
tokenstring|nullThe masked token, <bot id>:••••••. Null when no token is saved. The secret part never leaves the server.
botobject|nullThe bot that answered the last check. Null when no token is saved.
idintThe bot's Telegram user id.
usernamestringThe bot username, without the @ sign.
first_namestringThe bot's display name.
can_join_groupsboolWhether the bot may join groups, a BotFather setting.
tested_atstring|nullWhen the token was last saved or tested, in site time.
chatsobjectLinked chat counts.
sharedintGroup and channel chats that receive template notifications.
personalintStaff members' private chats.
brokenintChats of either kind whose connection was lost.
Errors 1
insufficient_scope403The key lacks the required scope.
Request
curl 'https://panel.example.com/api/v1/admin/notifications/telegram' \
  -H "Authorization: Bearer $API_KEY"
const res = await fetch('https://panel.example.com/api/v1/admin/notifications/telegram', {
  headers: { Authorization: `Bearer ${apiKey}` },
});

const { data } = await res.json();
const canSend = data.available;
$ch = curl_init('https://panel.example.com/api/v1/admin/notifications/telegram');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER     => ['Authorization: Bearer ' . $apiKey],
]);

$body = json_decode(curl_exec($ch), true);
curl_close($ch);
// "enabled" alone does not mean copies go out: "available" also needs an active chat.
$tg = Api::Notifications()->GetTelegramSettings()['data'];
$canSend = $tg['available'];

Writing the Channel Settings

put/api/v1/admin/notifications/telegram
Notifications/UpdateTelegramSettings admin both fields are checked first

Saves a bot token and switches the channel on or off. Both fields are optional; one invalid field means nothing is written.

Body 2
tokenstringThe bot token from BotFather. Telegram checks it before it is saved, and the first saved token also switches the channel on.
enabledboolThe channel switch. Text such as "true", "0" or "off" is read by its meaning. Switching on needs a saved token.
Response fields data
dataobjectThe settings after the write. Same shape as the read endpoint.
Errors 6
invalid_token422The token is not a string or not in BotFather's format.
token_rejected422Telegram refused the token. The message carries Telegram's reason.
invalid_enabled422The enabled field is not a switch value.
not_configured422Switching on while no token is saved.
save_failed500The token could not be stored.
insufficient_scope403The key lacks the required scope.
Request
curl -X PUT 'https://panel.example.com/api/v1/admin/notifications/telegram' \
  -H "Authorization: Bearer $API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{"token":"123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11","enabled":true}'
const res = await fetch('https://panel.example.com/api/v1/admin/notifications/telegram', {
  method:  'PUT',
  headers: { Authorization: `Bearer ${apiKey}`, 'Content-Type': 'application/json' },
  body:    JSON.stringify({ enabled: false }),
});

const body = await res.json();
$ch = curl_init('https://panel.example.com/api/v1/admin/notifications/telegram');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST  => 'PUT',
    CURLOPT_HTTPHEADER     => ['Authorization: Bearer ' . $apiKey, 'Content-Type: application/json'],
    CURLOPT_POSTFIELDS     => json_encode(['token' => $botToken]),
]);

$body = json_decode(curl_exec($ch), true);
curl_close($ch);
// A token of a DIFFERENT bot breaks every linked chat: compare bot.id before and after.
$before = Api::Notifications()->GetTelegramSettings()['data']['bot']['id'] ?? null;
$after  = Api::Notifications()->UpdateTelegramSettings(['token' => $botToken])['data'];

Testing the Bot

post/api/v1/admin/notifications/telegram/test
Notifications/TestTelegramBot admin

Checks the saved token with Telegram and refreshes the bot details. No message goes to any chat.

Body —
——No body is needed. The saved token is checked; send an empty body.
Response fields data
dataobjectThe settings after the check. Same shape as the read endpoint; bot.tested_at moves.
Errors 3
not_configured422No bot token is saved.
telegram_error422Telegram refused the saved token. The message carries Telegram's reason.
insufficient_scope403The key lacks the required scope.
Request
curl -X POST 'https://panel.example.com/api/v1/admin/notifications/telegram/test' \
  -H "Authorization: Bearer $API_KEY"
const res = await fetch('https://panel.example.com/api/v1/admin/notifications/telegram/test', {
  method: 'POST',
  headers: { Authorization: `Bearer ${apiKey}` },
});

const { data } = await res.json();
$ch = curl_init('https://panel.example.com/api/v1/admin/notifications/telegram/test');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST           => true,
    CURLOPT_HTTPHEADER     => ['Authorization: Bearer ' . $apiKey],
]);

$body = json_decode(curl_exec($ch), true);
curl_close($ch);
// A token revoked in BotFather shows up here as telegram_error.
$check = Api::Notifications()->TestTelegramBot();

Listing the Chats

get/api/v1/admin/notifications/telegram/chats
Notifications/GetTelegramChats admin

Lists every linked chat: shared chats first with the default at the top, then personal chats. Telegram's own chat id is not returned; every call uses the row id.

Query parameters 1
kindstringshared or personal. Leave it out for both kinds.
Response fields data[] — 15
idintThe chat row id.
kindstringshared or personal.
typestringTelegram's chat type: group, supergroup, channel or private.
titlestringThe chat title from Telegram. For a private chat, the person's name.
admin_idint|nullThe staff member of a personal chat. Null for a shared chat.
langstring|nullThe language of the chat's messages. Null means a personal chat follows its staff member and a shared chat the member who linked it.
is_defaultboolWhether this is the default shared chat. Templates that name no chat go there.
statusstringactive or broken. Broken means the bot lost the chat: removed, blocked, or the chat is gone.
last_errorstring|nullTelegram's last refusal for this chat.
quiet_enabledboolQuiet hours are on. Messages in the range still arrive, without a sound.
quiet_startstring|nullThe range start, HH:MM in the site's time zone.
quiet_endstring|nullThe range end, HH:MM. A range may cross midnight, as in 22:00 to 08:00.
last_sent_atstring|nullWhen a message was last delivered.
linked_byint|nullThe staff member who linked the chat.
linked_atstring|nullWhen the chat was linked.
Meta 1
totalintThe number of chats returned.
Errors 2
invalid_kind422The kind is neither shared nor personal.
insufficient_scope403The key lacks the required scope.
Request
curl -G 'https://panel.example.com/api/v1/admin/notifications/telegram/chats' \
  -H "Authorization: Bearer $API_KEY" \
  --data-urlencode 'kind=shared'
const res = await fetch('https://panel.example.com/api/v1/admin/notifications/telegram/chats?kind=shared', {
  headers: { Authorization: `Bearer ${apiKey}` },
});

const { data } = await res.json();
const broken = data.filter((c) => c.status === 'broken');
$ch = curl_init('https://panel.example.com/api/v1/admin/notifications/telegram/chats?kind=shared');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER     => ['Authorization: Bearer ' . $apiKey],
]);

$body = json_decode(curl_exec($ch), true);
curl_close($ch);
// kind travels in the query string, the second argument.
$shared = Api::Notifications()->GetTelegramChats([], ['kind' => 'shared'])['data'];

Reading a Chat

get/api/v1/admin/notifications/telegram/chats/{id}
Notifications/GetTelegramChat admin

Returns one linked chat.

Path parameters 1
idintThe chat row id.
Response fields data
dataobjectThe chat. Same shape as one row of the list.
Errors 3
invalid_id422The id is not a positive integer.
chat_not_found404No chat with this id.
insufficient_scope403The key lacks the required scope.
Request
curl 'https://panel.example.com/api/v1/admin/notifications/telegram/chats/5' \
  -H "Authorization: Bearer $API_KEY"
const res = await fetch(`https://panel.example.com/api/v1/admin/notifications/telegram/chats/${chatId}`, {
  headers: { Authorization: `Bearer ${apiKey}` },
});

const { data } = await res.json();
$ch = curl_init('https://panel.example.com/api/v1/admin/notifications/telegram/chats/' . $chatId);
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER     => ['Authorization: Bearer ' . $apiKey],
]);

$body = json_decode(curl_exec($ch), true);
curl_close($ch);
// last_error holds Telegram's own reason when the chat is broken.
$chat = Api::Notifications()->GetTelegramChat(['id' => 5])['data'];

Updating a Chat

patch/api/v1/admin/notifications/telegram/chats/{id}
Notifications/UpdateTelegramChat admin a field left out keeps its value

Changes a chat's default flag and quiet hours. Every field is checked before anything is written.

Path parameters 1
idintThe chat row id.
Body 4
is_defaultboolTrue makes this shared chat the default and takes the flag from the previous one. The default cannot be cleared directly; make another shared chat the default instead.
quiet_enabledboolThe quiet hours switch. Turning it off keeps the saved range.
quiet_startstringThe range start, HH:MM in 24-hour form.
quiet_endstringThe range end, HH:MM in 24-hour form. While quiet hours are on, the two times must differ.
Response fields data
dataobjectThe chat after the write. Same shape as one row of the list.
Errors 10
invalid_id422The id is not a positive integer.
chat_not_found404No chat with this id.
personal_chat422The default flag was asked for on a personal chat.
default_required422The flag was cleared on the default chat.
invalid_quiet_hours422A bound is not HH:MM, or quiet hours are on with equal or missing bounds.
invalid_is_default422The default flag is not a switch value.
invalid_quiet_enabled422The quiet hours switch is not a switch value.
invalid_quiet_start422The range start is an array or an object.
invalid_quiet_end422The range end is an array or an object.
insufficient_scope403The key lacks the required scope.
Request
curl -X PATCH 'https://panel.example.com/api/v1/admin/notifications/telegram/chats/5' \
  -H "Authorization: Bearer $API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{"quiet_enabled":true,"quiet_start":"22:00","quiet_end":"08:00"}'
const res = await fetch(`https://panel.example.com/api/v1/admin/notifications/telegram/chats/${chatId}`, {
  method:  'PATCH',
  headers: { Authorization: `Bearer ${apiKey}`, 'Content-Type': 'application/json' },
  body:    JSON.stringify({ is_default: true }),
});

const body = await res.json();
$ch = curl_init('https://panel.example.com/api/v1/admin/notifications/telegram/chats/' . $chatId);
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST  => 'PATCH',
    CURLOPT_HTTPHEADER     => ['Authorization: Bearer ' . $apiKey, 'Content-Type: application/json'],
    CURLOPT_POSTFIELDS     => json_encode(['quiet_enabled' => false]),
]);

$body = json_decode(curl_exec($ch), true);
curl_close($ch);
// Quiet hours silence the sound, not the message: everything still arrives.
Api::Notifications()->UpdateTelegramChat([
    'id' => 5, 'quiet_enabled' => true, 'quiet_start' => '22:00', 'quiet_end' => '08:00',
]);

Removing a Chat

delete/api/v1/admin/notifications/telegram/chats/{id}
Notifications/DeleteTelegramChat admin the bot leaves a shared chat

Removes a linked chat. For a shared chat the bot also leaves the group or channel.

Path parameters 1
idintThe chat row id.
Response fields data — 2
idintThe removed chat row id.
deletedboolAlways true.
Errors 4
invalid_id422The id is not a positive integer.
chat_not_found404No chat with this id.
delete_failed500The chat row could not be removed.
insufficient_scope403The key lacks the required scope.
Request
curl -X DELETE 'https://panel.example.com/api/v1/admin/notifications/telegram/chats/6' \
  -H "Authorization: Bearer $API_KEY"
const res = await fetch(`https://panel.example.com/api/v1/admin/notifications/telegram/chats/${chatId}`, {
  method: 'DELETE',
  headers: { Authorization: `Bearer ${apiKey}` },
});

const body = await res.json();
$ch = curl_init('https://panel.example.com/api/v1/admin/notifications/telegram/chats/' . $chatId);
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST  => 'DELETE',
    CURLOPT_HTTPHEADER     => ['Authorization: Bearer ' . $apiKey],
]);

$body = json_decode(curl_exec($ch), true);
curl_close($ch);
// Removing the default chat moves the flag; read the list again afterwards.
Api::Notifications()->DeleteTelegramChat(['id' => 6]);

Sending a Test Message

post/api/v1/admin/notifications/telegram/chats/{id}/test
Notifications/SendTelegramTestMessage admin a real message

Sends the panel's test message to the chat. A delivered message marks the chat active again; a refusal marks it broken.

Path parameters 1
idintThe chat row id.
Body —
——No body is needed. The message text is fixed; send an empty body.
Response fields data — 2
idintThe chat row id.
message_idintTelegram's id of the delivered message.
Errors 4
invalid_id422The id is not a positive integer.
chat_not_found404No chat with this id.
telegram_error422Telegram did not deliver the message. The message carries Telegram's reason.
insufficient_scope403The key lacks the required scope.
Request
curl -X POST 'https://panel.example.com/api/v1/admin/notifications/telegram/chats/5/test' \
  -H "Authorization: Bearer $API_KEY"
const res = await fetch(`https://panel.example.com/api/v1/admin/notifications/telegram/chats/${chatId}/test`, {
  method: 'POST',
  headers: { Authorization: `Bearer ${apiKey}` },
});

const body = await res.json();
$ch = curl_init('https://panel.example.com/api/v1/admin/notifications/telegram/chats/' . $chatId . '/test');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST           => true,
    CURLOPT_HTTPHEADER     => ['Authorization: Bearer ' . $apiKey],
]);

$body = json_decode(curl_exec($ch), true);
curl_close($ch);
// A broken chat that takes the message comes back as active.
$sent = Api::Notifications()->SendTelegramTestMessage(['id' => 5]);

Pitfalls

A different bot breaks every chat

Saving the token of a different bot marks every linked chat as broken, because the new bot is not a member of them. Notifications stop until the chats are linked again in the panel. Compare the bot id before and after a token change.

A chat cannot be linked through the API

Linking needs a one-time link that a person opens in Telegram. These endpoints only read, adjust and remove chats that are already linked. A removed chat comes back only through the panel.

Removing the default moves the flag

When the removed chat was the default, the flag moves to the oldest remaining active shared chat. With none left, no chat is the default until one is chosen.

The test message is a real message

The chat test sends a real Telegram message that everyone in the chat sees. The bot test checks the token alone and sends nothing.

هل كان هذا مفيدًا؟

شكرًا على ملاحظاتك!

هل ما زلت بحاجة إلى مساعدة؟

فريق الدعم متاح على مدار الساعة لمساعدتك في كل ما لم تجده في قاعدة المعرفة.