Last active
March 2, 2020 08:10
-
-
Save keidarcy/2390ae7f1b96aa97b1937bf703fd9070 to your computer and use it in GitHub Desktop.
use cotohaapi to build a slackbot to analysis any sns message
This file contains hidden or 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
const axios = require('axios').default; | |
const SlackBot = require('slackbots'); | |
const URL = 'https://api.ce-cotoha.com/api/dev/nlp/v1/sentiment'; | |
const COTOHA_TOKEN = process.env.COTOHA_TOKEN; | |
const SLACK_TOKEN = process.env.SLACK_TOKEN; | |
const USER_ID = process.env.USER_ID; | |
axios.defaults.headers.common['Authorization'] = `Bearer ${COTOHA_TOKEN}`; | |
const bot = new SlackBot({ | |
token: SLACK_TOKEN, | |
name: '彼女分析ロボット' | |
}); | |
bot.on('start', () => console.log('彼女分析開始...')); | |
bot.on('message', message => { | |
if (Object.is(message.user, USER_ID) && Object.is(message.type, 'message')) { | |
(async function() { | |
try { | |
const robotMessage = await getData(message.text); | |
await send(robotMessage); | |
} catch (e) { | |
console.log('error in bot on :', e.message); | |
} | |
})(); | |
} | |
}); | |
const getData = async message => { | |
let json = {}; | |
if (Object.is(message, 'こんにちは')) { | |
return message; | |
} else if (Object.is(message, 'Bye')) { | |
return message; | |
} else { | |
try { | |
json = await axios.post(URL, { | |
sentence: message | |
}); | |
} catch (e) { | |
console.log('error in get data', e.message); | |
} | |
const form = json.data.result.emotional_phrase.map(pharse => pharse.form); | |
const emotion = json.data.result.emotional_phrase.map( | |
pharse => pharse.emotion | |
); | |
let emoji = ''; | |
switch (json.data.result.sentiment) { | |
case 'Positive': | |
emoji = '😘'; | |
suggestion = '積極的に進んでみましょう!'; | |
break; | |
case 'Negative': | |
emoji = '🙁'; | |
suggestion = 'もう諦めた方良いかもしれない:('; | |
break; | |
case 'Neutral': | |
emoji = '😐'; | |
suggestion = 'しばらく見てみましょう。'; | |
break; | |
} | |
return { | |
message: message, | |
status: json.data.status, | |
sentiment: json.data.result.sentiment, | |
form: form.join(' | '), | |
emotion: emotion.join(' | '), | |
// .replace(/P/g, '積極') | |
// .replace(/N/g, '消極'), | |
emoji: emoji, | |
suggestion: suggestion | |
}; | |
} | |
}; | |
const send = message => { | |
let reply = ''; | |
if (Object.is(message, 'こんにちは')) { | |
reply = '彼女のメッセージ分析してあげるよ。どうぞ😈'; | |
} else if (Object.is(message, 'Bye')) { | |
reply = 'Bye! 幸せにしてね🦾'; | |
} else { | |
reply = ` | |
\`\`\` | |
${message.message} | |
\`\`\` | |
キーワード : ${message.form} | |
感情種別 : ${message.emotion} | |
感情分析結果 : *${message.sentiment}* ${message.emoji} | |
アドバイス : ${message.suggestion} | |
`; | |
} | |
const params = { | |
icon_emoji: ':robot_face:' | |
}; | |
bot.postMessageToChannel('general', `${reply}`, params); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment