Help

Re: Sending messages via a bot in Telegram

3342 3
cancel
Showing results for 
Search instead for 
Did you mean: 
NikPaw
5 - Automation Enthusiast
5 - Automation Enthusiast

Hi all.
Perhaps someone can help me.
I need help from someone who understands the Telegram API.
Below is the code of a script which should work under certain conditions in Automations and send a message to a Telegram user via bot in an open chat with some data from the table.
At the moment the script sends an error

error_code: 400
description: "Bad Request: message text is empty"

The script works fine in the sandbox and sends messages. Not in Airtable.
Perhaps the problem is related to Airtable and its specific variables?
How do you think it is possible to solve this problem.
On Stack Owerflow people occasionally write about this problem, but their solutions have not helped me.
I would be grateful for an answer.

const token = 'ххх';
let inputConfig = input.config();
let message = `${inputConfig.first_name}, документ готов по заказу №${inputConfig.order}.<br>Вы можете скачать его <a href = "${inputConfig.doc_link}"по ссылке</a>.<br>Ждем Вас снова.<br>Ваш, RealEstate DocuService`;
console.log(`${message}`);
const options = {
  method: 'POST',
  Host: 'api.telegram.org:443',
  headers: {
    Accept: 'application/json',
    'User-Agent': 'Airtable',
    'Content-Type': 'application/json',    
  },
  body: JSON.stringify({
    text: `${message}`,
    parse_mode: 'HTML',
    disable_web_page_preview: true,
    disable_notification: true,
    reply_to_message_id: 0,
    chat_id: `${inputConfig.chat_id}`
 })
};
fetch(`https://api.telegram.org/bot${token}/sendMessage?chat_id=${inputConfig.chat_id}`)
  .then(response => response.json())
  .then(response => console.log(response))
  .catch(err => console.error(err));
5 Replies 5

HI @NikPaw
Do you have all of the input.config variables set up on the script?

were the variables first_name, order, doc_link, chat_id
All of them are well displayed by console.log

Screenshot_145

Hm Nik, looks like you’re not sending the options

Could you replace:

fetch(`https://api.telegram.org/bot${token}/sendMessage?chat_id=${inputConfig.chat_id}`)

with:

fetch(`https://api.telegram.org/bot${token}/sendMessage?chat_id=${inputConfig.chat_id}`, options)

And see whether that works?

@Adam_TheTimeSavingCo you have helped me a lot.
Thank you very much for the tip.
I somehow thought that options was for tying additional parameters like ?=param=‘Yes’ into the link
Now it really works. The script sends the message through the bot.
:sparkles: :man_dancing:

ReesSamantha
4 - Data Explorer
4 - Data Explorer

Sorry for bumping into an old conversation. I must mention that while I can assist you with general programming concepts and problem-solving, I'm not an expert in the Telegram API specifically. However, I'd like to suggest an alternative approach that might help you achieve your goals. It's worth noting that there are services available where you can get already phone-verified accounts that can be used for various purposes. So, you can Buy Telegram Accounts for automation and messaging. This way you may have more control over the process and potentially overcome any limitations or obstacles you're facing with your current setup.