Reset repo
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
{
|
||||
"env": {
|
||||
"commonjs": true,
|
||||
"es6": true,
|
||||
"node": true
|
||||
},
|
||||
"extends": "eslint:recommended",
|
||||
"globals": {
|
||||
"Atomics": "readonly",
|
||||
"SharedArrayBuffer": "readonly"
|
||||
},
|
||||
"parserOptions": {
|
||||
"ecmaVersion": 2018
|
||||
},
|
||||
"rules": {
|
||||
}
|
||||
}
|
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,5 @@
|
||||
|
||||
out/
|
||||
api-token.json
|
||||
# Logs
|
||||
logs
|
||||
|
21
.vscode/launch.json
vendored
Normal file
21
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "node",
|
||||
"request": "launch",
|
||||
"name": "Launch Program",
|
||||
"skipFiles": [
|
||||
"<node_internals>/**"
|
||||
],
|
||||
"preLaunchTask": "tsc: build - tsconfig.json",
|
||||
"program": "${workspaceFolder}/out/index.js",
|
||||
"outFiles": [
|
||||
"${workspaceFolder}/**/*.js"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
14
.vscode/tasks.json
vendored
Normal file
14
.vscode/tasks.json
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"version": "2.0.0",
|
||||
"tasks": [
|
||||
{
|
||||
"type": "typescript",
|
||||
"tsconfig": "tsconfig.json",
|
||||
"problemMatcher": [
|
||||
"$tsc"
|
||||
],
|
||||
"group": "build",
|
||||
"label": "tsc: build - tsconfig.json"
|
||||
}
|
||||
]
|
||||
}
|
24
README.md
24
README.md
@@ -1,24 +0,0 @@
|
||||
# jack-red
|
||||
|
||||
Telegram bot for various things. Uses Telegram api, does stuff.
|
||||
|
||||
`npm i`
|
||||
|
||||
`npm test`
|
||||
|
||||
## To Do
|
||||
|
||||
- [X] Links
|
||||
- [ ] ???
|
||||
|
||||
### Note
|
||||
|
||||
More unstable than a stack of cards.
|
||||
Uses long-ish polling. If you dont like the timeout, increase it.
|
||||
`api-token.json` holds the bot api token
|
||||
|
||||
## Current point of existence
|
||||
|
||||
`!magnet <search>`
|
||||
|
||||
Will search and return magnet links for corresponding search term
|
@@ -1,3 +0,0 @@
|
||||
{
|
||||
"token":""
|
||||
}
|
@@ -1,8 +0,0 @@
|
||||
{
|
||||
"offset":null,
|
||||
"limit":100,
|
||||
"timeout":60,
|
||||
"allowed_updates":[
|
||||
"message","edited_message"
|
||||
]
|
||||
}
|
62
index.js
62
index.js
@@ -1,62 +0,0 @@
|
||||
// TOREAD: https://core.telegram.org/bots/api
|
||||
const request = require('request')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const respond = require('./scripts/respond')
|
||||
|
||||
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}`)
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
let contents = res.body||{'ok':false}
|
||||
if(!contents.ok){
|
||||
console.log(contents)
|
||||
throw new Error("Not Ok")
|
||||
}
|
||||
// contents - Now work on response
|
||||
if(contents.result.length>0){
|
||||
// Ready to work on
|
||||
if(parameters.offset===null){
|
||||
parameters.offset = contents.result[0].update_id + 1
|
||||
}
|
||||
contents.result.forEach(e=>{
|
||||
if(e.update_id + 1 > parameters.offset)
|
||||
{
|
||||
parameters.offset = e.update_id + 1
|
||||
}
|
||||
//console.log(e.update_id)
|
||||
respond.call(base,e)
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
else{
|
||||
console.log("Polling Timed out, empty.")
|
||||
}
|
||||
|
||||
// Allow function to run in next interval since complete
|
||||
upDateOngoing=false;
|
||||
})
|
||||
}
|
||||
},1000 )
|
2422
package-lock.json
generated
2422
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
27
package.json
27
package.json
@@ -1,26 +1,33 @@
|
||||
{
|
||||
"name": "jack-red",
|
||||
"version": "1.0.0",
|
||||
"description": "Bot in Telegram",
|
||||
"main": "index.js",
|
||||
"version": "0.1.0",
|
||||
"description": "Bot",
|
||||
"main": "out/index.js",
|
||||
"scripts": {
|
||||
"test": "node index"
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "tsc && node out"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://chrisvrose@github.com/chrisvrose/jack-red.git"
|
||||
"url": "git+https://github.com/chrisvrose/jack-red.git"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"keywords": [
|
||||
"bot",
|
||||
"ts"
|
||||
],
|
||||
"author": "Atreya Bain",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/chrisvrose/jack-red/issues"
|
||||
},
|
||||
"homepage": "https://github.com/chrisvrose/jack-red#readme",
|
||||
"devDependencies": {
|
||||
"eslint": "^5.15.1"
|
||||
"@types/got": "^9.6.11",
|
||||
"@types/node": "^14.0.13",
|
||||
"@types/yargs": "^15.0.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"request": "^2.88.0",
|
||||
"torrent-search-api": "^2.0.9"
|
||||
"got": "^11.3.0",
|
||||
"yargs": "^15.3.1"
|
||||
}
|
||||
}
|
||||
|
@@ -1,50 +0,0 @@
|
||||
const tsa = require('torrent-search-api')
|
||||
tsa.enableProvider('ThePirateBay')
|
||||
// Make a response e
|
||||
|
||||
//Check if can be responded to
|
||||
exports.canRespond = msg=>{
|
||||
//let words = msg.split(" ")
|
||||
return('!'==msg[0])
|
||||
}
|
||||
|
||||
exports.getCommand = msg=>{
|
||||
let words = msg.split(" ")
|
||||
return words[0].substring(1)
|
||||
}
|
||||
|
||||
exports.getPredicate = msg=>{
|
||||
let pred = msg.split(" ")
|
||||
pred.shift();
|
||||
return pred.join(" ")
|
||||
}
|
||||
|
||||
|
||||
|
||||
let magnet = async searchterm=>{
|
||||
console.log(`Search for:${searchterm}`)
|
||||
let result = await tsa.search(searchterm,'All',5)
|
||||
let resultString=''
|
||||
for(let i=0;i<3&&i<result.length;i++){
|
||||
let magnet = await tsa.getMagnet(result[i]) || ' '
|
||||
resultString = resultString + `${result[i].title.replace(/[\[\]]/g,'')} - \n${magnet}\n\n`
|
||||
}
|
||||
console.log(resultString)
|
||||
//console.log(await tsa.getMagnet(result[0]))
|
||||
return resultString
|
||||
}
|
||||
|
||||
|
||||
// The main function, calls the rest
|
||||
exports.makeResponse = async (text)=>{
|
||||
let command = this.getCommand(text)
|
||||
let pred = this.getPredicate(text)
|
||||
let resultString=''
|
||||
if(command=="magnet"){
|
||||
resultString = await magnet(pred);
|
||||
}
|
||||
else{
|
||||
resultString = pred
|
||||
}
|
||||
return resultString
|
||||
}
|
@@ -1,20 +0,0 @@
|
||||
// Obtain a sendMessage Object
|
||||
const makeResponse = require('./makeResponse')
|
||||
|
||||
module.exports = async (messageObject)=>{
|
||||
if(makeResponse.canRespond(messageObject.text)){
|
||||
//console.log(makeResponse.getCommand(messageObject.text))
|
||||
let answer = {
|
||||
"chat_id": messageObject.from.id,
|
||||
"text": await makeResponse.makeResponse( messageObject.text ),
|
||||
//"parse_mode": "Markdown",
|
||||
"reply_to_message_id": messageObject.message_id
|
||||
}
|
||||
console.log("Made up reply")
|
||||
return answer
|
||||
|
||||
}
|
||||
else{
|
||||
return null
|
||||
}
|
||||
}
|
@@ -1,23 +0,0 @@
|
||||
// Send update object for processing and request to send reply
|
||||
|
||||
const process = require('./process')
|
||||
const request = require('request')
|
||||
|
||||
module.exports.call = async (base,updateObject)=>{
|
||||
if('message' in updateObject){
|
||||
let responseObject = await process(updateObject.message)
|
||||
//console.log(process.process(updateObject.message))
|
||||
if(responseObject)
|
||||
{
|
||||
request.post({
|
||||
"url":`${base}sendMessage`,
|
||||
"json":true,
|
||||
"body":responseObject
|
||||
},(err,res,body)=>{
|
||||
if(err) console.log(err)
|
||||
if(body.ok) console.log(`Successfully sent: ${body}`)
|
||||
else console.log(responseObject)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
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;
|
||||
}
|
70
tsconfig.json
Normal file
70
tsconfig.json
Normal file
@@ -0,0 +1,70 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
/* Visit https://aka.ms/tsconfig.json to read more about this file */
|
||||
|
||||
/* Basic Options */
|
||||
"incremental": true, /* Enable incremental compilation */
|
||||
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
|
||||
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
|
||||
// "lib": [], /* Specify library files to be included in the compilation. */
|
||||
// "allowJs": true, /* Allow javascript files to be compiled. */
|
||||
// "checkJs": true, /* Report errors in .js files. */
|
||||
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
|
||||
"declaration": true, /* Generates corresponding '.d.ts' file. */
|
||||
"declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
|
||||
"sourceMap": true, /* Generates corresponding '.map' file. */
|
||||
// "outFile": "./", /* Concatenate and emit output to single file. */
|
||||
"outDir": "./out", /* Redirect output structure to the directory. */
|
||||
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
|
||||
// "composite": true, /* Enable project compilation */
|
||||
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
|
||||
"removeComments": true, /* Do not emit comments to output. */
|
||||
// "noEmit": true, /* Do not emit outputs. */
|
||||
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
|
||||
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
|
||||
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
|
||||
|
||||
/* Strict Type-Checking Options */
|
||||
"strict": true, /* Enable all strict type-checking options. */
|
||||
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
|
||||
// "strictNullChecks": true, /* Enable strict null checks. */
|
||||
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
|
||||
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
|
||||
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
|
||||
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
|
||||
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
|
||||
|
||||
/* Additional Checks */
|
||||
// "noUnusedLocals": true, /* Report errors on unused locals. */
|
||||
// "noUnusedParameters": true, /* Report errors on unused parameters. */
|
||||
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
|
||||
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
|
||||
|
||||
/* Module Resolution Options */
|
||||
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
|
||||
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
|
||||
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
|
||||
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
|
||||
// "typeRoots": [], /* List of folders to include type definitions from. */
|
||||
// "types": [], /* Type declaration files to be included in compilation. */
|
||||
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
|
||||
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
|
||||
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
|
||||
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
||||
|
||||
/* Source Map Options */
|
||||
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
|
||||
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
||||
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
|
||||
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
|
||||
|
||||
/* Experimental Options */
|
||||
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
|
||||
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
|
||||
|
||||
/* Advanced Options */
|
||||
"skipLibCheck": true, /* Skip type checking of declaration files. */
|
||||
"forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */
|
||||
"resolveJsonModule": true
|
||||
}
|
||||
}
|
2276
tsconfig.tsbuildinfo
Normal file
2276
tsconfig.tsbuildinfo
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user