|
| 1 | +// Description: |
| 2 | +// Text reciever |
| 3 | +// |
| 4 | +// Configuration: |
| 5 | +// HUBOT_TEXT_CHANNEL - channel to send messages in |
| 6 | +// HUBOT_TEXT_ACCOUNT - Account SID for verification |
| 7 | +// |
| 8 | +// Commands: |
| 9 | +// hubot text add <number> <name> - add a number to the list (make sure to add the +) |
| 10 | +// hubot text numberof <name> - gets the number of the name |
| 11 | +// hubot text whois <number> - gets the name of the person with that number |
| 12 | +// hubot text remove <number> |
| 13 | +// |
| 14 | +// Author: |
| 15 | +// Detry322 |
| 16 | + |
| 17 | +const config = require('hubot-conf'); |
| 18 | + |
| 19 | +const BRAIN_LOCATION = 'text.numbers'; |
| 20 | + |
| 21 | +module.exports = (robot) => { |
| 22 | + const conf = config('text', robot); |
| 23 | + |
| 24 | + const addNumber = (number, name) => { |
| 25 | + const mapping = robot.brain.get(BRAIN_LOCATION) || {}; |
| 26 | + mapping[number] = name; |
| 27 | + robot.brain.set(BRAIN_LOCATION, mapping); |
| 28 | + }; |
| 29 | + |
| 30 | + const removeNumber = (number) => { |
| 31 | + const mapping = robot.brain.get(BRAIN_LOCATION) || {}; |
| 32 | + delete mapping[number]; |
| 33 | + robot.brain.set(BRAIN_LOCATION, mapping); |
| 34 | + }; |
| 35 | + |
| 36 | + const getName = (number) => { |
| 37 | + const mapping = robot.brain.get(BRAIN_LOCATION) || {}; |
| 38 | + return mapping[number]; |
| 39 | + }; |
| 40 | + |
| 41 | + const getNumber = (name) => { |
| 42 | + const mapping = robot.brain.get(BRAIN_LOCATION) || {}; |
| 43 | + return Object.keys(mapping).find((key) => mapping[key] === name); |
| 44 | + }; |
| 45 | + |
| 46 | + robot.router.post('/text/receive', (req, res) => { |
| 47 | + res.header('Content-Type', 'text/xml').send('<Response></Response>'); |
| 48 | + let number = req.body.From; |
| 49 | + let message = req.body.Body; |
| 50 | + let decorator = 'Twilio'; |
| 51 | + if (req.body.AccountSid === conf('account')) { |
| 52 | + const matches = message.match(/(\+?[0-9]+) - (.+)$/); |
| 53 | + if (matches) { |
| 54 | + [, number, message] = matches; |
| 55 | + decorator = 'Google Voice'; |
| 56 | + } |
| 57 | + const name = getName(number) ? getName(number) : number; |
| 58 | + robot.messageRoom( |
| 59 | + conf('channel'), |
| 60 | + `[${decorator}] Text from ${name}: ${message}`, |
| 61 | + ); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + robot.respond(/text add (\+?[0-9]+) (.+)$/i, (res) => { |
| 66 | + const number = res.match[1]; |
| 67 | + const name = res.match[2]; |
| 68 | + addNumber(number, name); |
| 69 | + res.send(`Added: \`${number}\` for \`${name}\``); |
| 70 | + }); |
| 71 | + |
| 72 | + robot.respond(/text numberof (.+)$/i, (res) => { |
| 73 | + const name = res.match[1]; |
| 74 | + const number = getNumber(name); |
| 75 | + if (number) { |
| 76 | + return res.send(`\`${name}\` is \`${number}\``); |
| 77 | + } |
| 78 | + return res.send(`Could not find number for \`${name}\``); |
| 79 | + }); |
| 80 | + |
| 81 | + robot.respond(/text whois (\+?[0-9]+)$/i, (res) => { |
| 82 | + const number = res.match[1]; |
| 83 | + const name = getName(number); |
| 84 | + if (name) { |
| 85 | + return res.send(`\`${number}\` is \`${name}\``); |
| 86 | + } |
| 87 | + return res.send(`Could not find name for \`${number}\``); |
| 88 | + }); |
| 89 | + |
| 90 | + robot.respond(/text remove (\+?[0-9]+)$/i, (res) => { |
| 91 | + const number = res.match[1]; |
| 92 | + removeNumber(number); |
| 93 | + res.send(`Removed: \`${number}\``); |
| 94 | + }); |
| 95 | +}; |
0 commit comments