Microsoft bot 框架单独保存对话或会话

时间:2021-03-16 16:41:21

标签: node.js botframework tedious

我在 Azure 上部署了一个 Microsoft bot 框架聊天机器人,我正在使用 Tedious 来保存我的对话,事实是,bot 正在网络上使用,许多人可以打开它以同时进行交互,但是当我保存对话时来自用户,它保存了其他用户同时进行的所有其他交互,我需要每个用户都单独保存自己的对话,即使他们同时与聊天机器人交互...

这是我的代码,也许我遗漏了什么:

Bot.js

//SQL Connection
var Connection = require('tedious').Connection;  
var config = {  
    server: 'myserver',
    authentication: {
        type: 'default',
        options: {
            userName: 'myuser',
            password: 'mypass' 
        }
    },
    options: {
        encrypt: true,
        database: 'mydatabase'
    }
};  
const connection = new Connection(config);  
connection.on('connect', function(err) {  
    console.log("Connected");  
});  
var Request = require('tedious').Request  
var TYPES = require('tedious').TYPES; 


// Function to save the conversation and bot ids
function executeConversationStatement(bot, cid, ulg ) {  
    request = new Request("INSERT INTO table (bot_id, conversationID, conversation_date, userlogged) VALUES (@bot, @cid, CURRENT_TIMESTAMP, @ulg); SELECT @@IDENTITY AS ID",function(err) {  
     if (err) {  
        console.log(err);}  
    });  
    request.addParameter('bot', TYPES.Int, bot);  
    request.addParameter('cid', TYPES.NVarChar, cid);  
    request.addParameter('ulg', TYPES.NVarChar, ulg); 
    request.on('row', function(columns) { 
        insertedcid = columns[0].value; // This is the id I pass later
        columns.forEach(function(column) {  
          if (column.value === null) {  
            console.log('NULL');  
          } else {  
            console.log("Conversation id of inserted item is " + column.value);  
          }  
        });
    }); 
    connection.execSql(request);  
}  


// Here on members added I save the conversation id generated by the framework
class BOT extends ActivityHandler {
    constructor(conversationState,userState,telemetryClient) {
        super();

        this.conversationState = conversationState;
        this.userState = userState;
        this.dialogState = conversationState.createProperty("dialogState");
 
        this.previousIntent = this.conversationState.createProperty("previousIntent");
        this.conversationData = this.conversationState.createProperty('conservationData');

        const qnaMaker = new QnAMaker({
            knowledgeBaseId: process.env.QnAKnowledgebaseId,
            endpointKey: process.env.QnAEndpointKey,
            host: process.env.QnAEndpointHostName
        });

        this.qnaMaker = qnaMaker;
        

        this.onMessage(async (context, next) => {

            await this.dispatchToIntentAsync(context);
            await next();

        });

        this.onDialog(async (context, next) => {
            await this.conversationState.saveChanges(context, false);
            await this.userState.saveChanges(context, false);
            await next();
        });   
        
        this.onMembersAdded(async (context, next) => {

            const { channelId, membersAdded } = context.activity;
            actid = context._activity.id;
            if (channelId === 'directline' || channelId === 'webchat') {
              for (let member of membersAdded) {
                if (member.id === context.activity.recipient.id) {
                  await context.sendActivity("Hi, I´m a chatbot to guide You");
                  try{
                      var saveqna = new executeConversationStatement(context._activity.id , 'Invitado');
                  }
                  catch{
                      console.log('Not saved');
                  }
                }
              }
            }
            await next();
        });

    }

//Finally, here I save the interaction:
async dispatchToIntentAsync(context) {
    var result = await this.qnaMaker.getAnswers(context);
    // Statement to save interaction with the insertedcid obtained above
    var saveqnaint = new executeInteractionStatement(insertedcid, context._activity.text, result); 
}

无论我使用t生成的Id还是数据库pk,当多个用户聊天时我总是保持相同的标识符,我如何为每个会话获得单独的Id?

0 个答案:

没有答案