Action

Send Draft to Telegram Bot

Posted by deanfx, Last update 22 days ago

UPDATES

22 days ago

Updated icon

This action will send the active draft to the specified Telegram Bot automatically without needing to confirm/approve to send as the other action for Saved Messages works.

Required:
Create a bot with @BotFather on Telegram
Obtain your bots Token & ChatID

First Use:
At first run, it will ask you for the bots Token & ChatID. Once entered, you will not have to enter this data again.

X: https://x.com/deanfx
Mastodon: https://twit.social/@deanfx

Steps

  • script

    const creds = Credential.create("Telegram Bot", "Telegram bot token + chat id");
    creds.addPasswordField("token", "Bot Token");
    creds.addTextField("chatId", "Chat ID (numeric)");
    creds.authorize();
    
    const token  = creds.getValue("token");
    const chatId = creds.getValue("chatId");
    
    if (!token || !chatId) context.fail("Missing token or chatId.");
    
    let http = HTTP.create();
    let resp = http.request({
      url: `https://api.telegram.org/bot${token}/sendMessage`,
      method: "POST",
      encoding: "json",
      headers: { "Content-Type": "application/json" },
      data: {
        chat_id: chatId,
        text: draft.content,
        disable_web_page_preview: "true"
      }
    });
    
    if (!resp.success) {
      context.fail(`${resp.statusCode}\n${resp.responseText}\n${resp.error}`);
    }

Options

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