Skip to content

Commit

Permalink
Merge pull request #5 from TUS-OSK/improve_slack
Browse files Browse the repository at this point in the history
Improve_slack
  • Loading branch information
ilim0t authored Mar 2, 2019
2 parents 34f1a73 + 9a952fb commit c8e3f77
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`で入れれば治る.
途中でErrorが出たらまあ頑張れ. 多分`package-lock.json`, `token.json`を消してみるか, opencv4nodejsに必要なcmake, gcc, python等のlibraryを`apt``brew`で入れれば治る.
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion libs/caputure.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
113 changes: 97 additions & 16 deletions slack.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: "部室ちゃん"
})
}

/**
Expand All @@ -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;
Expand All @@ -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: "部室ちゃん"
})
}
};

0 comments on commit c8e3f77

Please sign in to comment.