Better request format, expect only JSON reply
This commit is contained in:
50
index.js
50
index.js
@@ -2,21 +2,57 @@
|
||||
const request = require('request')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
|
||||
//const
|
||||
|
||||
|
||||
const token = JSON.parse( fs.readFileSync('api-token.json')).token
|
||||
let parameters = JSON.parse(fs.readFileSync('default-parameters.json'))
|
||||
|
||||
const base = `https://api.telegram.org/bot${token}/`
|
||||
console.log(`Token: ${token}`)
|
||||
request(`${base}getUpdates`,(err,res,body)=>{
|
||||
|
||||
let updateOffset=-1
|
||||
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;
|
||||
}
|
||||
// If response malformed then move into next
|
||||
let contents = JSON.parse(res.body||{'ok':false})
|
||||
//console.log(res.body+`${typeof(res.body)}`)
|
||||
|
||||
let contents = res.body||{'ok':false}
|
||||
if(!contents.ok){
|
||||
console.log(contents)
|
||||
throw new Error("Not Ok")
|
||||
}
|
||||
console.log(`${JSON.stringify(contents.result)}`)
|
||||
})
|
||||
// contents - Now work on response
|
||||
if(contents.result.length>0){
|
||||
console.log(`Update:${JSON.stringify(contents.result)}`)
|
||||
// Ready to work on
|
||||
contents.result.forEach(e=>{
|
||||
console.log(e.update_id)
|
||||
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
else{
|
||||
console.log("Empty contents")
|
||||
}
|
||||
|
||||
// Allow function to run in next interval since complete
|
||||
upDateOngoing=false;
|
||||
})
|
||||
}
|
||||
},1000 )
|
Reference in New Issue
Block a user