Skip to content

Instantly share code, notes, and snippets.

@jamesmorgan
Last active July 30, 2017 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesmorgan/454fedaac949b73db430e3233088dfc2 to your computer and use it in GitHub Desktop.
Save jamesmorgan/454fedaac949b73db430e3233088dfc2 to your computer and use it in GitHub Desktop.
contactRelationUpdate event hook to persist data to firebase
bot.on("contactRelationUpdate", (message) => {
const userId = message.user.id;
const username = message.user.name || "--";
const channelId = message.address.channelId;
const address = message.address;
// When the users adds the bot
if (message.action === "add") {
// Persist the user to firebase
// Create a reference for the specific channel and user e.g. `emulator/userA` or `slack/userA` or `skype/userB`
firebase.database().ref(`chat-users/${channelId}/${userId}`)
.set({
userId, username, channelId, address, // Data stored in firebase
})
.then(() => {
// Once we've stored this simply reply to the user welcoming them
bot.send(new builder.Message()
.address(message.address)
.text("Hello %s... Thanks for adding the bot", username));
})
.catch((err) => console.log(err));
}
// When a user removes the bot
if (message.action === "remove") {
// Remove the user from firebase (optional)
firebase.database().ref(`chat-users/${channelId}/${userId}`)
.remove()
.then(() => {
// Once removed send them a good bye message
bot.send(new builder.Message()
.address(message.address)
.text("%s has removed the bot", username));
})
.catch((err) => console.log(err));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment