Last active
May 17, 2020 04:17
-
-
Save ya-ma-cho/1fe13793da19f259e39a68295d73f720 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SLACK_API_TOKEN = PropertiesService.getScriptProperties().getProperty('SLACK_API_TOKEN'); | |
var OUTGOING_TOKEN = PropertiesService.getScriptProperties().getProperty('OUTGOING_TOKEN'); | |
var SPREADSHEET_ID = PropertiesService.getScriptProperties().getProperty('SPREADSHEET_ID'); | |
var RECORD_SHEET_NAME = 'record'; | |
var MEMBER_SHEET_NAME = 'member'; | |
function doPost(e) { | |
if(OUTGOING_TOKEN == e.parameter.token && e.parameter.trigger_word == 'iine'){ | |
var slackApp = SlackApp.create(SLACK_API_TOKEN); | |
var ss = SpreadsheetApp.openById(SPREADSHEET_ID); | |
if(!ss.getSheetByName(MEMBER_SHEET_NAME)) slackApp.chatPostMessage(e.parameter.channel_id, 'イノベータァーーー・・・・・ジャパァァァーーーーン!!\r\nhttp://tech.innovator.jp.net/entry/iine-slack-bot', {username : 'いいね太郎',icon_emoji : ':+1:' }); | |
var record_sheet = getRecordSheet(ss); | |
var member_sheet = getMemberSheet(ss); | |
// ランキング表示 | |
if(e.parameter.text.match(/^iine ranking$/)){ | |
slackApp.chatPostMessage(e.parameter.channel_id, getRanking(member_sheet), {username : 'いいね太郎',icon_emoji : ':+1:' }); | |
return; | |
} | |
var iine_params = e.parameter.text.match(/^iine <\@(.*)>(.*)/); | |
if(iine_params == null) return; | |
var user_info = slackApp.usersInfo(iine_params[1]); | |
if(!user_info['ok']) return; | |
var iine_target = user_info['user']['name']; | |
// 自分自身へのいいね禁止 | |
if(iine_target == e.parameter.user_name ) { | |
slackApp.chatPostMessage(e.parameter.channel_id, '自分に いいね はできないよ?', {username : 'いいね太郎',icon_emoji : ':+1:' }); | |
return; | |
} | |
var iine_message = iine_params[2].replace(/(^\s+)|(\s+$)/g, ""); | |
var last_row = member_sheet.getLastRow(); | |
var tmp_user = ''; | |
for( i = 2 ; i <= last_row ; i++ ){ | |
tmp_user = member_sheet.getRange('A' + i ).getValue(); | |
if(iine_target == tmp_user){ | |
record_sheet.appendRow([new Date(),e.parameter.user_name,iine_target,iine_message]); | |
slackApp.chatPostMessage(e.parameter.channel_id, iine_target + 'に 1 いいね付与 (合計:' + member_sheet.getRange('B' + i).getValue() + ' いいね)', {username : 'いいね太郎',icon_emoji : ':+1:' }); | |
break; | |
} | |
} | |
} | |
} | |
// 履歴リストのシートを取得 | |
function getRecordSheet(ss){ | |
var sheet = ss.getSheetByName(RECORD_SHEET_NAME); | |
// 履歴リストのシートがない場合は生成 | |
if(!sheet) { | |
sheet = ss.insertSheet(RECORD_SHEET_NAME); | |
sheet.setColumnWidth(1, 130); | |
sheet.setColumnWidth(4, 500); | |
sheet.getRange('A1:D1').setBackground('#036'); | |
sheet.getRange('A1:D1').setFontColor('#FFF'); | |
sheet.getRange('A1').setValue('いつ'); | |
sheet.getRange('B1').setValue('誰が'); | |
sheet.getRange('C1').setValue('誰に'); | |
sheet.getRange('D1').setValue('メッセージ'); | |
sheet.getRange('D1').setWrap(true); | |
sheet.getRange('A:D').setBorder(true, true, true, true, true, true); | |
} | |
return sheet; | |
} | |
// メンバーリストのシートを取得 | |
function getMemberSheet(ss){ | |
var sheet = ss.getSheetByName(MEMBER_SHEET_NAME); | |
// メンバーリストのシートがない場合は生成 | |
if(!sheet) { | |
sheet = ss.insertSheet(MEMBER_SHEET_NAME); | |
resetMembers(sheet); | |
} | |
return sheet; | |
} | |
// メンバーリストの設定 | |
function resetMembers(sheet){ | |
sheet.getRange('A:C').clear(); | |
sheet.getRange('A1:C1').setBackground('#036'); | |
sheet.getRange('A1:C1').setFontColor('#FFF'); | |
sheet.getRange('A1').setValue('メンバー'); | |
sheet.getRange('B1').setValue('いいねされた数'); | |
sheet.getRange('C1').setValue('いいねした数'); | |
sheet.getRange('A:C').setBorder(true, true, true, true, true, true); | |
var userList = SlackApp.create(SLACK_API_TOKEN).usersList(); | |
var user; | |
var line = 2; | |
for(var i = 0;i<userList.members.length;i++) { | |
user = userList.members[i]; | |
if(!user.deleted) { | |
sheet.getRange(line, 1).setValue(user.name); | |
sheet.getRange(line, 2).setValue('=COUNTIF(' + RECORD_SHEET_NAME + '!C:C,A' + line + ')'); | |
sheet.getRange(line, 3).setValue('=COUNTIF(' + RECORD_SHEET_NAME + '!B:B,A' + line + ')'); | |
line += 1; | |
} | |
} | |
} | |
// ランキングの取得 | |
function getRanking(sheet){ | |
var ranking_text = ''; | |
var range = sheet.getRange("A:C"); | |
range.sort([{column: 2, ascending: false},{column: 3, ascending: false}, {column: 1, ascending: true}]); | |
var user = ''; | |
var iine = ''; | |
var tmp_iine = ''; | |
var disp_rank = ''; | |
var last_row = sheet.getLastRow(); | |
for( i = 2 ; i <= last_row ; i++ ){ | |
user = sheet.getRange('A' + i ).getValue(); | |
iine = sheet.getRange('B' + i ).getValue(); | |
if (iine == 0) return ranking_text; | |
if(iine != tmp_iine){ | |
disp_rank = i - 1; | |
} | |
ranking_text = ranking_text + disp_rank + '位 ' + user + ' (' + iine + 'いいね)\r\n'; | |
tmp_iine = iine; | |
} | |
return ranking_text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment