diff --git a/README.md b/README.md index ed80534..3a5d78f 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,10 @@ heroku login heroku create servant-botkit heroku config:set SLACK_TEAM=[your team name] heroku config:set TOKEN=[your token] +heroku config:set TRELLO_KEY=[your token] +heroku config:set TRELLO_TOKEN=[your token] +heroku config:set TRELLO_UI_NOTE=[your token] +heroku config:set TRELLO_LIST_NEW_ID=[your token] ``` ### heroku config diff --git a/index.js b/index.js index 8da4fa0..580d93d 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ const Botkit = require('botkit'); +const request = require('request') if (!process.env.TOKEN) { console.log('Error: Specify TOKEN in environment'); @@ -42,14 +43,72 @@ controller.hears('', hearing_event_all, function(bot,message) { event_ts = event_ts.replace('.','') var url_parameter = `?thread_ts=${message.thread_ts}&cid=${channel_id}` var post_link = `https://${slack_team}.slack.com/archives/${channel_id}/p${event_ts}` - var channel_from_id + var URL = `https://slack.com/api/channels.info?token=${process.env.TOKEN}&channel=${channel_id}` + request(URL, (error, response, body) => { + if (!error && response.statusCode == 200) { + let channel_info = JSON.parse(body); + // console.log(channel_info); + var channel_name = channel_info.channel.name; + + if(channel_name.match('^times_.+')) { + repost_to('#timeline', bot, post_link, message); + } + + if (channel_name.match('^日報_(.*)$') && channel_name.match('_').length === 1/* && username !== 'slackbot'*/) { + repost_to('#日報_all', bot, post_link, message); + } + + if (channel_name.match('^日報_(.*)_.+')) { + matcher = channel_name.match('^日報_(.*)_.+'); + repost_to(`#日報_${matcher[1]}`, bot, post_link, message); + repost_to('#日報_all', bot, post_link, message); + } + + const key = process.env.TRELLO_KEY; + const token=process.env.TRELLO_TOKEN; + const ui_note=process.env.TRELLO_UI_NOTE; + const list_new_id=process.env.TRELLO_LIST_NEW_ID; + const bot_room = 'ui_notes' + const key_word_matcher = '^title:.*' + const title_matcher = '^title:([^\n]*)' + var msg = message["text"]; + + + if (channel_name.match(bot_room)){ + if(msg.match(key_word_matcher)){ + if(message["files"]==null){ + var img_url = "" + }else{ + var img_url = message["files"][0]["url_private"]; + }; + var title = encodeURIComponent(msg.match(title_matcher)[1]); + var desc = encodeURIComponent(msg+'\n'+img_url); + var url = `https://trello.com/1/cards?key=${key}&token=${token}&idList=${list_new_id}&name=${title}&desc=${desc}`; + var webclient = require("request"); + webclient.post({ + url: url, + headers: { + "content-type": "application/json" + } + }, function (error, response, body){}); + + bot.reply(message,"uiチームのタスクに登録されました。随時取り掛かります。") + + }; + }; + } + }); +}); + + +function repost_to(channel, bot, post_link, message) { + console.log(message); bot.api.chat.postMessage({ text: `${post_link}`, - channel: '#random', - as_user: false, - username: bot.identity.name, + channel: channel, + as_user: true, unfurl_links: true }, function(err, message){ if(err) { console.log("err: ", err); return; } }); -}); +}