Skip to content

Sends messages to a Telegram chat through a bot and waits for the answer, for notifications and simple human-in-the-loop approvals. Telegram.bot returns a TelegramBot tied to one chat. Setup: create a bot with @BotFather to get a token, send the bot a message from the chat that should receive the notifications, and find that chat's numeric id (bot.poll() shows chat_id of the incoming message). Keep the token in a secret: secret("name"). Things to know: answers are read with long polling (getUpdates). A bot that has a webhook set, or that is read by another running script, does not receive updates here. Updates are consumed once they have been read. All calls go to api.telegram.org and raise "Telegram API error: ..." on failure.

Functions

  • bot TelegramBot Creates a bot for one chat and checks the token with the Telegram getMe call, so an invalid token raises "Telegram API error: Unauthorized" here.
  • close self Closes every bot created with Telegram.bot in this script.
Telegram.bot(token: string, chatId: string)→TelegramBot

Creates a bot for one chat and checks the token with the Telegram getMe call, so an invalid token raises "Telegram API error: Unauthorized" here. The chat id must be a number or numeric text; negative ids (groups) work, and a non-numeric value such as "@mychannel" raises an error. Every message the bot sends goes to this chat, and only replies from this chat are read.

Parameters

token string
Bot token from @BotFather; use secret()
chatId string
Numeric id of the chat to talk to (negative for groups)

Returns

TelegramBot A Telegram bot bound to one chat.

Example

local bot = Telegram.bot(secret("telegram_token"), secret("telegram_chat_id"))
bot.send("Nightly run finished: 42 checks passed")
bot.close()
Telegram.close()→self

Closes every bot created with Telegram.bot in this script. A closed bot cannot be used again.

Returns

self chainable The library itself, so calls can be chained.