1

I've built a telegram bot using guidance from this video:

https://www.youtube.com/watch?v=mKSXd_od4Lg&t=348s

Is there any way to edit the sendText function such that I could allow the bot to send a message at a set time?

function sendText(id,text){
  var url = telegramUrl + "/sendMessage?chat_id=" + id + "&text=" + text;
  var response = UrlFetchApp.fetch(url);
}

Thank you!

Colin
  • 31
  • 1

1 Answers1

0

You can include into your code a call to a ClockTrigger

For this, you need to store the id and text temporarily in script properties since you cannot pass parameters to a clock trigger.

Sample:


const props = PropertiesService.getScriptProperties();

function sendText(id,text){
  props.setProperty('id', JSON.stringify(id));
  props.setProperty('text', text);
  ScriptApp.newTrigger("fetch")
    .timeBased()
    .after(10 * 60 * 1000)
    .create();
}

function fetch(){
  var id = JSON.parse(props.getProperty('id'));
  va text =  props.getProperty('text');
  var url = telegramUrl + "/sendMessage?chat_id=" + id + "&text=" + text;
  var response = UrlFetchApp.fetch(url);
}
ziganotschka
  • 25,866
  • 2
  • 16
  • 33