2019-07-18 18:23:44 +05:30
|
|
|
// TOREAD: https://core.telegram.org/bots/api
|
2019-07-11 16:17:17 +05:30
|
|
|
const request = require('request')
|
2019-07-18 18:23:44 +05:30
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
|
|
|
|
|
|
|
|
const token = JSON.parse( fs.readFileSync('api-token.json')).token
|
2019-08-04 21:31:55 +05:30
|
|
|
let parameters = JSON.parse(fs.readFileSync('default-parameters.json'))
|
|
|
|
|
2019-07-18 18:23:44 +05:30
|
|
|
const base = `https://api.telegram.org/bot${token}/`
|
|
|
|
console.log(`Token: ${token}`)
|
2019-08-04 21:31:55 +05:30
|
|
|
|
|
|
|
let upDateOngoing=false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setInterval(()=>{
|
|
|
|
|
|
|
|
if(!upDateOngoing){
|
|
|
|
upDateOngoing=true;
|
|
|
|
request.post(
|
|
|
|
{
|
|
|
|
"url":`${base}getUpdates`,"json":true,"body": parameters
|
|
|
|
},
|
|
|
|
(err,res,body)=>{
|
|
|
|
// Checking response
|
|
|
|
if(err){
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
//console.log(res.body+`${typeof(res.body)}`)
|
|
|
|
|
|
|
|
let contents = res.body||{'ok':false}
|
|
|
|
if(!contents.ok){
|
|
|
|
console.log(contents)
|
|
|
|
throw new Error("Not Ok")
|
|
|
|
}
|
|
|
|
// contents - Now work on response
|
|
|
|
if(contents.result.length>0){
|
|
|
|
console.log(`Update:${JSON.stringify(contents.result)}`)
|
|
|
|
// Ready to work on
|
2019-08-05 09:43:07 +05:30
|
|
|
if(parameters.offset===null){
|
|
|
|
parameters.offset = contents[0].update_id + 1
|
|
|
|
}
|
2019-08-04 21:31:55 +05:30
|
|
|
contents.result.forEach(e=>{
|
2019-08-05 09:43:07 +05:30
|
|
|
if(e.update_id + 1 > parameters.offset)
|
|
|
|
{
|
|
|
|
parameters.offset = e.update_id + 1
|
|
|
|
}
|
2019-08-04 21:31:55 +05:30
|
|
|
console.log(e.update_id)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
else{
|
2019-08-05 09:43:07 +05:30
|
|
|
console.log("Polling Timed out, empty.")
|
2019-08-04 21:31:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Allow function to run in next interval since complete
|
|
|
|
upDateOngoing=false;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},1000 )
|