【发布时间】:2018-03-26 11:21:19
【问题描述】:
我从未使用过 Cosmos DB,所以我真的不知道连接是如何工作的。我确实搜索了很长时间的答案,但没有找到任何答案。在文档中,我只找到了如何将 Bot 与我的 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.`);
});
【问题讨论】:
-
您是否尝试过使用
documentdb?这是一个指向 sample 的链接,了解如何在 Web 应用代码中使用该包。 -
另外,有些人会说这是一个不完整或格式不佳的问题,因为通过查看您的代码,看起来您并没有尝试为您的主动消息创建与 CosmosDB 的连接。它仅包含用于管理 Bot 状态的 CosmosDB 连接。 Stack Overflow 对问题不满意,至少没有显示您的问题的解决方案。
标签: node.js database azure botframework azure-cosmosdb