Add error handles and getUpdates
This commit is contained in:
9
.vscode/launch.json
vendored
9
.vscode/launch.json
vendored
@@ -4,6 +4,15 @@
|
|||||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Attach",
|
||||||
|
"port": 9229,
|
||||||
|
"request": "attach",
|
||||||
|
"skipFiles": [
|
||||||
|
"<node_internals>/**"
|
||||||
|
],
|
||||||
|
"type": "pwa-node"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "node",
|
"type": "node",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
|
25
README.md
Normal file
25
README.md
Normal file
@@ -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
|
@@ -4,8 +4,8 @@
|
|||||||
"description": "Bot",
|
"description": "Bot",
|
||||||
"main": "out/index.js",
|
"main": "out/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"start": "tsc && node out",
|
||||||
"start": "tsc && node out"
|
"test": "tsc && node --inspect-brk out"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
38
src/index.ts
38
src/index.ts
@@ -1,11 +1,12 @@
|
|||||||
import delay from './misc/delay'
|
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 {getMessage,getInit} from './tg/getWrapper';
|
||||||
import { assert } from 'console';
|
import { assert } from 'console';
|
||||||
|
|
||||||
const token = process.env["JACK_TOKEN"] as string;
|
const token = process.env['JACK_TOKEN'] as string;
|
||||||
if(token===undefined){
|
if(token===undefined){
|
||||||
throw new Error("No token");
|
throw new Error('No token');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (require.main === module) {
|
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) {
|
async function loop(args:envVars) {
|
||||||
const requestURL = 'https://api.telegram.org/bot'+token;
|
const requestURL = 'https://api.telegram.org/bot'+token;
|
||||||
const name = await getInit(requestURL);
|
let name:string;
|
||||||
console.info(`NAME:${name}`);
|
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);
|
const selfName = await getName(name);
|
||||||
|
console.info(`NAME:${selfName}`);
|
||||||
|
|
||||||
let lastUpdate = 0;
|
let lastUpdate = 0;
|
||||||
|
|
||||||
@@ -28,11 +37,12 @@ async function loop(args:envVars) {
|
|||||||
//basically, check every second at max
|
//basically, check every second at max
|
||||||
let x = delay(1000);
|
let x = delay(1000);
|
||||||
//while we await, we can do other stuff
|
//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;
|
await x;
|
||||||
}
|
}
|
||||||
}catch(e){
|
}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
|
* @returns the last update we got
|
||||||
*/
|
*/
|
||||||
async function main(args:teleargs) {
|
async function main(args:teleargs) {
|
||||||
const actions = await getMessage(args,);
|
console.debug("Logging Update for:",args.lastUpdate);
|
||||||
// console.debug(actions);
|
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);
|
//assert(actions.body.ok);
|
||||||
// const update_id = actions.body.
|
// const update_id = actions.body.
|
||||||
// return the update number
|
// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -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{
|
export interface initBody{
|
||||||
ok:boolean,
|
ok:boolean,
|
||||||
result:{
|
result:{
|
||||||
|
@@ -2,10 +2,11 @@ import { assert } from "console";
|
|||||||
|
|
||||||
export default async function getName(name:string){
|
export default async function getName(name:string){
|
||||||
const nameSplit = name.split(/\s+/);
|
const nameSplit = name.split(/\s+/);
|
||||||
|
console.debug(nameSplit);
|
||||||
assert(nameSplit.length>0);
|
assert(nameSplit.length>0);
|
||||||
if(nameSplit.length===1){
|
if(nameSplit.length===1){
|
||||||
return name;
|
return name;
|
||||||
}else{
|
}else{
|
||||||
return name[0];
|
return nameSplit[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,17 +1,25 @@
|
|||||||
import assert from 'assert';
|
import assert from 'assert';
|
||||||
import got from 'got';
|
import got from 'got';
|
||||||
import {teleargs,initBody} from '../misc/defs';
|
import { teleargs, initBody } from '../misc/defs';
|
||||||
|
|
||||||
export async function getMessage(args:teleargs){
|
export async function getMessage(args: teleargs) {
|
||||||
return got(args.requestURL+'/getUpdates',{responseType:'json'});
|
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>{
|
export async function getInit(requestURL: string): Promise<string> {
|
||||||
const response = await got(requestURL+"/getMe",{responseType:'json'});
|
const response = await got(requestURL + "/getMe", { responseType: 'json' });
|
||||||
const body = response.body as unknown as initBody;
|
const body = response.body as unknown as initBody;
|
||||||
|
|
||||||
//This assertion makes sure we dont get undefined values into the rest of the program
|
//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);
|
assert(body.result.is_bot);
|
||||||
return body.result.first_name as string;
|
return body.result.first_name as string;
|
||||||
}
|
}
|
@@ -172,8 +172,13 @@
|
|||||||
"affectsGlobalScope": false
|
"affectsGlobalScope": false
|
||||||
},
|
},
|
||||||
"./src/misc/defs.ts": {
|
"./src/misc/defs.ts": {
|
||||||
"version": "1dd5df1ac4d7645afe992af3ebeb2b9f93c0349cdee22ee7b27e76cba738434f",
|
"version": "9ad55bdafc44ffa2fddff87ca5d73e30bea45b488b4e0a919c9451216314ca89",
|
||||||
"signature": "f1b81c4c55d0482768c1e7628c19488fe4011b8a62fdf367747f9ca83ef48240",
|
"signature": "4f3f3fa6f98ebd8edbde568924f43d46dc8543d15985c21f1b6bb559e1e87aa1",
|
||||||
|
"affectsGlobalScope": false
|
||||||
|
},
|
||||||
|
"./src/misc/getName.ts": {
|
||||||
|
"version": "6781ba5ea24a074465d581b1eac877340cfd5dde6ec0549ce361a03d67b96076",
|
||||||
|
"signature": "b791a0a7b15ce7d3dcda9ab7413064c319b6715cdf4b6077bc4259889a2d89d0",
|
||||||
"affectsGlobalScope": false
|
"affectsGlobalScope": false
|
||||||
},
|
},
|
||||||
"./node_modules/@types/node/globals.d.ts": {
|
"./node_modules/@types/node/globals.d.ts": {
|
||||||
@@ -517,12 +522,12 @@
|
|||||||
"affectsGlobalScope": false
|
"affectsGlobalScope": false
|
||||||
},
|
},
|
||||||
"./src/tg/getWrapper.ts": {
|
"./src/tg/getWrapper.ts": {
|
||||||
"version": "a04eee7ae6d6aff550d6f3602ea116ee56d01bb8cfb8c0dbcf04f03b40539519",
|
"version": "1dfb416a546b6255674ae57cbed3518fb0858a2c9ba444f7c52ad0e6153c50fe",
|
||||||
"signature": "f441d45667d0c06a19f51eca34a3a16bc6d844cb653731683c5937b0f645601f",
|
"signature": "f441d45667d0c06a19f51eca34a3a16bc6d844cb653731683c5937b0f645601f",
|
||||||
"affectsGlobalScope": false
|
"affectsGlobalScope": false
|
||||||
},
|
},
|
||||||
"./src/index.ts": {
|
"./src/index.ts": {
|
||||||
"version": "88f4239f46e5a5583499d30f6c660a5d1464fcca01e77e9b97df79209271cf65",
|
"version": "392953e0e221acdfbd58512760ad64b6955af1ddb7bea06955df5d904bb691cf",
|
||||||
"signature": "f667f6b1d66ba55f0dcd27f5ff050063fecfe289bad00e8c5d48344a775c1439",
|
"signature": "f667f6b1d66ba55f0dcd27f5ff050063fecfe289bad00e8c5d48344a775c1439",
|
||||||
"affectsGlobalScope": false
|
"affectsGlobalScope": false
|
||||||
},
|
},
|
||||||
@@ -1349,6 +1354,7 @@
|
|||||||
"./node_modules/@types/node/util.d.ts",
|
"./node_modules/@types/node/util.d.ts",
|
||||||
"./src/misc/defs.ts",
|
"./src/misc/defs.ts",
|
||||||
"./src/misc/delay.ts",
|
"./src/misc/delay.ts",
|
||||||
|
"./src/misc/getName.ts",
|
||||||
"./src/tg/getWrapper.ts"
|
"./src/tg/getWrapper.ts"
|
||||||
],
|
],
|
||||||
"./src/misc/defs.ts": [
|
"./src/misc/defs.ts": [
|
||||||
@@ -1364,6 +1370,13 @@
|
|||||||
"./node_modules/@types/node/ts3.2/util.d.ts",
|
"./node_modules/@types/node/ts3.2/util.d.ts",
|
||||||
"./node_modules/@types/node/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": [
|
"./src/tg/getWrapper.ts": [
|
||||||
"./node_modules/@types/node/fs.d.ts",
|
"./node_modules/@types/node/fs.d.ts",
|
||||||
"./node_modules/@types/node/ts3.2/fs.d.ts",
|
"./node_modules/@types/node/ts3.2/fs.d.ts",
|
||||||
@@ -2269,6 +2282,7 @@
|
|||||||
"./src/index.ts",
|
"./src/index.ts",
|
||||||
"./src/misc/defs.ts",
|
"./src/misc/defs.ts",
|
||||||
"./src/misc/delay.ts",
|
"./src/misc/delay.ts",
|
||||||
|
"./src/misc/getName.ts",
|
||||||
"./src/tg/getWrapper.ts"
|
"./src/tg/getWrapper.ts"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user