@@ -8,6 +8,8 @@ Telegram bot deployed using Heroku.
|
|||||||
|
|
||||||
A test on async await, and typescript. Since this project is IO-bound(mostly), it makes sense to use Node and Typescript.
|
A test on async await, and typescript. Since this project is IO-bound(mostly), it makes sense to use Node and Typescript.
|
||||||
|
|
||||||
|
Special thanks to: [`twss`](https://github.com/DanielRapp/twss.js)
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
*You don't*
|
*You don't*
|
||||||
|
27
package-lock.json
generated
27
package-lock.json
generated
@@ -119,6 +119,11 @@
|
|||||||
"mimic-response": "^1.0.0"
|
"mimic-response": "^1.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"colors": {
|
||||||
|
"version": "1.4.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz",
|
||||||
|
"integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA=="
|
||||||
|
},
|
||||||
"combined-stream": {
|
"combined-stream": {
|
||||||
"version": "1.0.8",
|
"version": "1.0.8",
|
||||||
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
|
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
|
||||||
@@ -265,6 +270,14 @@
|
|||||||
"wrappy": "1"
|
"wrappy": "1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"optimist": {
|
||||||
|
"version": "0.3.7",
|
||||||
|
"resolved": "https://registry.npmjs.org/optimist/-/optimist-0.3.7.tgz",
|
||||||
|
"integrity": "sha1-yQlBrVnkJzMokjB00s8ufLxuwNk=",
|
||||||
|
"requires": {
|
||||||
|
"wordwrap": "~0.0.2"
|
||||||
|
}
|
||||||
|
},
|
||||||
"p-cancelable": {
|
"p-cancelable": {
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/p-cancelable/-/p-cancelable-2.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/p-cancelable/-/p-cancelable-2.0.0.tgz",
|
||||||
@@ -297,12 +310,26 @@
|
|||||||
"lowercase-keys": "^2.0.0"
|
"lowercase-keys": "^2.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"twss": {
|
||||||
|
"version": "0.1.6",
|
||||||
|
"resolved": "https://registry.npmjs.org/twss/-/twss-0.1.6.tgz",
|
||||||
|
"integrity": "sha1-k8sxeYJGQ8gsD5B7b+35fE94leo=",
|
||||||
|
"requires": {
|
||||||
|
"colors": "*",
|
||||||
|
"optimist": "0.3"
|
||||||
|
}
|
||||||
|
},
|
||||||
"typescript": {
|
"typescript": {
|
||||||
"version": "3.9.5",
|
"version": "3.9.5",
|
||||||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.5.tgz",
|
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.5.tgz",
|
||||||
"integrity": "sha512-hSAifV3k+i6lEoCJ2k6R2Z/rp/H3+8sdmcn5NrS3/3kE7+RyZXm9aqvxWqjEXHAd8b0pShatpcdMTvEdvAJltQ==",
|
"integrity": "sha512-hSAifV3k+i6lEoCJ2k6R2Z/rp/H3+8sdmcn5NrS3/3kE7+RyZXm9aqvxWqjEXHAd8b0pShatpcdMTvEdvAJltQ==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
"wordwrap": {
|
||||||
|
"version": "0.0.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.3.tgz",
|
||||||
|
"integrity": "sha1-o9XabNXAvAAI03I0u68b7WMFkQc="
|
||||||
|
},
|
||||||
"wrappy": {
|
"wrappy": {
|
||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
|
||||||
|
@@ -31,6 +31,7 @@
|
|||||||
"typescript": "^3.9.5"
|
"typescript": "^3.9.5"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"got": "^11.3.0"
|
"got": "^11.3.0",
|
||||||
|
"twss": "^0.1.6"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
7
src/@types/twss/index.d.ts
vendored
Normal file
7
src/@types/twss/index.d.ts
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
declare module 'twss'{
|
||||||
|
export let threshold:number;
|
||||||
|
export let algo:string;
|
||||||
|
export function is(arg:string):boolean;
|
||||||
|
export function probability(arg:string):number;
|
||||||
|
export function prob(arg:string):number;
|
||||||
|
}
|
@@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
import { getUpdateResultBody, sendMessage } from '../misc/defs';
|
import { getUpdateResultBody, sendMessage } from '../misc/defs';
|
||||||
|
import twss from 'twss';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -13,33 +13,61 @@ export async function replyStrategy(updates: getUpdateResultBody[]) {
|
|||||||
|
|
||||||
|
|
||||||
const textMessages = updates.filter(e => e.message && e.message.text);
|
const textMessages = updates.filter(e => e.message && e.message.text);
|
||||||
|
|
||||||
//Log stats
|
//Log stats
|
||||||
if (textMessages.length > 0||nonTextMessages.length > 0) {
|
if (textMessages.length > 0 || nonTextMessages.length > 0) {
|
||||||
console.log(`Got: ${textMessages.length} text messages,${nonTextMessages.length} weird stuff`);
|
console.log(`Got: ${textMessages.length} text messages,${nonTextMessages.length} weird stuff`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const textReplies = await replyText(textMessages);
|
const textReplies = await replyText(textMessages);
|
||||||
const nonTextReplies = await replyNonText(nonTextMessages);
|
const nonTextReplies = await replyNonText(nonTextMessages);
|
||||||
|
|
||||||
return [...textReplies,...nonTextReplies];
|
return [...textReplies, ...nonTextReplies];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export async function replyText(updates: getUpdateResultBody[]): Promise<sendMessage[]> {
|
export async function replyText(updates: getUpdateResultBody[]): Promise<sendMessage[]> {
|
||||||
return updates.map(e => {
|
return updates.map(e => {
|
||||||
|
const { text, reply_to_message_id } = getReplyTextSingle(e)
|
||||||
return {
|
return {
|
||||||
chat_id: e.message?.from?.id as number,
|
chat_id: e.message?.from?.id as number,
|
||||||
reply_to_message_id: e.message?.message_id,
|
reply_to_message_id,
|
||||||
text: 'Hello World!'
|
text
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function replyNonText(updates: getUpdateResultBody[]):Promise<sendMessage[]> {
|
export function getReplyTextSingle(update: getUpdateResultBody): { text: string, reply_to_message_id?: number } {
|
||||||
|
//get commands first
|
||||||
|
if ((update.message?.text ?? '').toLowerCase().startsWith('/get')) {
|
||||||
|
const text = update.message?.text ?? '';
|
||||||
|
const caseval = text.split(' ');
|
||||||
|
if (caseval.length > 1) {
|
||||||
|
switch (caseval[1]) {
|
||||||
|
case 'prob':
|
||||||
|
return { text: twss.threshold.toString(), reply_to_message_id: update.message?.message_id }
|
||||||
|
case 'algo':
|
||||||
|
return { text: twss.algo, reply_to_message_id: update.message?.message_id }
|
||||||
|
default:
|
||||||
|
return {text: 'No.', reply_to_message_id: update.message?.message_id }
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return { text: 'No.', reply_to_message_id: update.message?.message_id }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//then a twss :P
|
||||||
|
if (twss.is(update.message?.text ?? '')) {
|
||||||
|
return { text: "That's what she said ( ͡° ͜ʖ ͡°)", reply_to_message_id: update.message?.message_id };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { text: ':D' };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function replyNonText(updates: getUpdateResultBody[]): Promise<sendMessage[]> {
|
||||||
return updates.map(e => {
|
return updates.map(e => {
|
||||||
return {
|
return {
|
||||||
chat_id: e.message?.from?.id as number,
|
chat_id: e.message?.from?.id as number,
|
||||||
text: 'This is useless to me :(',
|
text: 'This is useless to me :(',
|
||||||
reply_to_message_id: e.message?.message_id
|
reply_to_message_id: e.message?.message_id
|
||||||
|
Reference in New Issue
Block a user