在 (Google Sheets) 中创建 stackoverflow 收件箱未读消息列表以在 Telegram 中创建机器人并接收这些通知

Create stackoverflow inbox unread messages list in (Google Sheets) to create a bot in Telegram and receive these notifications

要获取未读的收件箱消息,有这个 API:

https://api.stackexchange.com/docs/inbox-unread

{
  "items": [],
  "has_more": false,
  "quota_max": 10000,
  "quota_remaining": 9998
}

我想寻求帮助,让我在 Google 表格中列出这些收件箱提醒。通过一个脚本,我承认我无法 assemble 一个能够做到列出消息的功能。

我的想法是获取此列表,然后通过脚本创建一个函数,将这些警报发送到我的 Telegram,如下所示:

function EnviarTelegram(botSecret, chatId, body) {
var response = UrlFetchApp.fetch("https://api.telegram.org/bot" + botSecret + "/sendMessage?text=" + encodeURIComponent(body) + "&chat_id=" + chatId + "&parse_mode=HTML");
}

那样的话,我会在 Telegram 上得到类似的东西,就像我已经拥有的 RSS Feeds 一样:

但是我不想收到新问题的通知,而是希望在有人回答我的问题、我获得积分等时收到我的收件箱通知。

  • 您想使用 Stackexchange API.
  • 检索自己在 Whosebug 上的未读评论和声誉
  • 您想将检索到的值放入电子表格。
  • 您想使用 Google Apps 脚本实现此目的。

如果我的理解是正确的,这个答案怎么样?请将此视为几个答案之一。

用法:

1。检索访问令牌

为了检索未读的评论和声誉,需要使用访问令牌。

流:
  1. Register an application at stackapps.com.
    • 输入"Application Name"、"Description"、"OAuth Domain"和"Application Website"。
      • 作为示例凭证,我将它们输入为 "sampleApp"、"This is a sample app."、"localhost" 和 "sample".
      • 为了用作重定向uri,请将"localhost"设置为"OAuth Domain"。
    • 复制 "Client Id"、"Client Secret" 和 "Key"。 "Key" 也需要使用 API.
  2. 授权范围。
    • 请创建以下 URL 并使用您的浏览器访问它。请设置您的 client_id.
    • https://stackexchange.com/oauth?client_id=#####&scope=no_expiry%20read_inbox&redirect_uri=http://localhost
      • 在此脚本中,"no_expiry" 和 "read_inbox" 用作范围。这些范围用于检索未读评论。
    • 当浏览器打开创建的URL后,请点击"Approve"如下。
    • 从浏览器 URL 中复制代码 ### code ###,例如 http://localhost/?code=### code ###
  3. 使用 "Client Id"、"Client Secret" 和 "Code" 检索访问令牌。请 运行 以下 curl 命令。这样,您可以检索访问令牌,如 access_token=#####.

    • 此访问令牌没有过期时间,因为 "no_expiry" 包含在范围内。所以你可以继续使用这个访问令牌。

      curl \
          -d "client_id=#####" \
          -d "client_secret=#####" \
          -d "code=#####" \
          -d "redirect_uri=http://localhost" \
          "https://stackexchange.com/oauth/access_token"
      

2。 运行 脚本

示例脚本 1:

此示例脚本检索未读评论。

function myFunction() {
  var key = "###"; // Please set your key.
  var accessToken = "###"; // Please set your access token.
  var spreadsheetId = "###"; // Please set the Spreadsheet ID.

  var url = "https://api.stackexchange.com/2.2/inbox/unread?filter=withbody&pagesize=100&access_token=" + accessToken + "&key=" + key;
  var comments = JSON.parse(UrlFetchApp.fetch(url).getContentText());

  var headers = ["creation_date", "link", "title", "body"];
  var values = comments.items.map(function(item) {return headers.map(function(h) {return item[h]})});
  values.unshift(headers);
  var sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName("Sheet1");
  sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}
示例脚本 2:

此示例脚本检索信誉历史记录。

function myFunction() {
  var key = "###"; // Please set your key.
  var accessToken = "###"; // Please set your access token.
  var userId = "###"; // Please set your user ID on Whosebug.
  var spreadsheetId = "###"; // Please set the Spreadsheet ID.

  var url = "https://api.stackexchange.com/2.2/users/" + userId + "/reputation-history/full?site=Whosebug&pagesize=100&access_token=" + accessToken + "&key=" + key;
  var reputation = JSON.parse(UrlFetchApp.fetch(url).getContentText());

  var headers = ["creation_date", "reputation_change", "post_id"];
  var values = reputation.items.map(function(item) {return headers.map(function(h) {return item[h]})});
  values.unshift(headers);
  var sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName("Sheet1");
  sheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}
  • 当您使用它时,您可以通过检查日期和时间来检索您的声誉变化。

注:

  • 这是一个示例脚本。所以请根据自己的实际情况进行修改。
  • 遗憾的是,在目前阶段,API中似乎没有任何方法可以在收到新评论时自动通知。因此,作为 Google Apps 脚本的一种解决方法,我正在 运行 使用 time-driven 触发器来设置脚本。通过这个,我可以知道新的评论。也可以知道声望的变化。

参考文献:

如果我误解了您的问题而这不是您想要的结果,我深表歉意。

更新时间:2020 年 3 月 19 日

自 2020 年 1 月起,访问令牌不能与 access_token=### 等查询参数一起使用。 Ref 所以请使用请求的访问令牌 header 而不是查询参数。如下

var res = UrlFetchApp.fetch(url, {headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}});