Jan 09, 2021 12:07 AM
Create a Telegram bot using @BotFather
Save the API key
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]
Create or choose chat to add the bot
Invite @getidsbot (or any similar - there are plenty of them) to chat and save the chat ID which was retrieved by it
Invite your new bot (created on step 1) to the chat
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]
If the bot sent the [MY_MESSAGE_TEXT] to the desired chat - you’re halfway through
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.
Create an automation, lets say, for when a new record appears
Add a trigger and choose “When record created ->” from Airtable section and select a table
Add an action and choose “Run script ->” from Airtable section
Add a new input variable In the left pane of script editor
Give the input name and choose the desired property of desired record. It’ll be referred from script body as inputConfig.[variable_name]
Edit a script adding let inputConfig = input.config(); in the beginning of it
Form a text using static text and Input Variable values
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’});
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)
Jan 11, 2021 07:02 AM
A small edition: how to send messages to private channels
Sending messages to the private channels:
Jun 17, 2021 01:49 PM
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'});
Mar 29, 2022 05:50 AM
Thanks for sharing this @ellie_rae