# telegram cheatsheet
Every export of @deckyard/telegram. Background is in the introduction .
jennifer import "@deckyard/telegram/" as telegram;
API calls throw a TelegramError (kind "telegram") on {"ok": false}, a non-JSON reply or a transport failure, with the token removed from the message. A socket failure is http's net.NetError (kind "net"); a reached cap, a deadline, a connection closed mid-answer and a cancelled task keep their kinds "limit", "timeout", "closed" and "cancelled".
# Types
# Bot
Field Meaning token as stringbot token from BotFather™ baseUrl as stringAPI base URL, no trailing slash
# User
Field Meaning id as intuser id isBot as boolwhether the user is a bot firstName as stringfirst name username as stringusername, "" if none
# Message
Field Meaning messageId as intmessage id chatId as intchat id text as stringtext, "" for non-text messages date as intsend time, Unix seconds
# Update
Field Meaning updateId as intupdate id; poll from this + 1 next hasMessage as boolwhether message is set message as Messagethe message, zero when hasMessage is false
Field Meaning text as stringlabel url as stringURL to open, "" for a callback button callbackData as stringcallback payload, "" for a URL button
# CallbackQuery
Field Meaning 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
# TelegramError
Every error of kind "telegram".
Field Meaning kind as stringalways "telegram" message as stringtelegram: and Telegram™'s description, or what went wrongerrorCode as intTelegram's error_code (400, 403, 429, ...); 0 when not an API error retryAfter as intunder flood control, seconds to wait before retrying; 0 otherwise migrateToChatId as inta group's new supergroup chat id; 0 otherwise status as intthe reply's HTTP status; 0 when none was read
# Bots
Function Returns Notes telegram.bot(token)Botpublic API telegram.botWith(token, baseUrl)Botself-hosted server or test endpoint telegram.getMe(b)Usertoken check
# Sending
Function Returns Notes telegram.sendMessage(b, chatId, text)Messagetelegram.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
Function Returns Notes 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
Function Returns Notes telegram.getUpdates(b, offset, timeoutSec)list of Updatelong poll, timeoutSec in seconds; text messages only telegram.getUpdatesRaw(b, offset, timeoutSec)list of json.Valuethe same poll, every update as decoded JSON telegram.parseUpdate(update)Updateone raw update; throws telegram without update_id
# Helpers
Function Returns Notes telegram.buildUpload(field, chatId, filename, contentType, data)multipart.Builtan upload form, not sent telegram.redactToken(s, token)stringremoves the token from a string