Im working on a bot with Bot framework v4 in nodejs.
I want my Conversation reference for every user to be stored into an azure db.
Conversation reference is in a nested dictionary format. {'convid':{"a":'value","b":"value"}}
I want this dictionary to be stored in Azure DB (Which Db would be suitable?free version?Paid?) so that i could later retrieve this DB and send out a proactive messages to all users whose conv ref is stored.
I just want to know how to store dictionary into any azure DB with node js?
and retrieve it later? Any help would be appreciated.
Saving the conversation reference during a (private) conversation can be achieved using the following snippet.
conversationReference = TurnContext.getConversationReference(context.activity);
You can eventually save this JSON directly to CosmosDB using the Cosmos Javascript SDK.
const databaseDefinition = { id: "sample database" };
const collectionDefinition = { id: "sample collection" };
const { database } = await client.databases.create(databaseDefinition);
const { container } = await database.containers.create(collectionDefinition);
const { resource } = await container.items.create(conversationReference);
#Sree - You can pull all the conversation using Graph API's please check the documents. If you have requirement to store data in dictionary only, you can explore Cosmos db.
Please check Pricing.
Related
I have to log the user-bot conversation in CosmosDB for audit/history purpose. In V3 using .Net, I was using table logger module, like the below code.
builder.RegisterModule(new TableLoggerModule(account, chatHistoryTableName));
Now we are upgrading/rewriting the bot to V4 in NodeJS. Please guide if there is a similar approach available for V4 in NodeJS to save the entire conversation?
This example hasn't been merged yet: https://github.com/Microsoft/BotBuilder-Samples/pull/1266
It uses AzureBlobTranscriptStore and TranscriptLoggerMiddleware
const { AzureBlobTranscriptStore } = require('botbuilder-azure');
const { TranscriptLoggerMiddleware } = require('botbuilder-core');
// Get blob service configuration as defined in .bot file
const blobStorageConfig = botConfig.findServiceByNameOrId(BLOB_CONFIGURATION);
// The transcript store has methods for saving and retrieving bot conversation transcripts.
let transcriptStore = new AzureBlobTranscriptStore({storageAccountOrConnectionString: blobStorageConfig.connectionString,
containerName: blobStorageConfig.container
});
// Create the middleware layer responsible for logging incoming and outgoing activities
// into the transcript store.
var transcriptMiddleware = new TranscriptLoggerMiddleware(transcriptStore);
adapter.use(transcriptMiddleware);
This should provide a good start.
https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-howto-v4-state?view=azure-bot-service-4.0&tabs=javascript
Here is sample code for querying a CosmosDB
Here is the part of the code I am concerned with replicating so I can make queries on my own database.
const cosmos = require("../../lib/");
const CosmosClient = cosmos.CosmosClient;
const config = require("../Shared/config");
const databaseId = config.names.database;
const containerId = config.names.container;
const endpoint = config.connection.endpoint;
const masterKey = config.connection.authKey;
// Establish a new instance of the CosmosClient to be used throughout this demo
const client = new CosmosClient({ endpoint, auth: { masterKey } });
I'm not sure what my endpoint should be in order to query the database. My queries are Read-only (doesn't need to be strictly read only, but for what I'm doing, read-only is all that's needed), but I don't know what is in ../../lib/ that the cosmos variable is set equal to, and I don't know what the endpoint variable should contain (obviously some sort of endpoint, but which one). The ../../lib/ directory doesn't appear to be in the github repo.
The connection information can be found in the Keys section of the Azure portal.
The endpoint is the value in the URI section.
In case of the CosmosDB emulator, the default URI is https://localhost:8081.
This question already has an answer here:
Access db data in Cloud Functions for Firebase
(1 answer)
Closed 5 years ago.
So in my firebase app, I want to access an array whenever the data source is updated.
This part of app works well. The variables are assigned successfully.
exports.sendUsersNotification = functions.database.ref('/Groups/{groupId}/messageList/{messageId}/')
.onWrite(event => {
const original = event.data.val();
let userId = original['userId']
let groupId = event.params.groupId;
let messageId = event.params.messageId;
const payload = {
notification: {
title: 'Messenger',
body: 'You have a new message'
}
};
});
The array I want to access and loop through is in 'Groups/{groupId}/listeners'.
How can I access it?
Cloud Functions database triggers will only give you the data at the location in the database that matches the pattern you gave in functions.database.ref(). You can use the Admin SDK to query the database at other locations, or you can use event.data.ref.root to build a Reference to other locations, and use once() to perform the query. There are a lot of examples of this in documentation and samples.
I've been using successfully Firebase Functions to perform operations on files I've been uploading to Firebase Storage.
What I currently want to do is access the folder into which I'm shoving data by using wildcards, with the same method of implementation as how it works for the Realtime Database + Firestore. However, whenever I try to access these parameters, they exclusively return null.
Is this not possible?
Here's an example of my code:
exports.generateThumbnail = functions.storage.object('user-photos/{userId}/{publicOrPrivate}').onChange(event => {
const privateOrPublic = event.params.publicOrPrivate;
const isPrivate = (event.params.publicOrPrivate == "private");
const userId = event.params.userId;
console.log("user id and publicOrPrivate are", userId, privateOrPublic);
//"user id and publicOrPrivate are undefined undefined"
}
Thanks!
There is currently no wildcard support for Cloud Storage triggers. You have to check the path of the file that changed to figure out if you want to do anything with it.
When I write data to firebase database from both frontend(Angular 4) and backend(firebase functions), there is a push key generated by firebase. With this key, I cannot access data in the future because the key is unique. I am wondering is any way I can set the key myself or I can access the data without knowing the key?
Here is my code from frontend:
this.db.list(`${this.basePath}/`).push(upload);
Here is my code from backend:
admin.database().ref('/messages').push({original: original}).then(function (snapshot) {
res.redirect(303, snapshot.ref);});
All data I pushed will be under path/pushID/data
I cannot access data without knowing the pushID.
The best case I want is path/my own pushID/data
Thanks so much for help!!
If you want to loop through all messages:
var ref = firebase.database().ref("messages");
ref.once("value", function(snapshot) {
snapshot.forEach(function(message) {
console.log(message.key+": "+message.val().original);
});
});
If you want to find specific messages, use a query:
var ref = firebase.database().ref("messages");
var query = ref.orderByChild("original").equalTo("aaaa");
query.once("value", function(snapshot) {
snapshot.forEach(function(message) {
console.log(message.key+": "+message.val().original);
});
});
For much more on this, read the Firebase documentation on reading lists of data, sorting and filtering data, and take the Firebase codelab.
The keys should be unique in any way. You can set your own key like this instead of push
admin.database().ref('/messages/'+ yourUniqueId).set({original: original}).then(function (snapshot) {
res.redirect(303, snapshot.ref);});
yourUniqueId can be auth uid or email of user, like something unique.