Last active
July 30, 2017 15:11
-
-
Save jamesmorgan/454fedaac949b73db430e3233088dfc2 to your computer and use it in GitHub Desktop.
contactRelationUpdate event hook to persist data to firebase
This file contains hidden or 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
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