我有一个用Node.js编写的MS Teams bot。机器人会询问一系列问题,并且最后通过访问会话变量来显示最终的响应。一切都很好。
现在我尝试将会话变量存储在MS Azure SQL DB中。数据库在Azure中正确设置,因为我可以在SSMS中访问和向其写入数据。但我相信我可能在我的机器人代码中错误地连接到数据库。我正在使用的机器人代码来自:
connecting to SQL using Node.js
该代码对我有意义。但是我如何在我的机器人中使用该代码?这是我到目前为止所尝试的......
目前我正在使用本地内存MemoryBotStorage()并设置为。
var inMemoryStorage = new builder.MemoryBotStorage();
.set('storage', inMemoryStorage)
在处理Azure Cosmos数据库的另一个Microsoft article中,它指出“4.指定您要使用自定义数据库而不是内存存储。”所以我从中推断出我将实例化的sql db添加到.set('storage',DB Goes Here)但我的尝试失败了,我不确定我是否正确?
所以我的问题是如何从我的机器人代码中正确访问Azure sql server DB - 这是我提供的链接,甚至是正确的方法?
谢谢
注意 - 此代码sample对我有用 - 我能够连接和查询我的Azure数据库 - 但它只是数据库代码,并没有考虑机器人代码。
编辑 - 代码:
const builder = require('botbuilder');
const builderTeams = require('botbuilder-teams');
const restify = require('restify');
const connector = new builderTeams.TeamsChatConnector(
{
appId: "My app ID,
appPassword: "My App PW",
}
);
var inMemoryStorage = new builder.MemoryBotStorage();
const bot = new builder.UniversalBot(connector, [
function (session) {
session.send("Welcome.");
builder.Prompts.text(session, "Question1?");
},
function (session, results) {
session.dialogData.question1 = results.response;
builder.Prompts.text(session, "Question2?");
},
function (session, results) {
session.dialogData.Question2 = results.response;
builder.Prompts.text(session, "Question3?");
},
function (session, results) {
session.dialogData.Question3 = results.response;
// Begin DB
var Connection = require('tedious').Connection;
var config = {
userName: 'myusername',
password: 'mypw',
server: 'myserver.database.windows.net',
// If you are on Azure SQL Database, you need these next options.
options: { encrypt: true, database: 'mydb' }
};
var connection = new Connection(config);
connection.on('connect', function (err) {
// If no error, then good to proceed.
console.log("Connected");
executeStatement1();
});
var Request = require('tedious').Request
var TYPES = require('tedious').TYPES;
function executeStatement1() {
request = new Request("INSERT my (Username, Question1, Question2, Question3, StatusDate) VALUES (@Username, @Question1, @Question2, @Question3, CURRENT_TIMESTAMP);", function (err) {
if (err) {
console.log(err);
}
});
request.addParameter('Username', TYPES.NVarChar, session.userData.userName);
request.addParameter('Question1', TYPES.NVarChar, session.dialogData.Question1);
request.addParameter('Question2', TYPES.NVarChar, session.dialogData.Question2);
request.addParameter('Question3', TYPES.NVarChar, session.dialogData.Question3);
request.on('row', function (columns) {
columns.forEach(function (column) {
if (column.value === null) {
console.log('NULL');
} else {
console.log("ID of inserted item is " + column.value);
}
});
});
connection.execSql(request);
// End DB
// Process request and display details
session.endDialog();
}
]).set('storage', inMemoryStorage)
const server = restify.createServer();
server.post('api/messages', connector.listen());
server.listen(portnumber)
使用npm start运行时出错:
npm start
> simplebot@1.0.0 start C:\Developer\dailyStatus
> node index.js
C:\Developer\dailyStatus\index.js:81
]).set('storage', inMemoryStorage)
^
SyntaxError: Unexpected token ]
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! simplebot@1.0.0 start: `node index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the simplebot@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely...
npm ERR! A complete log of this run can be found in:
npm ERR! C: etc.
FINAL
我能够使用此tutorial来解决这个问题。还要感谢Marc LeFleur。
答案 0 :(得分:1)
你有几个错别字。例如,您错过了"
上的结束appId
:
const connector = new builderTeams.TeamsChatConnector(
{
appId: "My app ID",
appPassword: "My App PW",
}
);
您也无法在<{1}}函数的中声明function executeStatement1() {...}
函数。这需要存在于构造函数之外,而从IDialogWaterfallStep
调用。