【问题标题】:Using variables in UrlFetchApp.fetch(...) doesn't work在 UrlFetchApp.fetch(...) 中使用变量不起作用
【发布时间】:2019-12-01 16:43:01
【问题描述】:

我想使用 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 函数的非法参数的参数有效,如果我将它复制并粘贴到我的浏览器。

【问题讨论】:

  • 如果不了解您的其他变量,我们将无法解决此问题。
  • 添加了我的 body 声明,另外两个变量是硬编码的字符串,它们在其他上下文中正常工作
  • 如果您执行Logger.log(body);,会记录什么?你也试过使用body.toString()吗?
  • this more general question 以及 this one -- 在 body 上尝试 encodeURIcomponent();还要记住there may be issues with the length of the message

标签: google-apps-script


【解决方案1】:

Telegram API reference 指定 getMessages() 返回消息的LIST - 这是一个数组。您需要访问单个消息(列表的数组条目)才能以字符串形式获取其内容。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      • 1970-01-01
      • 2014-09-23
      相关资源
      最近更新 更多