Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83646

How to send a message to Telegram from Zapier

Zapier does not offer native integration for Telegram. How can one send a message to Telegram chat from Zapier?

Upvotes: 2

Views: 3331

Answers (2)

Joel
Joel

Reputation: 155

There's no need to do this using Python or Javascript. You can use the POST function in Zapier's Webhooks to send a message directly via the Bot API:

URL https://api.telegram.org/bot{BOT_API_TOKEN}/sendMessage

Payload Type json

Data

  • text your message
  • chat_id -{CHANNEL_OR_GROUP_ID}

Note the use of the - symbol in front of the chat_id; you must have this in order for it to work.

Upvotes: 1

Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83646

Zapier offers Code actions that can execute JavaScript or Python code. You can use JavaScript fetch and Telegram HTTP API to post messages to Telegram chats through your bot.

// TG bot API documentation https://core.telegram.org/bots/api

// Set up the bot with BotFather and the API token https://telegram.me/BotFather
const TG_API_TOKEN = "xxx";

// Add the bot to a chat
// In chat type: /start to make the bot to recognise the chat 
// Get chat it by calling TG getUpdates API in terminal and picking
// the chat id from the output by hand
//
//  curl https://api.telegram.org/bot$TG_API_TOKEN/getUpdates | jq 
// 
const CHAT_ID = "xxx";

async function postData(url, data) {
  // Default options are marked with *
  const response = await fetch(url, {
    method: 'POST', 
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(data) // body data type must match "Content-Type" header
  });
  return response.json(); // parses JSON response into native JavaScript objects
}

// Create the message using uptick formatting from whatever inputData field 
// you choose in Zapier
const message = `Hello my old friend ${inputData.id}`;

console.log("Sending out", message);

// Create sendMessage payload
const payload = {chat_id: CHAT_ID, text: message, disable_notification: false};

// Which endpoint we are calling
const endpoint = `https://api.telegram.org/bot${TG_API_TOKEN}/sendMessage`;

// Call Telegram HTTP API
const resp = await postData(endpoint, payload);

console.log("We got", resp);

// Zapier scripts needed output - pass Telegram API response
output = resp;

Upvotes: 6

Related Questions