电报机器人处理多个请求并存储用户数据数小时

发布于 2025-02-06 19:10:19 字数 3409 浏览 3 评论 0原文

我是node.js中的新手,目前正在使用node.js + telegraf + mysql创建电报机器人。但是,我的电报机器人无法处理多个用户请求,它将它们混合在一起,过了一会儿,它实际上失去了存储的数据。从我阅读的文章中,似乎需要“会议和国家管理”,老实说,我不知道如何实施会议和国家。请帮忙。

创建连接以连接到数据库的连接

const conn=mysql.createConnection({
host:"*******",
user:"***",
password:"****",
database: "DB"
conn.connect();

查找用户在DB中输入的数字,然后将该数字的结果存储在dataStore = [];

var arr=[wizardData.number,wizardData.firstnumber]
var sql='SELECT DB.tblbotusers.Name,DB.tblbotusers.PhoneNumber AS phoneNumber,DB.tblbotusers.IDDealer ,DB.tblbotusers.UserID FROM     DB.tblbotusers WHERE DB.tblbotusers.PhoneNumber IN (?);'
conn.query(sql,[arr],function(err,result){
if (err){
  throw err;
}
if(result==0)
{
console.log("Number Not Found In Database")
ctx.replyWithHTML("❌Your <b>number</b> is not found in the database! Please register"
,Markup.inlineKeyboard([
[Markup.button.callback('Return to main menu','mainmenu')]
]))
}
else{
console.log(result)
dataStore=[];
result.forEach(item =>{
dataStore.push({
Name:item.Name,
phoneNumber:item.phoneNumber,
IDDealer:item.IDDealer,
UserID:item.UserID
})
});

它利用数据存储[],并且可以使用一段时间,但是几分钟或几分钟后,它指出数据存储的未定义不确定。

 console.log(newagentFN+" "+newagentMSISDN+" "+newagentMSISDNtxt)
                            dataStore.forEach(item =>{
                            var newagent = [wizardData.agentaddname[0],wizardData.agentaddname[1],wizardData.agentidpassnum,status,wizardData.agentnumber,item.IDDealer];
                            var sql='INSERT INTO DB.tblbotagentstoadd(FirstName,LastName,IDNumber,SpecialInstructions,MSISDN,IDDealer) VALUES (?);';
                                conn.query(sql,[newagent],function(err,result){
                                     if (err){
                                       throw err;
                                     };
                                     //console.log('data has been added');
                                 });
                             });

首先,结果存储在数组中,并且机器人运行良好,但是在用户的一些请求后,机器人冻结了此错误,'referenceError:DataStore未定义'

c:\ inetpub \ inetpub \ inetpub \ wwwroot \ scanstockpavbot \ node_modules \ node_modules \ \ node_modules \ \ node_modules \ \ \ \ \ \ node_modules \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \\ mysql \ lib \ protoct \ parser.js:437 投掷错误; // rethrow non-mysql错误 ^

 ReferenceError: dataStore is not defined
at C:\inetpub\wwwroot\scanstockpavbot\scenes\scanstarterpack.js:811:49
at Array.forEach (<anonymous>)
at C:\inetpub\wwwroot\scanstockpavbot\scenes\scanstarterpack.js:810:65
at Array.forEach (<anonymous>)
at Query.<anonymous> (C:\inetpub\wwwroot\scanstockpavbot\scenes\scanstarterpack.js:809:65)
at Query.<anonymous> (C:\inetpub\wwwroot\scanstockpavbot\node_modules\mysql\lib\Connection.js:526:10)
at Query._callback (C:\inetpub\wwwroot\scanstockpavbot\node_modules\mysql\lib\Connection.js:488:16)
at Query.Sequence.end (C:\inetpub\wwwroot\scanstockpavbot\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
at Query._handleFinalResultPacket (C:\inetpub\wwwroot\scanstockpavbot\node_modules\mysql\lib\protocol\sequences\Query.js:149:8)
at Query.EofPacket (C:\inetpub\wwwroot\scanstockpavbot\node_modules\mysql\lib\protocol\sequences\Query.js:133:8)

ps c:\ inetpub \ wwwroot \ scanstockpavbot&gt;

I'm a newbie in node.js and I'm currently using node.js + telegraf + mysql to create a telegram bot. My telegram bot however cannot handle multiple user requests, it mixes them up and after a while it actually loses the data stored. from the articles I read it seems it's in need of "Sessions and state management" and I honestly don't know how to implement sessions and states. Please help.

Create connection to connect to Database

const conn=mysql.createConnection({
host:"*******",
user:"***",
password:"****",
database: "DB"
conn.connect();

It Looks up the number user has entered in in DB and then stores results of that number in dataStore=[];

var arr=[wizardData.number,wizardData.firstnumber]
var sql='SELECT DB.tblbotusers.Name,DB.tblbotusers.PhoneNumber AS phoneNumber,DB.tblbotusers.IDDealer ,DB.tblbotusers.UserID FROM     DB.tblbotusers WHERE DB.tblbotusers.PhoneNumber IN (?);'
conn.query(sql,[arr],function(err,result){
if (err){
  throw err;
}
if(result==0)
{
console.log("Number Not Found In Database")
ctx.replyWithHTML("❌Your <b>number</b> is not found in the database! Please register"
,Markup.inlineKeyboard([
[Markup.button.callback('Return to main menu','mainmenu')]
]))
}
else{
console.log(result)
dataStore=[];
result.forEach(item =>{
dataStore.push({
Name:item.Name,
phoneNumber:item.phoneNumber,
IDDealer:item.IDDealer,
UserID:item.UserID
})
});

It makes use of the dataStore [] and it works for a while but then after a few requests or minutes it states dataStore is undefined.

 console.log(newagentFN+" "+newagentMSISDN+" "+newagentMSISDNtxt)
                            dataStore.forEach(item =>{
                            var newagent = [wizardData.agentaddname[0],wizardData.agentaddname[1],wizardData.agentidpassnum,status,wizardData.agentnumber,item.IDDealer];
                            var sql='INSERT INTO DB.tblbotagentstoadd(FirstName,LastName,IDNumber,SpecialInstructions,MSISDN,IDDealer) VALUES (?);';
                                conn.query(sql,[newagent],function(err,result){
                                     if (err){
                                       throw err;
                                     };
                                     //console.log('data has been added');
                                 });
                             });

At first the results are stored in array and the bot works fine but after a few requests from the user, the bot freezes and I get this error, 'ReferenceError: dataStore is not defined'

C:\inetpub\wwwroot\scanstockpavbot\node_modules\mysql\lib\protocol\Parser.js:437
throw err; // Rethrow non-MySQL errors
^

 ReferenceError: dataStore is not defined
at C:\inetpub\wwwroot\scanstockpavbot\scenes\scanstarterpack.js:811:49
at Array.forEach (<anonymous>)
at C:\inetpub\wwwroot\scanstockpavbot\scenes\scanstarterpack.js:810:65
at Array.forEach (<anonymous>)
at Query.<anonymous> (C:\inetpub\wwwroot\scanstockpavbot\scenes\scanstarterpack.js:809:65)
at Query.<anonymous> (C:\inetpub\wwwroot\scanstockpavbot\node_modules\mysql\lib\Connection.js:526:10)
at Query._callback (C:\inetpub\wwwroot\scanstockpavbot\node_modules\mysql\lib\Connection.js:488:16)
at Query.Sequence.end (C:\inetpub\wwwroot\scanstockpavbot\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
at Query._handleFinalResultPacket (C:\inetpub\wwwroot\scanstockpavbot\node_modules\mysql\lib\protocol\sequences\Query.js:149:8)
at Query.EofPacket (C:\inetpub\wwwroot\scanstockpavbot\node_modules\mysql\lib\protocol\sequences\Query.js:133:8)

PS C:\inetpub\wwwroot\scanstockpavbot>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文