-
-
Save tdelam/a34e4163034e462c284adfe76c06ac5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
add the imports:
const EventEmitter = require("events").EventEmitter; let event = new EventEmitter();