Reset repo
This commit is contained in:
53
src/index.ts
Normal file
53
src/index.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import delay from './misc/delay'
|
||||
import {teleargs,envVars} from './misc/defs'
|
||||
import {getMessage,getInit} from './tg/getWrapper';
|
||||
import { assert } from 'console';
|
||||
|
||||
const token = process.env["JACK_TOKEN"] as string;
|
||||
if(token===undefined){
|
||||
throw new Error("No token");
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
|
||||
loop({token}).catch(reason => {
|
||||
console.error("E:", reason);
|
||||
});
|
||||
}
|
||||
|
||||
async function loop(args:envVars) {
|
||||
const requestURL = 'https://api.telegram.org/bot'+token;
|
||||
const name = await getInit(requestURL);
|
||||
console.info(`NAME:${name}`);
|
||||
const selfName = await getName(name);
|
||||
|
||||
let lastUpdate = 0;
|
||||
|
||||
try {
|
||||
while (true) {
|
||||
//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});
|
||||
await x;
|
||||
}
|
||||
}catch(e){
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param args The arguments
|
||||
* @returns the last update we got
|
||||
*/
|
||||
async function main(args:teleargs) {
|
||||
const actions = await getMessage(args,);
|
||||
// console.debug(actions);
|
||||
|
||||
//assert(actions.body.ok);
|
||||
// const update_id = actions.body.
|
||||
// return the update number
|
||||
return 0;
|
||||
}
|
||||
|
25
src/misc/defs.ts
Normal file
25
src/misc/defs.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { string, boolean } from "yargs"
|
||||
|
||||
export interface teleargs{
|
||||
name:string,
|
||||
token:string,
|
||||
requestURL:string,
|
||||
lastUpdate:number
|
||||
}
|
||||
export interface envVars{
|
||||
token:string,
|
||||
|
||||
}
|
||||
|
||||
export interface initBody{
|
||||
ok:boolean,
|
||||
result:{
|
||||
id:number,
|
||||
is_bot:boolean,
|
||||
first_name:string,
|
||||
username:string,
|
||||
can_join_groups:boolean,
|
||||
can_read_all_group_messages:boolean,
|
||||
supports_inline_queries: boolean
|
||||
}
|
||||
}
|
5
src/misc/delay.ts
Normal file
5
src/misc/delay.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
function delay(ms:number):Promise<number> {
|
||||
return new Promise(resolve => setTimeout(()=>resolve(ms), ms));
|
||||
}
|
||||
|
||||
export default delay;
|
11
src/misc/getName.ts
Normal file
11
src/misc/getName.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { assert } from "console";
|
||||
|
||||
export default async function getName(name:string){
|
||||
const nameSplit = name.split(/\s+/);
|
||||
assert(nameSplit.length>0);
|
||||
if(nameSplit.length===1){
|
||||
return name;
|
||||
}else{
|
||||
return name[0];
|
||||
}
|
||||
}
|
17
src/tg/getWrapper.ts
Normal file
17
src/tg/getWrapper.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import assert from 'assert';
|
||||
import got from 'got';
|
||||
import {teleargs,initBody} from '../misc/defs';
|
||||
|
||||
export async function getMessage(args:teleargs){
|
||||
return got(args.requestURL+'/getUpdates',{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.result.is_bot);
|
||||
return body.result.first_name as string;
|
||||
}
|
Reference in New Issue
Block a user