summaryrefslogtreecommitdiff
path: root/spawn.js
blob: 13ee81b925704695283ae849567174d3663015af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
// }