【问题标题】:Send a scheduled message in my telegram bot linked to a google spreadsheet在链接到谷歌电子表格的电报机器人中发送预定消息
【发布时间】:2021-08-20 04:26:41
【问题描述】:
我使用此视频中的指导构建了一个电报机器人:
https://www.youtube.com/watch?v=mKSXd_od4Lg&t=348s
有没有什么方法可以编辑 sendText 函数,让机器人在设定的时间发送消息?
function sendText(id,text){
var url = telegramUrl + "/sendMessage?chat_id=" + id + "&text=" + text;
var response = UrlFetchApp.fetch(url);
}
谢谢!
【问题讨论】:
标签:
javascript
google-sheets
bots
telegram
telegram-bot
【解决方案1】:
为此,您需要将id 和text 临时存储在script properties 中,因为您无法将参数传递给时钟触发器。
示例:
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);
}