This repository has been archived by the owner on Mar 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
arxiv-bot.js
84 lines (70 loc) · 2.21 KB
/
arxiv-bot.js
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
var Botkit = require('botkit')
var fs = require('fs')
var format = require('util').format
var arxivApi = require('./arxiv-api')
// Ideally this could be done with a group around the ID in the URL, but I
// needed to get all unique links in a message, and this way was easier.
var ARXIV_ID = /\d{4}\.\d{4,5}/;
var ARXIV_LINK = /(?:https?:\/\/)?arxiv\.org\/(?:abs|pdf)\/(\d{4}\.\d{4,5})(?:v\d+)?(?:.pdf)?/g;
var controller = Botkit.slackbot({debug: false})
// Load Slack token from file (required)
if (!process.env.slack_token_path) {
console.log('Error: Specify slack_token_path in environment')
process.exit(1)
}
fs.readFile(process.env.slack_token_path, function (err, data) {
if (err) {
console.log('Error: Specify token in slack_token_path file')
process.exit(1)
}
data = String(data)
data = data.replace(/\s/g, '')
controller
.spawn({token: data})
.startRTM(function (err) {
if (err) {
throw new Error(err)
}
})
})
var formatArxivAsAttachment = function (arxivData, callback) {
var attachment = {
author_name: arxivData.authors.slice(0, 3).join(', '),
title : '[' + arxivData.id + '] ' + arxivData.title,
title_link : arxivData.url,
text : arxivData.summary.split(' ').slice(0, 40).join(' ') + ' ...',
fallback : arxivData.title
};
if (arxivData.authors.length > 3) {
attachment.author_name += ', and others';
}
callback(null, attachment);
}
var summarizeArxiv = function (arxivId, callback) {
arxivApi.fetchArxiv(arxivId, function (err, arxivData) {
if (err) {
callback(err)
}
else {
formatArxivAsAttachment(arxivData, callback)
}
})
}
// Listen for any messages containing an ArXiv link
controller.hears(
[ARXIV_LINK],
['ambient', 'mention', 'direct mention', 'direct message'],
function (bot, message) {
message.match.forEach(function (link) {
var arxivId = link.match(ARXIV_ID)[0]
summarizeArxiv(arxivId, function (err, attachment) {
if (err) {
console.log(format('ERROR: using "%s" from URL "%s": %s', arxivId, link, err))
}
else {
bot.reply(message, {attachments: [attachment]})
}
})
})
}
)