-
Notifications
You must be signed in to change notification settings - Fork 0
/
dts.test.ts
112 lines (99 loc) · 3.85 KB
/
dts.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import type * as Telegram from "telegram-bot-api-types";
import {HttpsProxyAgent} from 'https-proxy-agent';
import type {Response} from 'node-fetch';
import fetch from 'node-fetch';
import * as fs from 'node:fs';
import * as process from 'node:process';
const {token} = JSON.parse(fs.readFileSync('test/config.json', 'utf8'));
const agent = new HttpsProxyAgent(process.env.HTTPS_PROXY || process.env.https_proxy || '');
class APIClientBase {
readonly token: string;
readonly baseURL: string = `https://api.telegram.org`;
constructor(token: string, baseURL?: string) {
this.token = token;
if (baseURL) {
this.baseURL = baseURL;
}
while (this.baseURL.endsWith('/')) {
this.baseURL = this.baseURL.slice(0, -1);
}
}
request<T>(method: Telegram.BotMethod, params: T): Promise<Response> {
for (const key in params) {
if (params[key] instanceof File || params[key] instanceof Blob) {
return this.formDataRequest(method, params);
}
}
return this.jsonRequest(method, params);
}
async requestJSON<T, R>(method: Telegram.BotMethod, params: T): Promise<R> {
return this.request(method, params).then(res => res.json() as R)
}
private uri(method: Telegram.BotMethod): string {
return `${this.baseURL}/bot${this.token}/${method}`
}
private jsonRequest<T>(method: Telegram.BotMethod, params: T): Promise<Response> {
return fetch(this.uri(method), {
agent, // Disable this line if you don't need proxy
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(params),
});
}
private formDataRequest<T>(method: Telegram.BotMethod, params: T): Promise<Response> {
const formData = new FormData();
for (const key in params) {
const value = params[key];
if (value instanceof File) {
formData.append(key, value, value.name);
} else if (value instanceof Blob) {
formData.append(key, value, 'blob');
} else if (typeof value === 'string') {
formData.append(key, value);
} else {
formData.append(key, JSON.stringify(value));
}
}
return fetch(this.uri(method), {
method: 'POST',
body: formData,
});
}
}
type APIClient = APIClientBase & Telegram.AllBotMethods;
export function createAPIClient(token: string): APIClient {
const client = new APIClientBase(token);
return new Proxy(client, {
get(target, prop, receiver) {
if (prop in target) {
return Reflect.get(target, prop, receiver);
}
return (...args: any[]) => {
if (typeof prop === 'string' && prop.endsWith('WithReturns')) {
const method = prop.slice(0, -11) as Telegram.BotMethod;
return Reflect.apply(target.requestJSON, target, [method, ...args]);
}
return Reflect.apply(target.request, target, [prop as Telegram.BotMethod, ...args]);
};
}
}) as APIClient;
}
async function main() {
const client = createAPIClient(token);
const {result: user} = await client.getMeWithReturns();
console.log(`Hello! My name is ${user.username}`);
await client.deleteWebhook();
let offset = 0
while (true) {
const {result: updates} = await client.getUpdatesWithReturns({offset: offset});
for (const update of updates) {
offset = update.update_id + 1;
if (update.message?.text) {
await client.sendMessageWithReturns({chat_id: update.message.chat.id, text: update.message.text});
}
}
}
}
main().catch(console.error);