const rp = require('request-promise');
require('dotenv').config()
const TelegramApi = require('node-telegram-bot-api')
const bot = new TelegramApi(process.env.BOT_TOKEN, {polling: true})
var db = require('./my_sql_connect.js');
const cron = require("node-cron");
const propotkl=5;
bot.setMyCommands([
{command: '/add', description: 'Add node: /add xxx.xxx.xxx.xxx:9000'},
{command: '/delete', description: 'Delete node: /delete xxx.xxx.xxx.xxx:9000'},
{command: '/notif', description: 'show logs'},
{command: '/notifoff', description: 'hide logs'},
{command: '/my', description: 'your nodes'}
])
const start = () => {
bot.on('message', async msg => {
const text = msg.text;
/* if(msg.from.id != chatId){
return bot.sendMessage(msg.from.id, 'создайте своего бота, в инструкции же написано!');
} */
//console.log(msg)
if(text === '/start'){
return bot.sendMessage(msg.from.id, `Welcome to Sui testnet checker bot`)
}
if(/^\/add\s([0-9]{1,3}[\.]){3}[0-9]{1,3}[\:]{1}[0-9]{4,5}$/.test(text)){
var ip=text.replace("/add ", "").trim();
const sql = `INSERT INTO userip(userid,ipu) VALUES (?, ?);`
db.query(sql, [msg.from.id,ip],function(err, resp) {
if(err) {
bot.sendMessage(msg.from.id,'failed to add data | не удалось добавить данные')
return console.log(err);
}
bot.sendMessage(msg.from.id,`ip ${ip} added to user ${msg.from.id}`)
console.log('Inserted ')
})
return
}
if(text === '/my'){
console.log("нажат my")
console.log("базу прошли")
const sql = `SELECT ipu FROM userip WHERE userid=?`;
db.query(sql, [msg.from.id],function(err, resp) {
if(err) {
bot.sendMessage(msg.from.id,'не удалось достать данные | failed to retrieve data')
return console.log(err);
}
//bot.sendMessage(msg.from.id,`ip ${ip} добавлен к юзеру ${msg.from.id}`)
if(resp && resp!=''){
resp.map((ip)=>{
bot.sendMessage(msg.from.id,`Ваши ip ${ip.ipu}`)
})
}else{
bot.sendMessage(msg.from.id,`You haven't added an ip yet | Вы еще не добавили ip`)
}
})
return
}
if(text === '/notif'){
let tmpsql=`UPDATE userip SET worked=0 WHERE userid=?`;
db.query(tmpsql, [msg.from.id],function(err, resp) {
if(err) {
return console.log(err);
}
bot.sendMessage(msg.from.id, `Sending logs is enabled`)
})
}
if(text === '/notifoff'){
let tmpsql=`UPDATE userip SET worked=1 WHERE userid=?`;
db.query(tmpsql, [msg.from.id],function(err, resp) {
if(err) {
return console.log(err);
}
bot.sendMessage(msg.from.id, `Sending logs is disabled`)
})
return
}
if(/^\/delete\s([0-9]{1,3}[\.]){3}[0-9]{1,3}[\:]{1}[0-9]{4,5}$/.test(text)){
var ip=text.replace("/delete ", "").trim();
const sql = `DELETE FROM userip WHERE ipu=? AND userid=?;`
db.query(sql, [ip,msg.from.id],function(err, resp) {
if(err) {
bot.sendMessage(msg.from.id,'failed to delete ip | не удалось удалить ip')
return console.log(err);
}
if(resp.affectedRows>0) {
bot.sendMessage(msg.from.id,`ip ${ip} deleted`)
}else{
bot.sendMessage(msg.from.id,`ip ${ip} not found`)
}
console.log('Deleted:')
})
return
}
return bot.sendMessage(msg.from.id, `Unknown command`)
})
}
start()
async function curl(val){
const ip = `http://${val}`
var options = {
method: "POST",
//uri: "https://fullnode.testnet.sui.io:443",
uri: ip,
body: { "jsonrpc":"2.0", "method":"sui_getTotalTransactionNumber","id":1},
headers:{"Content-Type":"application/json"},
json: true
}
try{
let tmp = await rp(options)
console.log(ip," :ответил", tmp.result)
return(tmp)
}catch{
console.log(ip," :неотвечает")
}
}
cron.schedule('*/1 * * * * *', async () => {
let bddata;
console.log("зашли читать БД");
const sql = `SELECT * FROM userip`;
db.query(sql, '',function(err, resp) {
if(err) {
//bot.sendMessage(msg.from.id,'не удалось добавить данные')
return console.log(err);
}
bddata =resp
//console.log(bddata)
if(bddata && bddata!=''){
bddata.map(async (val)=>{
console.log("получили ip", val.ipu)
let tmp = await curl(val.ipu);
})
}else{
// console.log(`База пуста`)
}
})
//console.log("bddata")
}) |