我从未使用过 Cosmos DB,所以我真的不知道连接是如何工作的。我确实寻找了很长时间的答案,但没有找到任何答案。在文档中,我只找到了如何将机器人与我的 Cosmos DB 连接,我确实做到了。
我需要的答案:
- 检索用户注册时保存到数据库的用户地址的代码是什么(
//REGISTER
)
-- 在 //TEST
中,我有一个简单的代码来发送主动消息,现在,如您所见,它仅发送到应用程序中地址的当前状态。相反,我需要从保存用户的 Azure Cosmos DB 中检索它。
HERE IS THE LINK FOR MY BOT CODE
我提前非常感谢任何至少会尝试回答的人。 =)
内联代码:
var restify = require('restify');
var builder = require('botbuilder');
var fs = require('fs');
//RESTIFY SETUP
var https_options = {
key: fs.readFileSync('./herobot.hero-translating.com.key'), //on current folder
certificate: fs.readFileSync('./herobot.hero-translating.com.crt')
};
var server = restify.createServer(https_options);
server.listen(process.env.port || process.env.PORT || 8081, function() {
console.log('%s listening to %s', server.name, server.url);
});
//BOT CONNECTOR
var connector = new builder.ChatConnector({
appId: 'HIDDEN',
appPassword: 'HIDDEN'
});
//COSMOS DB CONNECTION
var azure = require('botbuilder-azure');
var documentDbOptions = {
host: 'https://HIDDEN.documents.azure.com:443/',
masterKey: 'HIDDEN',
database: 'HIDDEN',
collection: 'HIDDEN',
};
var docDbClient = new azure.DocumentDbClient(documentDbOptions);
var cosmosStorage = new azure.AzureBotStorage({ gzipData: false }, docDbClient);
var bot = new builder.UniversalBot(connector);
bot.set('storage', cosmosStorage);
server.post('/api/smessages', connector.listen());
//DIALOGS
String.prototype.contains = function(content) {
return this.indexOf(content) !== -1;
};
bot.dialog('/', function(session) {
function sendProactiveMessage(address) {
var msg = new builder.Message().address(address);
msg.text('Test message.');
msg.textLocale('en-US');
bot.send(msg);
}
const userText = session.message.text.toLowerCase();
String.prototype.is = function() {
const args = Array.prototype.slice.call(arguments);
const text = userText;
for (let i = 0; i < args.length; i++)
if (text.includes(args[i])) return true;
return false;
};
//Greeting
if (userText.is('hello', 'hi', 'good morning', 'good afternoon', 'hey'))
session.send(`Hello.`);
//Thanks
else if (userText.is('thank you', 'thanks', 'thx'))
session.send(`You are welcome!`);
//REGISTER
else if (userText.is('register')) {
var savedAddress = session.message.address;
session.userData.savedAddress = savedAddress;
//REGISTERED MESSAGE
session.send("*Congratulations! You are now registered in our network! (goldmedal)*");
console.log(JSON.stringify(savedAddress));
}
//TEST
else if (userText.is('test')) {
sendProactiveMessage(savedAddress);
}
//Unrecognized messages
else
session.send(`I don't understand your input.`);
});
最佳答案
我认为您正在寻找的是从 Cosmos DB 向用户发送主动消息。
当您在代码中注册用户时,您将session.message.address保存到 session 用户的userData中。您可以在 Cosmos DB 中观察这些数据。 在我的机器人模拟器上运行时,我将在 Cosmos DB/botdocs/botdata 中看到 3 个文档(分钟):
- 默认用户,userData
- [conversationId],conversationData
- [conversationId],默认用户
default-user 本质上是机器人的用户 ID。并且您将地址(session.userData.savedAddress = savingAddress;)保存在userData中。
要发送主动消息,您只需在 Cosmos DB 中查询类似 (1.) 的文档并发送消息。
可以引用我的code 。此示例的当前触发器是在 5 秒后使用计时器,这将向所有注册用户发送主动消息。您可以更改查询以选择特定用户或将触发器更改为http请求。
关于node.js - Bot Framework 从 Cosmos DB 检索用户地址以发送主动消息 NODE JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49427925/