telegram bot handle multiple requests and store user data for hours

Viewed 25

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>

0 Answers
Related