gpt4 book ai didi

node.js - 将 Node.js BOT 连接到 MS Azure SQL 数据库

转载 作者:太空宇宙 更新时间:2023-11-04 00:10:10 25 4
gpt4 key购买 nike

我有一个用 Node.js 编写的可用 MS Teams 机器人。该机器人会提出一系列问题,并当前通过访问 session 变量在最后显示响应。一切都很好。

现在我尝试将 session 变量存储在 MS Azure SQL DB 中。数据库已在 Azure 中正确设置,因为我可以在 SSMS 中访问并向其写入数据。但我相信我的机器人代码中可能错误地连接到了数据库。我正在使用的机器人代码来自:

connecting to SQL using Node.js

该代码对我来说很有意义。但是我如何在我的机器人中使用该代码呢?这是我迄今为止所做的尝试...

目前我正在使用本地内存 MemoryBotStorage() 并进行设置。

var inMemoryStorage = new builder.MemoryBotStorage();
.set('storage', inMemoryStorage)

在另一个 Microsoft article在处理 Azure Cosmos DB 时,它指出“4.指定您要使用自定义数据库而不是内存存储。”因此,我推断我必须将实例化的 sql 数据库添加到 .set('storage', DB Goes Here) 但我的尝试失败了,我不确定我是否正确?

所以我的问题是如何从我的机器人代码中正确访问 Azure sql server DB - 我提供的链接是否正确?

谢谢

注意 - 此代码 sample为我工作 - 我能够连接和查询我的 Azure DB - 但它只是数据库代码,没有考虑机器人代码。

编辑 - 代码:

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.

最终

我能够使用这个 tutorial 来完成这个工作。还要感谢 Marc LeFleur。

最佳答案

您有几个错别字。例如,您缺少 appId 上的结束 ":

const connector = new builderTeams.TeamsChatConnector(
{
appId: "My app ID",
appPassword: "My App PW",
}

);

您也不能在 IDialogWaterfallStep 函数中声明 functionexecuteStatement1() {...} 函数。它需要位于构造函数之外,并从 IDialogWaterfallStep调用

关于node.js - 将 Node.js BOT 连接到 MS Azure SQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50161108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com