Comparing version 0.1.3 to 0.1.4
@@ -33,3 +33,5 @@ 'use strict'; | ||
this.app = express(); | ||
// this.app.use(bodyParser.json()); | ||
// doing this, because otherwise __verifyRe... doesn't have access | ||
// to the fbAppSecret | ||
this.app.use((req, res, next) => { | ||
@@ -39,3 +41,2 @@ req.fbAppSecret = this.credentials.fbAppSecret; | ||
}); | ||
// this.app.use(bodyParser.json()); | ||
this.app.use(bodyParser.json({ verify: this.__verifyRequestSignature })); | ||
@@ -42,0 +43,0 @@ this.app.use(bodyParser.urlencoded({ extended: true })); |
@@ -55,4 +55,3 @@ 'use strict'; | ||
const promise = new Promise((resolve) => { | ||
let update = null; | ||
update = { | ||
const update = { | ||
raw: rawUpdate, | ||
@@ -203,3 +202,2 @@ sender: { | ||
if (body.error) { | ||
console.log(body.error); | ||
this.emit('error', body.error); | ||
@@ -209,3 +207,2 @@ } | ||
.catch((err) => { | ||
console.log(err); | ||
this.emit('error', err); | ||
@@ -212,0 +209,0 @@ }); |
'use strict'; | ||
const EventEmitter = require('events'); | ||
const _ = require('lodash'); | ||
const BaseBot = require('./base_bot.js'); | ||
const Twit = require('twit'); | ||
const twitterBot = new EventEmitter(); | ||
class TwitterBot extends BaseBot { | ||
module.exports = twitterBot; | ||
constructor(settings) { | ||
// do proper credentials test here | ||
super(settings); | ||
const twit = new Twit(this.credentials); | ||
this.idStr = this.credentials.access_token.split('-')[0]; | ||
this.twit = twit; | ||
const twitterStream = twit.stream('user'); | ||
twitterStream.on('direct_message', (rawUpdate) => { | ||
// otherwise, Bot will try to answer self | ||
if (rawUpdate.direct_message.sender.id_str !== this.idStr) { | ||
const update = this.__formatUpdate(rawUpdate); | ||
this.__emitUpdate(update); | ||
} | ||
}); | ||
} | ||
__formatUpdate(rawUpdate) { | ||
const update = { | ||
raw: rawUpdate, | ||
sender: { | ||
id: rawUpdate.direct_message.sender.id_str, | ||
}, | ||
recipient: { | ||
id: this.idStr, | ||
}, | ||
timestamp: rawUpdate.created_at, | ||
message: { | ||
mid: rawUpdate.direct_message.id_str, | ||
seq: null, // twitter doesn't have such a concept. Can be copied with proper storage | ||
}, | ||
}; | ||
if (rawUpdate.direct_message.text !== undefined) { | ||
update.message.text = rawUpdate.direct_message.text; | ||
} | ||
return update; | ||
} | ||
sendMessage(message, session) { | ||
if (session) { | ||
message.recipient = { | ||
id: session.id, | ||
}; | ||
} | ||
// // optionalParams for twitter will basically be buttons params, which is | ||
// // text to append. | ||
// if (optionalParams) { | ||
// text = `${text}\n\n${optionalParams}` | ||
// } | ||
const messageData = {}; | ||
messageData.user_id = message.recipient.id; | ||
messageData.text = message.message.text; | ||
const params = { user_id: messageData.user_id, | ||
text: messageData.text, | ||
} | ||
this.twit.post('direct_messages/new', params, (err, data) => { | ||
if (err) { | ||
this.emit('error', err); | ||
} | ||
}); | ||
} | ||
} | ||
// twitterBot.formatButtonsParams = function(buttonNames) { | ||
// // buttonNames are in the row format for telegram, and twitter | ||
// // DMs don't support buttons, so we'll just append the text with the options | ||
// // written in plain text and new lines. | ||
// const buttonsParams = buttonNames.reduce((text, buttonName) => { | ||
// return `${text}\n${buttonName[0]}`; | ||
// }) | ||
// return buttonsParams; | ||
// } | ||
module.exports = TwitterBot; |
@@ -7,2 +7,3 @@ 'use strict'; | ||
const MessengerBot = require('./bot_types').MessengerBot; | ||
const TwitterBot = require('./bot_types').TwitterBot; | ||
@@ -49,3 +50,5 @@ /** | ||
botClass = MessengerBot; | ||
settings.messenger.sessionStore = this.sessionStore; | ||
} else if (settings.twitter !== undefined) { | ||
botType = 'twitter'; | ||
botClass = TwitterBot; | ||
} | ||
@@ -55,3 +58,3 @@ | ||
// this will set the store to undefined or the specified sessionStore | ||
settings.telegram.sessionStore = this.sessionStore; | ||
settings[botType].sessionStore = this.sessionStore; | ||
} | ||
@@ -65,3 +68,5 @@ this.createBot(botType, botClass, settings[botType]); | ||
this.bots.push(bot); | ||
this.app.use(`/${botType}`, bot.app); | ||
if (botType === 'telegram' || botType === 'messenger') { | ||
this.app.use(`/${botType}`, bot.app); | ||
} | ||
bot.on('update', (update) => { | ||
@@ -68,0 +73,0 @@ this.emit('update', bot, update); |
{ | ||
"name": "botmaster", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Framework allowing developers to write bots that are agnostic with respect to the channel used by their users (messenger, telegram etc...)", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -7,22 +7,14 @@ 'use strict' | ||
const request = require('request-promise'); | ||
const req = require('request'); | ||
require('chai').should(); | ||
const _ = require('lodash'); | ||
const TelegramBot = require('../../lib').botTypes.TelegramBot; | ||
const MemoryStore = require('../../lib/storage/memory_store'); | ||
const config = require('../config.js') | ||
const TOKEN = process.env.TELEGRAM_TEST_TOKEN; | ||
if (TOKEN === undefined) { | ||
throw new Error('Telegram Bot token must be defined. See Readme.md in ./tests'); | ||
} | ||
const USERID = process.env.TELEGRAM_TEST_USER_ID; | ||
if (USERID == undefined) { | ||
throw new Error('Telegram test user must be defined. See Readme.md in ./tests'); | ||
} | ||
const credentials = config.telegramCredentials; | ||
const userId = config.telegramUserId; | ||
describe('Telegram Bot', function() { | ||
const telegramSettings = { | ||
credentials: { | ||
authToken: TOKEN | ||
}, | ||
credentials, | ||
webhookEndpoint: '/telegram/webhook' | ||
@@ -33,5 +25,5 @@ }; | ||
message_id: 1, | ||
from: {id: USERID, first_name: 'Biggie', last_name: 'Smalls'}, | ||
from: {id: userId, first_name: 'Biggie', last_name: 'Smalls'}, | ||
chat: { | ||
id: USERID, | ||
id: userId, | ||
first_name: 'Biggie', | ||
@@ -173,3 +165,3 @@ last_name: 'Smalls', | ||
'recipient': { | ||
'id': TOKEN | ||
'id': credentials.authToken | ||
}, | ||
@@ -176,0 +168,0 @@ 'timestamp': rawUpdate.message.date * 1000, |
@@ -1,2 +0,3 @@ | ||
In order to run the tests, please do the following | ||
In order to run the tests, please create a config.js file in this folder | ||
that looks like this: | ||
--- | ||
@@ -6,19 +7,39 @@ | ||
```bash | ||
export TELEGRAM_TEST_TOKEN=<YOUR_TELEGRAM_BOT_TOKEN> | ||
# User Id which you want to send the messages. | ||
export TELEGRAM_TEST_USER_ID=<YOUR_TELEGRAM_USER_ID> | ||
``` | ||
```js | ||
# For Messenger | ||
const config = { | ||
```bash | ||
export MESSENGER_VERIFY_TOKEN=<YOUR_MESSENGER_VERIFY_TOKEN> | ||
export MESSENGER_PAGE_TOKEN=<YOUR_MESSENGER_PAGE_TOKEN> | ||
export FACEBOOK_APP_SECRET=<YOUR_FACEBOOK_APP_SECRET> | ||
``` | ||
telegramCredentials: { | ||
authToken: 'YOUR_OWN_INFO_HERE, | ||
}, | ||
# For Twitter | ||
telegramUserId: 'YOUR_OWN_INFO_HERE', | ||
messengerCredentials: { | ||
verifyToken: 'YOUR_OWN_INFO_HERE, | ||
pageToken: 'YOUR_OWN_INFO_HERE, | ||
fbAppSecret: 'YOUR_OWN_INFO_HERE, | ||
}, | ||
# For Slack | ||
twitterCredentials1: { | ||
// too_cool_for_you | ||
consumer_key: 'YOUR_OWN_INFO_HERE, | ||
consumer_secret: 'YOUR_OWN_INFO_HERE, | ||
access_token: ''YOUR_OWN_INFO_HERE, | ||
access_token_secret: 'YOUR_OWN_INFO_HERE, | ||
}, | ||
twitterCredentials2: { | ||
// watson_chat_demo bot | ||
consumer_key: 'YOUR_OWN_INFO_HERE, | ||
consumer_secret: 'YOUR_OWN_INFO_HERE, | ||
access_token: 'YOUR_OWN_INFO_HERE, | ||
access_token_secret: 'YOUR_OWN_INFO_HERE, | ||
} | ||
} | ||
config.twitterCredentials = config.twitterCredentials1; // just easier and default | ||
module.exports = config; | ||
``` |
@@ -8,3 +8,2 @@ 'use strict' | ||
const crypto = require('crypto'); | ||
const req = require('request'); | ||
require('chai').should(); | ||
@@ -14,6 +13,5 @@ const _ = require('lodash'); | ||
const SessionStore = Botmaster.storage.MemoryStore; | ||
const config = require('../config.js') | ||
const bodyParser = require('body-parser'); | ||
// app.use(bodyParser.json({ verify: verifyRequestSignature })); | ||
/* | ||
@@ -35,10 +33,4 @@ * just start a server listening on port 3000 locally | ||
const TOKEN = process.env.TELEGRAM_TEST_TOKEN; | ||
if (TOKEN === undefined) { | ||
throw new Error('Telegram Bot token must be defined. See Readme.md in ./tests'); | ||
} | ||
const USERID = process.env.TELEGRAM_TEST_USER_ID; | ||
if (USERID == undefined) { | ||
throw new Error('Telegram test user must be defined. See Readme.md in ./tests'); | ||
} | ||
const telegramCredentials = config.telegramCredentials; | ||
const telegramUserId = config.telegramUserId; | ||
@@ -49,5 +41,5 @@ const updateData = { | ||
message_id: 1, | ||
from: {id: USERID, first_name: 'Biggie', last_name: 'Smalls'}, | ||
from: {id: telegramUserId, first_name: 'Biggie', last_name: 'Smalls'}, | ||
chat: { | ||
id: USERID, | ||
id: telegramUserId, | ||
first_name: 'Biggie', | ||
@@ -72,5 +64,3 @@ last_name: 'Smalls', | ||
const telegramSettings = { | ||
credentials: { | ||
authToken: TOKEN | ||
}, | ||
credentials: telegramCredentials, | ||
webhookEndpoint: '/webhook', | ||
@@ -86,4 +76,4 @@ sessionStore: new SessionStore() | ||
const expectedSession = { | ||
id: USERID, | ||
botId: TOKEN, | ||
id: telegramUserId, | ||
botId: telegramCredentials.authToken, | ||
latestMid: 100, | ||
@@ -102,6 +92,6 @@ latestSeq: 1, | ||
it('should result in the update object having the right session', function(done) { | ||
it('should result in the update object having the right session on second message', function(done) { | ||
const expectedSession = { | ||
id: USERID, | ||
botId: TOKEN, | ||
id: telegramUserId, | ||
botId: telegramCredentials.authToken, | ||
latestMid: 101, | ||
@@ -127,25 +117,14 @@ latestSeq: 2, | ||
function getMessengerSignatureHeader(updateData, fbAppSecret) { | ||
const hash = crypto.createHmac('sha1', fbAppSecret) | ||
.update(JSON.stringify(updateData)) | ||
.digest('hex'); | ||
describe('MemoryStore for Messenger Bots', function() { | ||
return `sha1=${hash}`; | ||
} | ||
function getMessengerSignatureHeader(updateData, fbAppSecret) { | ||
const hash = crypto.createHmac('sha1', fbAppSecret) | ||
.update(JSON.stringify(updateData)) | ||
.digest('hex'); | ||
describe('MemoryStore for Messenger Bots', function() { | ||
const MESSENGER_VERIFY_TOKEN = process.env.MESSENGER_VERIFY_TOKEN; | ||
if (MESSENGER_VERIFY_TOKEN === undefined) { | ||
throw new Error('MESSENGER_VERIFY_TOKEN must be defined. See Readme.md in ./tests'); | ||
return `sha1=${hash}`; | ||
} | ||
const MESSENGER_PAGE_TOKEN = process.env.MESSENGER_PAGE_TOKEN; | ||
if (MESSENGER_PAGE_TOKEN == undefined) { | ||
throw new Error('MESSENGER_PAGE_TOKEN must be defined. See Readme.md in ./tests'); | ||
} | ||
const FACEBOOK_APP_SECRET = process.env.FACEBOOK_APP_SECRET; | ||
if (FACEBOOK_APP_SECRET == undefined) { | ||
throw new Error('FACEBOOK_APP_SECRET must be defined. See Readme.md in ./tests'); | ||
} | ||
const messengerCredentials = config.messengerCredentials; | ||
const userId = '134449875'; | ||
@@ -178,3 +157,4 @@ const botId = '123124412' | ||
headers: { | ||
'x-hub-signature': getMessengerSignatureHeader(updateData, FACEBOOK_APP_SECRET) | ||
'x-hub-signature': getMessengerSignatureHeader( | ||
updateData, messengerCredentials.fbAppSecret) | ||
} | ||
@@ -186,7 +166,3 @@ }; | ||
const messengerSettings = { | ||
credentials: { | ||
verifyToken: MESSENGER_VERIFY_TOKEN, | ||
pageToken: MESSENGER_PAGE_TOKEN, | ||
fbAppSecret: FACEBOOK_APP_SECRET | ||
}, | ||
credentials: messengerCredentials, | ||
webhookEndpoint: '/webhook' | ||
@@ -198,3 +174,3 @@ }; | ||
describe('when receiving an update from telegram', function() { | ||
describe('when receiving an update from messenger', function() { | ||
it('should result in the update object having the right session on first message', function(done) { | ||
@@ -217,3 +193,3 @@ const expectedSession = { | ||
it('should result in the update object having the right session', function(done) { | ||
it('should result in the update object having the right session on second message', function(done) { | ||
const expectedSession = { | ||
@@ -236,3 +212,3 @@ id: userId, | ||
options.headers['x-hub-signature'] = getMessengerSignatureHeader( | ||
options.body, FACEBOOK_APP_SECRET) | ||
options.body, messengerCredentials.fbAppSecret) | ||
@@ -239,0 +215,0 @@ request(options); |
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
51111
21
1351