TeleQube from code: QubApp.messenger
QubApp.messenger lets code read the TeleQube conversations of its owner and write in them as that person: a group of the team, a conversation with a colleague, a Telegram or WhatsApp conversation of the owner's account. Its methods have the names and the parameters of Telegram: writing — of the Bot API, reading, which the Bot API does not have, — of Telegram's client API.
The shortest example
function main() {
const chats = QubApp.messenger.getChats({ limit: 50 });
const buyers = chats.conversations.find(function (c) { return c.title === 'Buyers'; });
QubApp.messenger.sendMessage({ chat_id: buyers.topic, text: 'Campaign 123 is paused: ROAS 0.4' });
}
chat_id is a conversation's topic from getChats.
Methods
| Method | What it does |
|---|---|
getChats({ limit }) | the conversations the owner reads, the most recent first: topic, title, group, system (the owner's read-only Qubix channel), net and account of a Telegram or WhatsApp conversation, last — its last message; total — how many there are in all |
getChat({ chat_id }) | one conversation of that list, or null; a conversation beyond the most recent ones the list gives is refused with words — read it by its chat_id with getChatHistory |
getChatHistory({ chat_id, from_message_id, limit }) | a page of messages, the newest first: seq, at, mine, from, text, files, reactions; from_message_id pages down below that message, more says older ones remain |
getFile({ file_id }) | a file of a message: name, mime, size and data_b64 — its bytes in base64 |
sendMessage({ chat_id, text }) | writes a message and answers { message_id, chat: { id }, text } |
setMessageReaction({ chat_id, message_id, reaction }) | reaction is a list of { type: 'emoji', emoji }; as in the Bot API, it replaces the owner's own reactions on the message, and an empty list takes them all off; answers true |
message_id is a message's seq. The texts of messages are what people wrote: code that acts on them should check them first.
Whose rights a call has
The calls go as the owner of the code, with the same rights and refusals as the TeleQube window: a conversation the owner does not read is not there; writing is refused where the window refuses it — a read-only conversation, a removed or switched-off colleague, a blocked person. In a Telegram or WhatsApp conversation the message goes on into the network from the owner's account, and its delivery comes later: read it with getChatHistory.
| Where the code runs | Whose rights the calls have |
|---|---|
| Scripts: on a schedule and with the Run now button | the script's owner |
As with QubApp.mcp, the code must be last saved by its owner or by an administrator.
Limit of calls
One run of code may make as many calls as the QubApp.messenger calls per run field allows — the TeleQube from code group on the JavaScript tab of System; 100 by default. setMessageReaction makes one call to read the message and one for each reaction it puts or takes off.
When a call fails
A refused call throws an exception with the reason in words, in the language of the code owner's profile, Russian or English; a mistake in the parameters (no chat_id, no text, a reaction that is not { type: 'emoji', emoji }, a parameter sendMessage or setMessageReaction does not take, such as parse_mode) is a TypeError. An exception that is not caught stops the run with an error.