やりたいこと
Googleトレンドの急上昇ワードを、SlackBotに投稿する。
目的は、仕事の息抜き。
ゴールはGoogle急上昇ワードの内容をSlackに送ること!
手順
- SlackAPIトークンキー取得
- GoogleAppsScript作成
- GoogleAppsScriptトリガー設定
SlackAPIトークンキー取得
-
SlackAppsを作成する。
https://api.slack.com/apps -
SlackAppsのBotを作成する。
-
「OAuth & Permissions - Scopes」で権限を設定する。
-
「OAuth & Permissions - OAuth Tokens & Redirect URLs」で「Install App to Workspace」を実行する。
-
「OAuth & Permissions - OAuth Tokens & Redirect URLs」に作成された[Bot User OAuth Access Token]が表示される。
GoogleAppsScript作成
- プロジェクトを作成する。
https://script.google.com/home - 「新しいプロジェクト」を実行する。
- プロジェクト名を変更する。
- コード.gsを変更する。
const GOOGLE_TRENDS_URL = 'https://trends.google.com/trends/api/dailytrends?geo=JP'
const SLACK_URL = 'https://slack.com/api/chat.postMessage'
/* SlackAPIトークンキーを設定!!!! */
const SLACK_TOKEN = 'xoxb-13848890xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
/* SlackAPIを設定したチャンネル名を設定!!!! */
const SLACK_CANNEL = '急上昇ワード'
/**
* function name : notifySlackOfTrend.
* Overview : Notify Slack of trend information.
*/
function notifySlackOfTrend() {
// Get trend information from Google.
const response = UrlFetchApp.fetch(GOOGLE_TRENDS_URL, {
'method' : 'get',
});
const trends = JSON.parse(response.getContentText().replace(/^\)\]\}\'\,/g, ''))
if (trends.default.trendingSearchesDays.length === 0) return
// Create a Slack message from trend information.
const trendingSearches = trends.default.trendingSearchesDays[0].trendingSearches
let trendMessages = ['*Google急上昇ワード*']
for (const trendingSearchesDay of trends.default.trendingSearchesDays) {
trendMessages.push('*' + trendingSearchesDay. formattedDate + '*')
let ranking = 1
for (const trendingSearche of trendingSearchesDay.trendingSearches) {
const message = ranking++ + '. ' + '<https://trends.google.com' + trendingSearche.title.exploreLink + '|' + trendingSearche.title.query + '> '
+ '_' + trendingSearche.formattedTraffic + '_'
+ (trendingSearche.relatedQueries.length ?
'\n```\n関連ワード:' + trendingSearche.relatedQueries.map(relatedQuerie => {
return '<https://trends.google.com' + relatedQuerie.exploreLink + '|' + relatedQuerie.query + '>'
}).join(',')
+ '\n```'
: '')
console.log(message)
trendMessages.push(message)
}
// Send a message to Slack.
sendSlackBot(SLACK_CANNEL, trendMessages.join('\n'))
trendMessages = []
}
}
/**
* function name : sendSlackBot.
* Overview : If an channel is specified, an slack message will be sent.
*/
function sendSlackBot(channel, message) {
if (!channel) return
// Send message to bot.
UrlFetchApp.fetch(SLACK_URL, {
method: 'post',
payload: {
token: SLACK_TOKEN,
channel: channel,
text: message
}
})
}
- 動作確認
- 実行メソッドを「notifySlackOfTrend」に変更後、実行ボタンを押下。
権限確認が来るので、許可する。
- Slackのチャンネルに通知が来ていることを確認する
リンクもついているので、クリックで情報をみることもできます。
GoogleAppsScriptトリガー設定
Slackに急上昇ワードのBot送信ができたので、定期的に実行するようにトリガーを設定します。
最後に
今回は、毎日お昼になると急上昇ワードが送信されるようなネタBotを作成しました。
仕事で煮詰まっている人や、話す話題に困っている人に見てもらえると、チーム生産性やコミュニケーションが向上するかも!?
あまり、真面目にGASを触っていなかったのですが、手軽に使えるので今後もいろいろ試してみたいと思います。
番外編 Teamsに送信する場合
GASスクリプト
const GOOGLE_TRENDS_URL = 'https://trends.google.com/trends/api/dailytrends?geo=JP'
/* Teams Incoming WebhookのURLを設定!!!! */
const TEAMS_WEBHOOK = 'https://outlook.office.com/webhook/xxxxx'
/**
* function name : notifyTeamsOfTrend.
* Overview : Notify Teams of trend information.
*/
function notifyTeamsOfTrend() {
// Get trend information from Google.
const response = UrlFetchApp.fetch(GOOGLE_TRENDS_URL, {
'method' : 'get',
});
const trends = JSON.parse(response.getContentText().replace(/^\)\]\}\'\,/g, ''))
if (trends.default.trendingSearchesDays.length === 0) return
// Create a Teams message from trend information.
const trendingSearches = trends.default.trendingSearchesDays[0].trendingSearches
let trendMessages = ['# Google急上昇ワード']
for (const trendingSearchesDay of trends.default.trendingSearchesDays) {
trendMessages.push('## ' + trendingSearchesDay. formattedDate)
let ranking = 1
for (const trendingSearche of trendingSearchesDay.trendingSearches) {
const message = ranking++ + '. [' + trendingSearche.title.query + '](https://trends.google.com' + trendingSearche.title.exploreLink + ') '
+ '_' + trendingSearche.formattedTraffic + '_'
+ (trendingSearche.relatedQueries.length ?
'\n\n - 関連ワード:' + trendingSearche.relatedQueries.map(relatedQuerie => {
return '[' + relatedQuerie.query + '](https://trends.google.com' + relatedQuerie.exploreLink + ')'
}).join(',')
+ '\n\n'
: '\n')
console.log(message)
trendMessages.push(message)
}
}
// Send a message to Teams.
sendTeamsBot({text:trendMessages.join('\n')})
}
/**
* function name : sendTeamsBot.
* Overview : Teams message will be sent.
*/
function sendTeamsBot(message) {
// Send message to bot.
UrlFetchApp.fetch(TEAMS_WEBHOOK, {
method: 'post',
payload: JSON.stringify(message)
})
}