2017年7月8日土曜日

田胡研SlackBot その1

# はじめに この記事は田胡研初夏のAdventCalendar 8日目の記事です. 前回から間が微妙に空いてしまいましたが今日は前回のluuvaくんのvoiceroid apiに被せてそのapiを利用して田胡研SlackBotを作った話を書こうと思います. # 田胡研Slack 田胡研ではSlackをコミュニケーションツールとして使っています. 昔はIRCを使っていたらしいですがSlackに一本化しました. 基本コミュ障の集団なのでよくSlackで喋ったりしています. Slackはその便利さに加えてエンジニアとしてはいろいろなBotを作ることができるのが楽しいところです. 田胡研SlackにもいろいろBotが作られていて * Logger * ラーメン屋決定機 * 大学スクールバス時刻案内 * Voiceroid音声を研究室内で流せるBot などあります. スクールバス時刻案内BotはまたこのAdventCalendar期間中に書くとして,今日はVoiceroid音声をSlackから流せるBotについて書きます. # voiceroid-slackbot ## 利用手順 はじめにこのBotの利用ステップと動作を示します. 1. Slack上でBotにメンション付きで喋ってほしいテキストを送信する 1. SlackBotがVoiceroidAPIをcall 1. VoiceroidAPIから音声データがバイナリで飛んでくるので保存 1. 音声データを再生する (Botは研究室内のサーバで動いているのでそのスピーカから音声が流れる) という感じです. ## ソース slackbotはnodeで動かすことにしました. エコシステムのおかげでslackbotはほんと一瞬で作れてしまいます. ```js const RtmClient = require('@slack/client').RtmClient const WebClient = require('@slack/client').WebClient const RTM_EVENTS = require('@slack/client').RTM_EVENTS const exec = require('child_process').exec const axios = require('axios') const fs = require('fs') const token = process.env.API_TOKEN || 'DEBUG-TOKEN-HERE' const rtm = new RtmClient(token, { logLevel: 'error', autoReconnect: true, autoMark: true }) rtm.start() const web = new WebClient(token) rtm.on(RTM_EVENTS.MESSAGE, function handleRtmMessage(message) { console.log(message); let text = message.text // DM check if (text.indexOf('<@U5QCWJ64B>') === -1) { console.log('this is not direct message') return } text = text.replace('<@U5QCWJ64B>','') // play special sound if(text.indexOf('あけて') > -1){ randomNumber = Math.random(); let file_name = 'open_1.wav' if(randomNumber < 0.1){ file_name = 'open-gd.wav' } play(file_name) return } // play voiceloid sound axios.get('http://192.168.133.40:60334/voice', { params: { msg: text } }).then((res) => { return res.data }).then((data) => { // decode and save let sound = Buffer.from(data.sound, 'base64') let filename = `response.wav` fs.writeFile(filename, sound, (err) => { play(filename) }) }) }) function play (filename) { exec(`aplay -D plughw:1,0 ${filename}`, (err, stdout, stderr) => { if (err) { console.log(err) } console.log(stdout) }) } ``` ただ書き連ねたコードですが, あけて(研究室のカードロックを解除して!)というSlackのメッセージだけはVoiceroidを使わずたまにアニメ音声が流れるようになっています. こういうのは他にもいくつか追加できたらと思います. ## 使ってみて感想 * 深夜とかにスピーカーから音声を流せて中にいる人をおどかせる * ルイズコピペをどっからか流してテロが起こせる * カードキーを忘れて開けてほしいケースだとスマホも忘れていて結局Slackメッセージ経由で開けてもらえない * 研究室の中にいる人に確実に気づいてほしいメッセージを飛ばすのに便利 面白いものを思いついたらまたどんどんSlackBotを作って遊んでみたいですね. 今回はここでおしまい.

4 件のコメント:

  1. This is brilliant, I also recommend using nursing writing services for research. More also, I recommend also nursing essay writers for the best quality and unlimited support writing and editing services for your dissertation, essay, thesis, and Nursing dissertation writing services as well as nursing thesis writing services

    返信削除
  2. Excellent site you’ve got here.. It’s hard to find high-quality writing like yours nowadays. I seriously appreciate individuals like you!
    룰렛사이트탑

    返信削除
  3. I am glad to discover this page. I have to thank you for the time I spent on this especially great reading !! I really liked each part and also bookmarked you for new information on your site. Feel free to visit my website; 바카라사이트

    返信削除
  4. I will really appreciate the writer's choice for choosing this excellent article appropriate to my matter. We offer safe and trusted online site for gamers. click here: 바카라사이트


    返信削除