首页 > 解决方案 > 在 UrlFetchApp.fetch(...) 中使用变量不起作用

问题描述

我想使用 Google App Script 让 Telegram Bot 在聊天中发布消息。当我使用这行代码时,效果很好:

var message = UrlFetchApp.fetch(url + "/sendMessage?chat_id=" + chat_id_Testgruppe + "&text=HelloWorld");

但是,如果我用这样的变量替换“HelloWorld”:

var body = threads[i].getMessages().pop().getPlainBody();  // Gets the message-String from a Gmail-Thread
var message = UrlFetchApp.fetch(url + "/sendMessage?chat_id=" + chat_id_Testgruppe + "&text=" + body);

然后我得到一个无效的参数错误,即使显示为 fetch 函数的非法参数的参数有效,如果我将它复制并粘贴到我的浏览器。

标签: google-apps-script

解决方案


我需要在我的变量上使用 encodeURIComponent() ,这解决了这个问题。


推荐阅读