Help

Re: Script: Sending telegram notifications via bot (without 3rd party integration services)

3652 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Shoguevara
4 - Data Explorer
4 - Data Explorer
  1. Create a Telegram bot using @BotFather

  2. Save the API key

  3. Syntaxis to send HTTP POST command is https://api.telegram.org/bot[BOT_API_KEY]/sendMessage?chat_id=[MY_CHAT_ID]&text=[MY_MESSAGE_TEXT]

  4. Create or choose chat to add the bot

  5. Invite @getidsbot (or any similar - there are plenty of them) to chat and save the chat ID which was retrieved by it

  6. Invite your new bot (created on step 1) to the chat

  7. Test the bot, inserting the URL from step 3 into web browsers URL pane and hit Enter. Change [BOT_API_KEY] for one acquired on step 2, change [MY_CHAT_ID] for one acquired on step 5 and define some custom message to put in place of [MY_MESSAGE_TEXT]

  8. If the bot sent the [MY_MESSAGE_TEXT] to the desired chat - you’re halfway through

  9. Open https://airtable.com/ and choose desired base. In my case it is a base with a table, which also has a publicly accessible form attached to it, so “external” users could’ve been able to create new records without having an Airtable account.

  10. Create an automation, lets say, for when a new record appears

  11. Add a trigger and choose “When record created ->” from Airtable section and select a table

  12. Add an action and choose “Run script ->” from Airtable section

  13. Add a new input variable In the left pane of script editor

  14. Give the input name and choose the desired property of desired record. It’ll be referred from script body as inputConfig.[variable_name]

  15. Edit a script adding let inputConfig = input.config(); in the beginning of it

  16. Form a text using static text and Input Variable values

  17. Send a HTTP request using formed url via fetch() function

    var tgurl = new String();

    tgurl = ‘https://api.telegram.org/bot[BOT_API_KEY]/sendMessage?chat_id=[MY_CHAT_ID]&text= [static_text]’

    tgurl = tgurl.concat(’’, inputConfig.[variable_name])

    fetch((tgurl), {method: ‘POST’, body: ‘Hi there’});

  18. Save test and debug the script

Here’s an example of a script, which uses values from 3 Input Variables - properties of a new record, when “When record created” trigger fired:

let inputConfig = input.config();

var tgurl = new String();

tgurl = 'https://api.telegram.org/bot[BOT_API_KEY]/sendMessage?chat_id=[MY_CHAT_ID]&text=List of values -Value 1: '
tgurl = tgurl.concat('', inputConfig.[variable1])
 
tgurl = tgurl.concat('', ' Value 2: ')
tgurl = tgurl.concat('', inputConfig.[variable2])
 
tgurl = tgurl.concat('', ' Value 3: ')
tgurl = tgurl.concat('', inputConfig.[variable3])
 
console.log(`The value of dsc is ${tgurl}`);
 
fetch((tgurl), {method: 'POST', body: 'Hi there'});

This will a text to the chat from bot upon a new table records appearance, like:
List of values -Value 1: [variable1_value] -Value 2: [variable2_value] -Value 3: [variable3_value]

I do realize that the code is neither beautiful, nor elegant, but it does it’s job.
Refactoring suggestions are welcomed (thi is the 1st time I try to do smth with JS - I ain’t coder)

3 Replies 3
Shoguevara
4 - Data Explorer
4 - Data Explorer

A small edition: how to send messages to private channels
Sending messages to the private channels:

  1. First, make the channel public
  2. Send an HTTP POST request using channel name via https://api.telegram.org/bot[BOT_API_KEY]/sendMessage?chat_id=[@MY_CHANNEL_NAME]&text=[MY_MESSAGE_TE...
  3. Obtain CHAT_ID from the response
  4. Configure bot to use CHAT_ID instead of MY_CHANNEL_NAME
  5. Convert channel back to private
ellie_rae
4 - Data Explorer
4 - Data Explorer

Refactored, plus added HTML formatting support! (For some reason the message doesn’t send if I try to use Markdown?)

Disclaimer: I refuse to learn more JS than absolutely necessary so while this code works just fine for me, I won’t guarantee it works 100% of the time :stuck_out_tongue:

let inputConfig = input.config();
let VARIABLE1 = inputConfig.VARIABLE1;
let VARIABLE2 = inputConfig.VARIABLE2;

let apiKey = 'API KEY';
let chatID = 'CHAT ID';

let msg = `VARIABLE1: ${VARIABLE1}, <b>VARIABLE2:</b> ${VARIABLE2})`;
let url = `https://api.telegram.org/bot${apiKey}/sendMessage?chat_id=${chatID}&text=${msg}&parse_mode=HTML`;
 
console.log(`The message is ${msg}`);
console.log(`The URL is ${url}`);
 
fetch((url), {method: 'POST', body: 'Telegram msg'});

Thanks for sharing this @ellie_rae