Add error handles and getUpdates

This commit is contained in:
2020-06-14 18:25:10 +05:30
parent ed8fc1791b
commit 1451a4c5bd
8 changed files with 114 additions and 23 deletions

View File

@@ -1,11 +1,12 @@
import delay from './misc/delay'
import {teleargs,envVars} from './misc/defs'
import {teleargs,envVars,getUpdateBody} from './misc/defs'
import getName from './misc/getName'
import {getMessage,getInit} from './tg/getWrapper';
import { assert } from 'console';
const token = process.env["JACK_TOKEN"] as string;
const token = process.env['JACK_TOKEN'] as string;
if(token===undefined){
throw new Error("No token");
throw new Error('No token');
}
if (require.main === module) {
@@ -15,11 +16,19 @@ if (require.main === module) {
});
}
//Enable this to see cool viz
// setInterval(()=>console.log("10secs"),10000);
async function loop(args:envVars) {
const requestURL = 'https://api.telegram.org/bot'+token;
const name = await getInit(requestURL);
console.info(`NAME:${name}`);
let name:string;
try{
name = await getInit(requestURL);
}catch(e){
throw new Error('Cannot get initial details: '+(e.msg||e.message||e.code||'General Error'));
}
const selfName = await getName(name);
console.info(`NAME:${selfName}`);
let lastUpdate = 0;
@@ -28,11 +37,12 @@ async function loop(args:envVars) {
//basically, check every second at max
let x = delay(1000);
//while we await, we can do other stuff
lastUpdate = await main({name,token,requestURL,lastUpdate});
lastUpdate = await main({name:selfName,token,requestURL,lastUpdate});
await x;
}
}catch(e){
throw e;
console.error("E:",e.msg||e.message||'Error');
setTimeout(loop,5000);
}
}
@@ -42,12 +52,20 @@ async function loop(args:envVars) {
* @returns the last update we got
*/
async function main(args:teleargs) {
const actions = await getMessage(args,);
// console.debug(actions);
console.debug("Logging Update for:",args.lastUpdate);
const actions = await getMessage(args);
const body = actions.body as unknown as getUpdateBody;
assert(body.ok);
assert(body.result instanceof Array);
console.debug(body.result);
//assert(actions.body.ok);
// const update_id = actions.body.
// return the update number
return 0;
const getMaxMsgUpdateID:number = body.result.reduce((acc,curr)=>{
return curr.update_id>acc?curr.update_id:acc;
},args.lastUpdate);
return getMaxMsgUpdateID;
}

View File

@@ -11,6 +11,22 @@ export interface envVars{
}
export interface messageObj{
}
export interface getUpdateResultBody{
update_id:number,
message?:messageObj
}
export interface getUpdateBody{
ok:boolean,
result:getUpdateResultBody[]
}
export interface initBody{
ok:boolean,
result:{

View File

@@ -2,10 +2,11 @@ import { assert } from "console";
export default async function getName(name:string){
const nameSplit = name.split(/\s+/);
console.debug(nameSplit);
assert(nameSplit.length>0);
if(nameSplit.length===1){
return name;
}else{
return name[0];
return nameSplit[0];
}
}

View File

@@ -1,17 +1,25 @@
import assert from 'assert';
import got from 'got';
import {teleargs,initBody} from '../misc/defs';
import { teleargs, initBody } from '../misc/defs';
export async function getMessage(args:teleargs){
return got(args.requestURL+'/getUpdates',{responseType:'json'});
export async function getMessage(args: teleargs) {
const timeout = 50;
console.log("Timeout",timeout);
return got(args.requestURL + '/getUpdates', {
responseType: 'json' ,
searchParams: {
offset: args.lastUpdate+1,
timeout
}
});
}
export async function getInit(requestURL:string):Promise<string>{
const response = await got(requestURL+"/getMe",{responseType:'json'});
export async function getInit(requestURL: string): Promise<string> {
const response = await got(requestURL + "/getMe", { responseType: 'json' });
const body = response.body as unknown as initBody;
//This assertion makes sure we dont get undefined values into the rest of the program
assert(body.ok);
assert(body.ok);
assert(body.result.is_bot);
return body.result.first_name as string;
}