summaryrefslogtreecommitdiff
path: root/spawn.js
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2018-05-01 12:11:46 +0200
committerJules Laplace <julescarbon@gmail.com>2018-05-01 12:11:46 +0200
commit98affbe62185cf425a629ae68528f5381f3a864a (patch)
tree70b73d201e9915f59485e0cfdedc23108b24e320 /spawn.js
parent02c27ad8dcc689eda3b3e68d260cb23e9bf9a3cf (diff)
boilerplate for a slack bot
Diffstat (limited to 'spawn.js')
-rw-r--r--spawn.js173
1 files changed, 173 insertions, 0 deletions
diff --git a/spawn.js b/spawn.js
new file mode 100644
index 0000000..13ee81b
--- /dev/null
+++ b/spawn.js
@@ -0,0 +1,173 @@
+require('dotenv').load();
+
+const { RTMClient, WebClient } = require('@slack/client');
+
+// An access token (from your Slack app or custom integration - usually xoxb)
+const token = process.env.SLACK_CLI_TOKEN;
+
+class SpawnClient {
+ constructor(){
+ console.log('signing in', token)
+ this.web = new WebClient(token);
+ this.rtm = new RTMClient(token)
+ this.rtm.on('connected', () => console.log('connected'))
+ this.rtm.on('authenticated', () => console.log('authenticated'))
+ this.rtm.start();
+
+ this.listen()
+ this.list()
+ }
+ listen(){
+ console.log('listening')
+ this.rtm.on('message', (message) => {
+ console.log(message)
+ if ( (message.subtype && message.subtype === 'bot_message') ||
+ (!message.subtype && message.user === this.rtm.activeUserId) ) {
+ return;
+ }
+ switch (message.subtype) {
+ case 'file_share':
+ this.processFile(message.file)
+ break
+ case 'file_comment':
+ this.processFileCommand(message.file, message.comment)
+ break
+ // case 'file_mention':
+ // break
+ default:
+ console.log(`(channel:${message.channel}) ${message.user} says: ${message.text}`);
+ break
+ }
+ })
+ }
+ list(){
+ console.log('listing groups..')
+ this.web.groups.list().then((res) => {
+ console.log("____________\n")
+ console.log(res)
+
+ const group = res.groups[0]
+
+ if (group) {
+ this.group = group
+ console.log('we are in', group)
+ } else {
+ console.log('This bot does not belong to any private group, invite it to at least one and try again');
+ }
+ });
+ }
+
+ postMessageToGroup(msg){
+ if (! this.group) {
+ console.log('not in a group!')
+ return
+ }
+ rtm.sendMessage(msg, this.group.id)
+ .then((msg) => console.log(`ts:${msg.ts} <${group.name}> ${msg}`))
+ .catch(console.error);
+ }
+
+ processFile(){
+ console.log(message.text)
+ console.log(`download: ${message.file.url_private_download}`)
+ }
+
+ processFileCommand(file, comment){
+ const msg = comment.comment
+ console.log(`comment on file: ${msg}`)
+ }
+}
+
+const client = new SpawnClient ()
+
+// This argument can be a channel ID, a DM ID, a MPDM ID, or a group ID
+// const conversationId = 'C1232456';
+
+// The RTM client can send simple string messages
+// rtm.sendMessage('Hello there', conversationId)
+// .then((res) => {
+// // `res` contains information about the posted message
+// console.log('Message sent: ', res.ts);
+// })
+// .catch(console.error);
+
+
+
+// {
+// "type": "message",
+// "subtype": "file_share",
+// "ts": "1358877455.000010",
+// "text": "<@cal> uploaded a file: <https:...7.png|7.png>",
+// "file": {...},
+// "user": "U2147483697",
+// "upload": true
+// }
+
+// {
+// "type": "message",
+// "subtype": "file_comment",
+// "ts": "1361482916.000003",
+// "text": "<@cal> commented on a file: ...",
+// "file": {},
+// "comment": {}
+// }
+
+// {
+// "id" : "F2147483862",
+// "created" : 1356032811,
+// "timestamp" : 1356032811,
+// "name" : "file.htm",
+// "title" : "My HTML file",
+// "mimetype" : "text\/plain",
+// "filetype" : "text",
+// "pretty_type": "Text",
+// "user" : "U2147483697",
+// "mode" : "hosted",
+// "editable" : true,
+// "is_external": false,
+// "external_type": "",
+// "username": "",
+// "size" : 12345,
+// "url_private": "https:\/\/slack.com\/files-pri\/T024BE7LD-F024BERPE\/1.png",
+// "url_private_download": "https:\/\/slack.com\/files-pri\/T024BE7LD-F024BERPE\/download\/1.png",
+// "thumb_64": "https:\/\/slack-files.com\/files-tmb\/T024BE7LD-F024BERPE-c66246\/1_64.png",
+// "thumb_80": "https:\/\/slack-files.com\/files-tmb\/T024BE7LD-F024BERPE-c66246\/1_80.png",
+// "thumb_360": "https:\/\/slack-files.com\/files-tmb\/T024BE7LD-F024BERPE-c66246\/1_360.png",
+// "thumb_360_gif": "https:\/\/slack-files.com\/files-tmb\/T024BE7LD-F024BERPE-c66246\/1_360.gif",
+// "thumb_360_w": 100,
+// "thumb_360_h": 100,
+// "thumb_480": "https:\/\/slack-files.com\/files-tmb\/T024BE7LD-F024BERPE-c66246\/1_480.png",
+// "thumb_480_w": 480,
+// "thumb_480_h": 480,
+// "thumb_160": "https:\/\/slack-files.com\/files-tmb\/T024BE7LD-F024BERPE-c66246\/1_160.png",
+// "permalink": "https:\/\/tinyspeck.slack.com\/files\/cal\/F024BERPE\/1.png",
+// "permalink_public" : "https:\/\/tinyspeck.slack.com\/T024BE7LD-F024BERPE-3f9216b62c",
+// "edit_link": "https:\/\/tinyspeck.slack.com\/files\/cal\/F024BERPE\/1.png/edit",
+// "preview": "&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;meta charset='utf-8'&gt;",
+// "preview_highlight": "&lt;div class=\"sssh-code\"&gt;&lt;div class=\"sssh-line\"&gt;&lt;pre&gt;&lt;!DOCTYPE html...",
+// "lines": 123,
+// "lines_more": 118,
+// "is_public": true,
+// "public_url_shared": false,
+// "display_as_bot" : false,
+// "channels": ["C024BE7LT", ...],
+// "groups": ["G12345", ...],
+// "ims": ["D12345", ...],
+// "initial_comment": {...},
+// "num_stars": 7,
+// "is_starred": true,
+// "pinned_to": ["C024BE7LT", ...],
+// "reactions": [
+// {
+// "name": "astonished",
+// "count": 3,
+// "users": [ "U1", "U2", "U3" ]
+// },
+// {
+// "name": "facepalm",
+// "count": 1034,
+// "users": [ "U1", "U2", "U3", "U4", "U5" ]
+// }
+// ],
+// "comments_count": 1
+// } \ No newline at end of file