将 Node.js BOT 连接到 MS Azure SQL 数据库
Connect Node.js BOT to MS Azure SQL database
我有一个用 Node.js 编写的 MS Teams 机器人。该机器人会询问一系列问题,目前通过访问会话变量在最后显示响应。一切都很好。
现在我试图将会话变量存储在 MS Azure SQL 数据库中。数据库已在 Azure 中正确设置,因为我可以在 SSMS 中访问和写入数据。但我相信我可能在我的机器人代码中错误地连接到数据库。我使用的机器人代码来自:
connecting to SQL using Node.js
该代码对我来说很有意义。但是我如何在我的机器人中使用该代码?这是我迄今为止尝试过的...
目前我正在使用本地内存 MemoryBotStorage() 并设置为那个。
var inMemoryStorage = new builder.MemoryBotStorage();
.set('storage', inMemoryStorage)
在另一个处理 Azure Cosmos DB 的 Microsoft article 中,它声明“4.Specify 你想使用自定义数据库而不是内存存储。”因此,据此我推断我必须将实例化的 sql 数据库添加到 .set('storage', DB Goes Here) 但我的尝试失败了,我不确定我是否正确?
所以我的问题是如何从我的机器人代码正确访问 Azure sql 服务器数据库 - link 我提供的方法是否正确?
谢谢
注意 - 此代码 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 启动时出错:
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.
决赛
我能够使用这个 tutorial 来实现它。还要感谢 Marc LeFleur。
你有几个错别字。例如,您在 appId
:
上缺少结尾 "
const connector = new builderTeams.TeamsChatConnector(
{
appId: "My app ID",
appPassword: "My App PW",
}
);
您也不能在 IDialogWaterfallStep
函数的 中 声明 function executeStatement1() {...}
函数。这需要存在于构造函数之外,并且 从 IDialogWaterfallStep
调用 。
我有一个用 Node.js 编写的 MS Teams 机器人。该机器人会询问一系列问题,目前通过访问会话变量在最后显示响应。一切都很好。
现在我试图将会话变量存储在 MS Azure SQL 数据库中。数据库已在 Azure 中正确设置,因为我可以在 SSMS 中访问和写入数据。但我相信我可能在我的机器人代码中错误地连接到数据库。我使用的机器人代码来自:
connecting to SQL using Node.js
该代码对我来说很有意义。但是我如何在我的机器人中使用该代码?这是我迄今为止尝试过的...
目前我正在使用本地内存 MemoryBotStorage() 并设置为那个。
var inMemoryStorage = new builder.MemoryBotStorage();
.set('storage', inMemoryStorage)
在另一个处理 Azure Cosmos DB 的 Microsoft article 中,它声明“4.Specify 你想使用自定义数据库而不是内存存储。”因此,据此我推断我必须将实例化的 sql 数据库添加到 .set('storage', DB Goes Here) 但我的尝试失败了,我不确定我是否正确?
所以我的问题是如何从我的机器人代码正确访问 Azure sql 服务器数据库 - link 我提供的方法是否正确?
谢谢
注意 - 此代码 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 启动时出错:
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.
决赛
我能够使用这个 tutorial 来实现它。还要感谢 Marc LeFleur。
你有几个错别字。例如,您在 appId
:
"
const connector = new builderTeams.TeamsChatConnector(
{
appId: "My app ID",
appPassword: "My App PW",
}
);
您也不能在 IDialogWaterfallStep
函数的 中 声明 function executeStatement1() {...}
函数。这需要存在于构造函数之外,并且 从 IDialogWaterfallStep
调用 。