Created
June 29, 2018 17:04
-
-
Save tadaken3/f568f8218b410eb32ae5a868f14fda19 to your computer and use it in GitHub Desktop.
slackbot_codic
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
/* webhook でメッセージを拾う */ | |
function doPost(e) { | |
/* パラメーターを取得し、トークンが不一致なら処理終了 */ | |
var token = e.parameter.token; | |
var properties = PropertiesService.getScriptProperties(); | |
if (token != properties.getProperty('SLACK_REPLY_TOKEN')) { | |
return; | |
} | |
var text = e.parameter.text; | |
var keywords = text.substring(12); // メンション部分を削除 | |
variableConstantNamingTool(keywords); | |
//sendMessageFromBot(keywords); | |
} | |
/* codic API を使ってキャメルケースとアッパーケース両方の変数名を取得 | |
https://api.codic.jp/v1/engine/translate.json?&text=こんにちわ世界&casing=camel | |
これを投げると「こんにちわ世界」のキャメルケースとが返ってくる*/ | |
function variableConstantNamingTool(keywords) { | |
var codicUrl = 'https://api.codic.jp/v1/engine/translate.json?'; | |
var properties = PropertiesService.getScriptProperties(); | |
var casingArr = [[':dromedary_camel:','camel'], [':snake:', 'upper underscore']]; | |
var message = 'こんな変数名はいかがですか?'; | |
for (var i = 0; i < casingArr.length; i++) { | |
var fetchUrl = codicUrl + '&text=' + keywords +'&casing=' + casingArr[i][1]; | |
var fetchOpt = {'headers' : | |
{'Authorization' : 'Bearer ' + properties.getProperty('CODIC_TOKEN')} | |
}; | |
var response = UrlFetchApp.fetch(fetchUrl, fetchOpt); | |
var results = JSON.parse(response.getContentText()); | |
var result = results[0].translated_text; // 必要な結果だけを抽出 | |
message += '\n' + casingArr[i][0] + ' ' + result; | |
} | |
Logger.log(message) | |
sendMessageFromBot(message); | |
} | |
function sendMessageFromBot(message) { | |
var properties = PropertiesService.getScriptProperties(); | |
var slackApp = SlackApp.create(properties.getProperty('SLACK_TOKEN')); // bot のトークン | |
var channelId = "CBC4L8Q9G"; // 投稿したいチャンネルの ID | |
var options = {as_user: true}; | |
slackApp.postMessage(channelId, message, options); | |
} | |
function testvariableConstantNamingToot(){ | |
variableConstantNamingTool('こんにちは'); | |
} | |
function testSendMessageFromBot(){ | |
sendMessageFromBot('test') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment