Action

Telegram Bot Post

Posted by David Blue, Last update about 2 months ago

UPDATES

about 2 months ago

-Description update
-Listed publicly

Send draft contents to a specified Telegram channel/group chat via the Telegram Bot API.

The URL of the result will be copied to the clipboard and inserted as a markdown-formatted hyperlink one line break below the cursor’s current position in the following format:

[chatname-messageid](messageURL)

Example Result

Setup

Upon first run, the action creates the Telegram Bot credential identifier, which requires both the numeric chat_id of the target group chat/channel (best obtained with the Rose bot, imo,) and a bot API token (which you can obtain from @botfather)

Steps

  • script

    // setup and request credentials
    let credential = Credential.create("Telegram Bot", "Enter your bot token and target chat id.");
    
    credential.addTextField("chat", "Chat ID");
    credential.addPasswordField("token", "Bot Token");
    credential.authorize();
    
    const chat = credential.getValue("chat");
    const token = credential.getValue("token");
    
    // make post
    let endpoint = `https://api.telegram.org/bot${token}/sendMessage`;
    
    let data = {
    	"chat_id": chat,
    	"text": draft.content,
    	"parse_mode": "markdown",
    };
    
    // create and post HTTP request
    var http = HTTP.create();
    
    var response = http.request({
    	"url": endpoint,
    	"method": "POST",
    	"data": data,
    });
    
    // log result post ID and setup template tags for insertion
    if (response.success == true) {
    	let d = JSON.parse(response.responseText);
    	console.log("Posted to Telegram Message ID: " + d.result.message_id);
    	draft.setTemplateTag("messageid", d.result.message_id);
    	draft.setTemplateTag("chatalpha", d.result.chat.username);
    }
    
    else {
    	console.log("Telegram Error: " + response.error);
    	context.fail();
    }
  • insertText

    template
    - [[[chatalpha]]-[[messageid]]](https://t.me/[[chatalpha]]/[[messageid]])
  • clipboard

    template
    https://t.me/[[chatalpha]]/[[messageid]]

Options

  • After Success Nothing
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.