diff --git a/README.md b/README.md index 68964e0..2849d50 100644 --- a/README.md +++ b/README.md @@ -40,10 +40,10 @@ direnv allow ``` ## カメラを使えるようにする -Usageで書いたが,`npm install`で自動で設定されるが,注意が必要なので[opencv4nodejs](https://www.npmjs.com/package/opencv4nodejs#how-to-install)が使えれるよう事前に準備しておく. +Usageで書いたが,`npm install`で自動で設定されるが,注意が必要なので[opencv4nodejs](https://www.npmjs.com/package/opencv4nodejs#how-to-install)が使えれるよう事前に準備しておく. # Usage 1. `npm install` 2. `npm start` を実行. -途中でErrorが出たらまあ頑張れ. 多分`package-lock.json`, `token.json`を消してみるか, opencv4nodejsに必要なcmake, gcc, python等のlibraryを`apt`や`brew`で入れれば治る. \ No newline at end of file +途中でErrorが出たらまあ頑張れ. 多分`package-lock.json`, `token.json`を消してみるか, opencv4nodejsに必要なcmake, gcc, python等のlibraryを`apt`や`brew`で入れれば治る. diff --git a/app.js b/app.js index 88e3eca..172d2b8 100644 --- a/app.js +++ b/app.js @@ -8,7 +8,7 @@ const utils = require("./libs/utils"); const main = async () => { //写真撮影 const cap = new capture.Capture(0); - cap.setIntervalCapture(); + cap.setIntervalCapture(5 * 1000, 12); //環境設定 const {client_id, client_secret, slack_token} = process.env; diff --git a/libs/caputure.js b/libs/caputure.js index e9e9105..52ce12b 100644 --- a/libs/caputure.js +++ b/libs/caputure.js @@ -33,7 +33,7 @@ module.exports.Capture = class Capture { generateGif() { if (this.photoArray.length === 0) { - throw new Error(""); + throw new Error("まだまだ画像が撮れていません"); } const [height, width] = this.photoArray[0].sizes; diff --git a/slack.js b/slack.js index fce0149..f2e11db 100644 --- a/slack.js +++ b/slack.js @@ -20,7 +20,76 @@ module.exports.Slack = class Slack { start() { this.rtm.start().catch(console.error); this.rtm.on("ready", () => console.log("ready")); - this.rtm.on("message", message => this.reply(message)); + this.rtm.on("message", event => this.reply(event)); + this.rtm.on("reaction_added", async event => this.reactionReply(event)) + } + + static getBolck(text, url) { + return [ + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": text + } + }, + { + "type": "image", + "title": { + "type": "plain_text", + "text": "部室の様子", + "emoji": true + }, + "image_url": `${url}`, + "alt_text": "部室の様子" + }, + { + "type": "context", + "elements": [ + { + "type": "plain_text", + "text": "リアクションを付けることでアクションを起こせられます. \n:yarinaoshi:で再送, :wasureyou:で削除されます.", + "emoji": true + } + ] + } + ]; + } + + async reactionReply(event) { + const {reaction, item, item_user, user} = event; + if (item_user && item_user !== this.rtm.activeUserId) { + return; + } + const {type, channel, ts} = item; + + if (!["yarinaoshi", "wasureyou", "wastebasket", "recycle"].some(element => reaction === element)) { + return; + } + if (type !== "message") { + return; + } + await this.web.chat.delete({ + channel: channel, + ts: ts, + as_user: true + } + ); + if (!["wasureyou", "recycle"].some(element => reaction === element)) { + return; + } + + const replyText = await this.getReplyText(); + await this.web.chat.postMessage({ + channel: channel, + text: "再送|部室の様子", + blocks: Slack.getBolck(`:recycle:が付けられたため再送します\n${new Date().toLocaleString()}から直近一定時間の様子をgifアニメーションにしました.`, replyText), + icon_emoji: ":slack:", + // thread_ts: thread_ts || ts, + // reply_broadcast: true, + // as_user: true, + username: "部室ちゃん" + }) } /** @@ -29,8 +98,9 @@ module.exports.Slack = class Slack { async getReplyText() { } - reply(receiveMessage) { - const {user, text, channel, subtype, ts} = receiveMessage; + + async reply(event) { + const {user, text, channel, subtype, ts, thread_ts} = event; if (subtype) { // console.log(subtype); return; @@ -39,27 +109,38 @@ module.exports.Slack = class Slack { } else if (!text) { return; } else if (!text.match(new RegExp(`<@${this.rtm.activeUserId}>`))) { - return; + const response = await this.web.im.open({ + user: user, + include_locale: true + }); + const dmChannnel = response.channel.id; + if (dmChannnel !== channel) { + return; + } } if (text.match(/ip$/)) { const ips = utils.getLocalIps(); - return this.web.chat.postMessage({ + await this.web.chat.postMessage({ channel: channel, text: ips.map(ip => `address: ${ip}`).join("\n"), + // thread_ts: thread_ts || ts, + // reply_broadcast: true, as_user: true, - thread_ts: ts + // username: "部室ちゃん" }); + return; } - this.getReplyText() - .then(replyText => - this.web.chat.postMessage({ - channel: channel, - text: replyText, - as_user: true, - thread_ts: ts - }) - ) - // console.info(`Message sent: ${response.message.text}` + const replyText = await this.getReplyText(); + await this.web.chat.postMessage({ + channel: channel, + text: "部室の様子", + blocks: Slack.getBolck(`${new Date().toLocaleString()}から直近一定時間の様子をgifアニメーションにしました.`, replyText), + icon_emoji: ":slack:", + // thread_ts: thread_ts || ts, + // reply_broadcast: true, + // as_user: true, + username: "部室ちゃん" + }) } }; \ No newline at end of file