Skip to content

Instantly share code, notes, and snippets.

@tdelam
Created July 6, 2018 17:36
Show Gist options
  • Save tdelam/a34e4163034e462c284adfe76c06ac5e to your computer and use it in GitHub Desktop.
Save tdelam/a34e4163034e462c284adfe76c06ac5e to your computer and use it in GitHub Desktop.
controller.on('conversationStarted', (bot, convo) => {
console.log('A conversation started with ', convo.context.user);
// Map the convo when it starts with the proper user
// and set `quote_completed` to false. This is useful for
// resuming a quote. This is set to true with convo.on('end') callback
if (convo.status === 'active') {
const usersPublicProfile = 'https://graph.facebook.com/v2.6/' + convo.context.user + '?fields=first_name,last_name,&access_token=' + process.env.page_token;
request({
url: usersPublicProfile,
json: true // parse
}, (error, response, body) => {
if (!error && response.statusCode === 200) {
event.firstName = body.first_name;
event.lastName = body.last_name;
event.emit('update');
}
});
event.on('update', () => {
const { user } = convo.context;
const unixTimestamp = Date.now();
const humanTimestamp = new Date(unixTimestamp);
msg = {
id: user,
quoteCompleted: false,
convoStartedAt: humanTimestamp,
humanRequested: false,
firstName: event.firstName,
lastName: event.lastName
}
controller.storage.messages.save(msg, (err, id) => {
if (err) {
throw new Error(err);
}
});
});
}
});
@tdelam
Copy link
Author

tdelam commented Jul 13, 2018

add the imports:

const EventEmitter = require("events").EventEmitter; let event = new EventEmitter();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment