From 1451a4c5bd75f76109dd7849af1793e9a875030f Mon Sep 17 00:00:00 2001 From: Atreya Bain Date: Sun, 14 Jun 2020 18:25:10 +0530 Subject: [PATCH] Add error handles and getUpdates --- .vscode/launch.json | 9 +++++++++ README.md | 25 +++++++++++++++++++++++++ package.json | 4 ++-- src/index.ts | 38 ++++++++++++++++++++++++++++---------- src/misc/defs.ts | 16 ++++++++++++++++ src/misc/getName.ts | 3 ++- src/tg/getWrapper.ts | 20 ++++++++++++++------ tsconfig.tsbuildinfo | 22 ++++++++++++++++++---- 8 files changed, 114 insertions(+), 23 deletions(-) create mode 100644 README.md diff --git a/.vscode/launch.json b/.vscode/launch.json index 1a752c6..9f1e33a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,15 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { + "name": "Attach", + "port": 9229, + "request": "attach", + "skipFiles": [ + "/**" + ], + "type": "pwa-node" + }, { "type": "node", "request": "launch", diff --git a/README.md b/README.md new file mode 100644 index 0000000..e80ea71 --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# jack + +**indev** - still being made + +Telegram bot. + +## Usage + +*You don't* + +But seriously, this is only for me to keep track of stuff + +```sh +npm i +npm i --only=dev +JACK_TOKEN="u thought" npm start +``` + + +## Goals + + - [X] Read Name + - [ ] Read msgs + - [ ] Deal with msgs + - [ ] Implement response bodies \ No newline at end of file diff --git a/package.json b/package.json index 4b3ef5e..8726c49 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,8 @@ "description": "Bot", "main": "out/index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", - "start": "tsc && node out" + "start": "tsc && node out", + "test": "tsc && node --inspect-brk out" }, "repository": { "type": "git", diff --git a/src/index.ts b/src/index.ts index e3fccd4..8def446 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; } diff --git a/src/misc/defs.ts b/src/misc/defs.ts index 6d48b6e..eb9da53 100644 --- a/src/misc/defs.ts +++ b/src/misc/defs.ts @@ -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:{ diff --git a/src/misc/getName.ts b/src/misc/getName.ts index 5e0bd14..52b9eb8 100644 --- a/src/misc/getName.ts +++ b/src/misc/getName.ts @@ -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]; } } \ No newline at end of file diff --git a/src/tg/getWrapper.ts b/src/tg/getWrapper.ts index 7863ce6..5707210 100644 --- a/src/tg/getWrapper.ts +++ b/src/tg/getWrapper.ts @@ -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{ - const response = await got(requestURL+"/getMe",{responseType:'json'}); +export async function getInit(requestURL: string): Promise { + 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; } \ No newline at end of file diff --git a/tsconfig.tsbuildinfo b/tsconfig.tsbuildinfo index c6e7fe0..8968f30 100644 --- a/tsconfig.tsbuildinfo +++ b/tsconfig.tsbuildinfo @@ -172,8 +172,13 @@ "affectsGlobalScope": false }, "./src/misc/defs.ts": { - "version": "1dd5df1ac4d7645afe992af3ebeb2b9f93c0349cdee22ee7b27e76cba738434f", - "signature": "f1b81c4c55d0482768c1e7628c19488fe4011b8a62fdf367747f9ca83ef48240", + "version": "9ad55bdafc44ffa2fddff87ca5d73e30bea45b488b4e0a919c9451216314ca89", + "signature": "4f3f3fa6f98ebd8edbde568924f43d46dc8543d15985c21f1b6bb559e1e87aa1", + "affectsGlobalScope": false + }, + "./src/misc/getName.ts": { + "version": "6781ba5ea24a074465d581b1eac877340cfd5dde6ec0549ce361a03d67b96076", + "signature": "b791a0a7b15ce7d3dcda9ab7413064c319b6715cdf4b6077bc4259889a2d89d0", "affectsGlobalScope": false }, "./node_modules/@types/node/globals.d.ts": { @@ -517,12 +522,12 @@ "affectsGlobalScope": false }, "./src/tg/getWrapper.ts": { - "version": "a04eee7ae6d6aff550d6f3602ea116ee56d01bb8cfb8c0dbcf04f03b40539519", + "version": "1dfb416a546b6255674ae57cbed3518fb0858a2c9ba444f7c52ad0e6153c50fe", "signature": "f441d45667d0c06a19f51eca34a3a16bc6d844cb653731683c5937b0f645601f", "affectsGlobalScope": false }, "./src/index.ts": { - "version": "88f4239f46e5a5583499d30f6c660a5d1464fcca01e77e9b97df79209271cf65", + "version": "392953e0e221acdfbd58512760ad64b6955af1ddb7bea06955df5d904bb691cf", "signature": "f667f6b1d66ba55f0dcd27f5ff050063fecfe289bad00e8c5d48344a775c1439", "affectsGlobalScope": false }, @@ -1349,6 +1354,7 @@ "./node_modules/@types/node/util.d.ts", "./src/misc/defs.ts", "./src/misc/delay.ts", + "./src/misc/getName.ts", "./src/tg/getWrapper.ts" ], "./src/misc/defs.ts": [ @@ -1364,6 +1370,13 @@ "./node_modules/@types/node/ts3.2/util.d.ts", "./node_modules/@types/node/util.d.ts" ], + "./src/misc/getName.ts": [ + "./node_modules/@types/node/console.d.ts", + "./node_modules/@types/node/fs.d.ts", + "./node_modules/@types/node/ts3.2/fs.d.ts", + "./node_modules/@types/node/ts3.2/util.d.ts", + "./node_modules/@types/node/util.d.ts" + ], "./src/tg/getWrapper.ts": [ "./node_modules/@types/node/fs.d.ts", "./node_modules/@types/node/ts3.2/fs.d.ts", @@ -2269,6 +2282,7 @@ "./src/index.ts", "./src/misc/defs.ts", "./src/misc/delay.ts", + "./src/misc/getName.ts", "./src/tg/getWrapper.ts" ] },