Skip to content
@jennifer/telegram

telegram cheatsheet

Every export of @jennifer/telegram. Background is in the introduction.

jennifer
import "@jennifer/telegram/" as telegram;

API calls throw Error{kind: "telegram"} on {"ok": false}, a non-JSON reply or a transport failure, with the token removed from the message.

Types

Bot

FieldMeaning
token as stringbot token from BotFather
baseUrl as stringAPI base URL, no trailing slash

User

FieldMeaning
id as intuser id
isBot as boolwhether the user is a bot
firstName as stringfirst name
username as stringusername, "" if none

Message

FieldMeaning
messageId as intmessage id
chatId as intchat id
text as stringtext, "" for non-text messages
date as intsend time, Unix seconds

Update

FieldMeaning
updateId as intupdate id; poll from this + 1 next
hasMessage as boolwhether message is set
message as Messagethe message, zero when hasMessage is false

Button

FieldMeaning
text as stringlabel
url as stringURL to open, "" for a callback button
callbackData as stringcallback payload, "" for a URL button

CallbackQuery

FieldMeaning
id as stringid to pass to answerCallbackQuery
from as Userwho pressed the button
data as stringthe button's callback data
messageId as intmessage the button belongs to, 0 if absent

Bots

FunctionReturnsNotes
telegram.bot(token)Botpublic API
telegram.botWith(token, baseUrl)Botself-hosted server or test endpoint
telegram.getMe(b)Usertoken check

Sending

FunctionReturnsNotes
telegram.sendMessage(b, chatId, text)Message
telegram.sendMessageWith(b, chatId, text, parseMode)Message"Markdown", "MarkdownV2", "HTML" or ""
telegram.sendMessageWithKeyboard(b, chatId, text, rows)Messageinline keyboard
telegram.sendPhoto(b, chatId, photo, caption)MessageURL or file id
telegram.sendPhotoFile(b, chatId, filePath, contentType)Messagemultipart upload
telegram.sendDocumentFile(b, chatId, filePath, contentType)Messagemultipart upload
telegram.sendChatAction(b, chatId, action)bool"typing", "upload_photo", ...

Keyboards and callbacks

FunctionReturnsNotes
telegram.urlButton(text, url)Buttonopens a link
telegram.inlineButton(text, callbackData)Buttonsends a callback query
telegram.renderInlineKeyboard(rows)stringthe reply_markup JSON
telegram.parseCallbackQuery(update)CallbackQueryfrom a raw update (getUpdatesRaw); id is "" when it is not a button press
telegram.answerCallbackQuery(b, callbackId, text)boolstops the client's spinner

Receiving

FunctionReturnsNotes
telegram.getUpdates(b, offset, timeout)list of Updatelong poll, timeout in seconds; text messages only
telegram.getUpdatesRaw(b, offset, timeout)list of json.Valuethe same poll, every update as decoded JSON
telegram.parseUpdate(update)Updateone raw update; throws telegram without update_id

Helpers

FunctionReturnsNotes
telegram.buildUpload(field, chatId, filename, contentType, data)multipart.Builtan upload form, not sent
telegram.redactToken(s, token)stringremoves the token from a string