|
1 |
| -const request = require('request') |
2 | 1 | const winston = require('winston')
|
| 2 | +const fetch = require('node-fetch') |
| 3 | +const { URLSearchParams } = require('url') |
| 4 | +const match = require('@menadevs/objectron') |
| 5 | +const { pre } = require('../utils.js') |
| 6 | +const secret = require('../secret.json') |
3 | 7 |
|
4 |
| -const Plugin = require('../utils.js').Plugin |
| 8 | +const verbose = ` |
| 9 | +How to use this pluginL |
5 | 10 |
|
6 |
| -const META = { |
7 |
| - name: 'sentiment', |
8 |
| - short: 'provides a sentiment analysis on the last 10 messages of a user', |
9 |
| - examples: [ |
10 |
| - 'analyse jordan' |
11 |
| - ] |
12 |
| -} |
| 11 | + analyse jordan |
| 12 | +` |
13 | 13 |
|
14 |
| -function findUser (bot, name) { |
15 |
| - return new Promise((resolve, reject) => { |
16 |
| - const members = bot.users.filter(m => m.name === name) |
| 14 | +/** |
| 15 | + * find the user based on the passed name and return the first one found |
| 16 | + * returns undefined if no user was found |
| 17 | + * @param {*} users |
| 18 | + * @param {*} name |
| 19 | + */ |
| 20 | +async function findUser (users, name) { |
| 21 | + const { members } = await users.list() |
17 | 22 |
|
18 |
| - if (members.length !== 1) { |
19 |
| - reject(`I don't know of a ${name}`) |
20 |
| - } else { |
21 |
| - resolve(members[0]) |
22 |
| - } |
23 |
| - }) |
| 23 | + return members.filter(member => member.profile.display_name.toLowerCase() === name.toLowerCase())[0] |
24 | 24 | }
|
25 | 25 |
|
26 |
| -function pickTarget (bot, channel) { |
27 |
| - return [...bot.channels, ...bot.groups].filter(c => c.id === channel)[0] |
28 |
| -} |
| 26 | +/** |
| 27 | + * find the channel or group in the list of workspace channels/groups |
| 28 | + * returns undefined if the channel/group didn't match |
| 29 | + * @param {*} conversations (could be a channel, a group or an im) |
| 30 | + * @param {*} channel |
| 31 | + */ |
| 32 | +async function getTargetChannel (conversations, channel) { |
| 33 | + const { channels } = await conversations.list() |
29 | 34 |
|
30 |
| -function loadRecentMessages (options, channel, user) { |
31 |
| - return new Promise((resolve, reject) => { |
32 |
| - const target = pickTarget(options.bot, channel) |
33 |
| - let source = options.web.channels |
34 |
| - |
35 |
| - if (target.is_group) { |
36 |
| - source = options.web.groups |
37 |
| - } |
38 |
| - |
39 |
| - source.history(channel, { count: 1000 }, (error, response) => { |
40 |
| - if (error) { |
41 |
| - reject(error) |
42 |
| - } else { |
43 |
| - let messages = response.messages.filter(m => m.user === user.id) |
| 35 | + return channels.filter(c => c.id === channel && c.is_archived === false)[0] |
| 36 | +} |
44 | 37 |
|
45 |
| - if (messages.length > options.config.plugins.sentiment.recent) { |
46 |
| - messages = messages.slice(1, options.config.plugins.sentiment.recent) |
47 |
| - } |
| 38 | +/** |
| 39 | + * get the user messages in the last 100 messages in the provided channel |
| 40 | + * @param {*} channel |
| 41 | + * @param {*} user |
| 42 | + */ |
| 43 | +async function getUserMessagesInChannel (conversations, channel, user) { |
| 44 | + const { messages } = await conversations.history({ channel: channel.id, limit: 1000 }) |
| 45 | + // fetching first 10 messages from the list of messages |
| 46 | + const lastTenMessagesByUser = messages.filter(message => message.user === user.id).slice(0, 10) |
48 | 47 |
|
49 |
| - if (messages.length === 0) { |
50 |
| - reject('User has not spoken recently') |
51 |
| - } else { |
52 |
| - const text = messages.map(m => m.text).join('\n') |
53 |
| - resolve(text) |
54 |
| - } |
55 |
| - } |
56 |
| - }) |
57 |
| - }) |
| 48 | + return lastTenMessagesByUser |
58 | 49 | }
|
59 | 50 |
|
60 |
| -function analyseSentiment (secret, messages) { |
61 |
| - return new Promise((resolve, reject) => { |
62 |
| - request.post({ |
63 |
| - url: 'http://api.datumbox.com/1.0/SentimentAnalysis.json', |
64 |
| - form: { |
65 |
| - api_key: secret.datumbox, |
66 |
| - text: messages |
67 |
| - }, |
68 |
| - headers: { |
69 |
| - 'User-Agent': 'request' |
70 |
| - } |
71 |
| - }, (error, response, body) => { |
72 |
| - if (error) { |
73 |
| - reject(error) |
74 |
| - } else { |
75 |
| - resolve(JSON.parse(body)) |
76 |
| - } |
77 |
| - }) |
78 |
| - }) |
| 51 | +/** |
| 52 | + * send the messages to the api and return response |
| 53 | + * @param {*} secret |
| 54 | + * @param {*} messages |
| 55 | + */ |
| 56 | +async function analyseSentiment (messages) { |
| 57 | + const params = new URLSearchParams() |
| 58 | + params.set('api_key', secret.datumbox) |
| 59 | + params.append('text', messages) |
| 60 | + const response = await fetch('http://api.datumbox.com/1.0/SentimentAnalysis.json', { method: 'POST', body: params }) |
| 61 | + const jsonResult = await response.json() |
| 62 | + return jsonResult |
79 | 63 | }
|
80 | 64 |
|
81 |
| -function analyse (options, message, target) { |
82 |
| - findUser(options.bot, target) |
83 |
| - .then(user => loadRecentMessages(options, message.channel, user)) |
84 |
| - .then(messages => analyseSentiment(options.secret, messages)) |
85 |
| - .then(sentiment => message.reply_thread(`${target} has recently been ${sentiment.output.result}`)) |
86 |
| - .catch(error => winston.error(`${META.name} - Error: ${error}`)) |
| 65 | +/** |
| 66 | + * analyse the user messages in a channel or group |
| 67 | + * @param {*} options |
| 68 | + * @param {*} message |
| 69 | + * @param {*} target |
| 70 | + */ |
| 71 | +async function analyse (options, message, target) { |
| 72 | + try { |
| 73 | + const user = await findUser(options.web.users, target) |
| 74 | + if (!user) { |
| 75 | + message.reply_thread(`I don't know of a ${target}. Please validate you entered the correct person's name.`) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + const targetChannel = await getTargetChannel(options.web.conversations, message.channel) |
| 80 | + if (!targetChannel) { |
| 81 | + message.reply_thread('Are you in a channel or group? sentiment doesn\'t work in a direct message.') |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + const messages = await getUserMessagesInChannel(options.web.conversations, targetChannel, user) |
| 86 | + if (messages.length !== 0) { |
| 87 | + const response = await analyseSentiment(messages.map(m => m.text).join('\n')) |
| 88 | + message.reply_thread(`${target} has recently been ${response.output.result}.`) |
| 89 | + } else { |
| 90 | + message.reply_thread(`User ${target} has not spoken recently.`) |
| 91 | + } |
| 92 | + } catch (error) { |
| 93 | + message.reply_thread(`Something went wrong! this has nothing to do with the sentiments of ${target}. Please check the logs.`) |
| 94 | + options.logger.error(`${module.exports.name} - something went wrong. Here's the error: ${pre(error)}`) |
| 95 | + } |
87 | 96 | }
|
88 | 97 |
|
89 |
| -function register (bot, rtm, web, config, secret) { |
90 |
| - const plugin = new Plugin({ bot, rtm, web, config, secret }) |
91 |
| - plugin.route(/^analyse (.+)/, analyse, {}) |
| 98 | +const events = { |
| 99 | + message: (options, message) => { |
| 100 | + match(message, { |
| 101 | + type: 'message', |
| 102 | + text: /^analyse (?<name>.+)/ |
| 103 | + }, result => analyse(options, message, result.groups.name)) |
| 104 | + } |
92 | 105 | }
|
93 | 106 |
|
94 | 107 | module.exports = {
|
95 |
| - register, |
96 |
| - META |
| 108 | + name: 'sentiment', |
| 109 | + help: 'provides a sentiment analysis on the last 10 messages of a user', |
| 110 | + verbose, |
| 111 | + events, |
| 112 | + findUser, |
| 113 | + getTargetChannel, |
| 114 | + getUserMessagesInChannel, |
| 115 | + analyseSentiment, |
| 116 | + analyse |
97 | 117 | }
|
| 118 | + |
0 commit comments