2017-10-17 58 views
1

我想在微軟的Bot Framework提供的bot用戶數據存儲器中存儲一個簡單的鍵值對。當`session`對象不可用時,在Bot Framework中讀/寫bot用戶數據

一般而言,這很容易做到:

session.userData.key = value; 

不過,我想這樣做時,session對象不可用,在處理所有傳入消息的事件處理程序:

bot.on('incoming', incoming => { 
    // Check whether user asked to switch on debug mode. 
    if (incoming.text === 'debug on') { 
    console.log('Enabling debug mode.'); 
    // TODO FIXME - we need to save the result to bot memory. 
    } 
}); 

是有沒有其他方法可以保存到主app.js文件或事件處理程序的機器人內存?

回答

2

我發現瞭解決方案,這是使用UniversalBot.loadSession()

bot.on('incoming', incoming => { 
    // Load the user session so that we can save values to userData. 
    bot.loadSession(incoming.address, (error, session) => { 
    if (!error) { 
     // work with the session object as usual 
    } 
    }); 
}); 

您加載session對象之後,可以使用session.send()session.userData如常。

相關問題