Skip to content

Instantly share code, notes, and snippets.

@Kaz-su
Last active August 13, 2019 09:46
Show Gist options
  • Save Kaz-su/62eb5efd5543bad52b1e6ae1ced69949 to your computer and use it in GitHub Desktop.
Save Kaz-su/62eb5efd5543bad52b1e6ae1ced69949 to your computer and use it in GitHub Desktop.
誰かのツイートが消えてないか監視するやつ(GAS)
// Queryfeed でチャンネルにツイートを垂れ流している場合のみ利用可能
var token = '{slack-token}';
var channelID = '{channel-id}';
var userName = '{user-name}'; // @realDonaldTrump とか
function myFunction() {
var latest = Math.floor(new Date().getTime() / 1000);
var oldest = latest - 24 * 60 * 60;
var url = 'https://slack.com/api/channels.history?channel=' + channelID + '&oldest=' + oldest + '&latest=' + latest;
var queryfeedsHeaders = {
Authorization: 'Bearer '+ token,
contentType: 'application/x-www-form-urlencoded'
};
var queryfeedsOptions = {
method: 'GET',
headers: queryfeedsHeaders
};
var queryfeeds = UrlFetchApp.fetch(url, queryfeedsOptions);
var tweets = JSON.parse(queryfeeds).messages.filter(function (message) {
return message.attachments && message.attachments[0].author_subname === userName;
});
var tweetTextAndUrls = tweets.map(function (message) {
return {
text: message.attachments[0].text,
url: message.attachments[0].original_url
};
});
// Tweetを時刻降順で取得しているので、reverseして時系列どおりチェックを行う
tweetTextAndUrls.reverse().forEach(function (textAndUrl) {
var result = UrlFetchApp.fetch(textAndUrl.url, { muteHttpExceptions: true });
if (result.getResponseCode() === 404) {
notify('ツイートが消えました: ' + textAndUrl.text);
}
});
}
function notify (message) {
var headers = {
Authorization: 'Bearer '+ token,
contentType: 'application/json'
};
var data = {
channel: channelID,
text: message
};
var options = {
method: 'POST',
headers: headers,
payload: data
};
UrlFetchApp.fetch('https://slack.com/api/chat.postMessage', options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment