Skip to content

Commit

Permalink
some more updates
Browse files Browse the repository at this point in the history
  • Loading branch information
naseif committed Feb 21, 2022
1 parent 33c9fa2 commit b659bc4
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 35 deletions.
14 changes: 7 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { TelegramAPI } from "./structure/TelegramAPI";

const test = new TelegramAPI("");

test.on("message", (message) => console.log(message));

(async () => {
await test.getMe((data) => console.log(data));
console.log(
await test.getUpdates({
offset: 100,
timeout: 10,
})
);
// await test.getMe((data) => console.log(data));
// await test.getUpdates({
// offset: 100,
// timeout: 10,
// });
})();
84 changes: 78 additions & 6 deletions src/structure/TelegramAPI.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import EventEmitter from "eventemitter3";
import fetch, { Response, RequestInit } from "node-fetch";
import { URLSearchParams } from "node:url";
import { Message } from "../types/IMessage";
import { IMessage, Message } from "../types/IMessage";
import { IUpdate } from "../types/IUpdate";
import { IUpdateOptions } from "../types/IUpdateOptions";
import { IUser } from "../types/IUser";

export type TelegramEvents =
| "message"
| "edited_message"
| "channel_post"
| "edited_channel_post"
| "callback_query"
| "inline_query"
| "chosen_inline_result"
| "shipping_query"
| "pre_checkout_query"
| "poll"
| "poll_answer";

export class TelegramAPI extends EventEmitter {
private _token: string;
private endpoint: string;
private timeout: any;
private offset: number | undefined;

constructor(token: string) {
super();
Expand All @@ -32,15 +47,54 @@ export class TelegramAPI extends EventEmitter {
return result;
}

private processUpdates(updates: IUpdate) {}
private processUpdates(updates: IUpdate[]) {
updates.forEach((update) => {
this.offset = update.update_id + 1;
let message = update.message;
let editedMessage = update.edited_message;
let channelPost = update.channel_post;
let editedChannelPost = update.edited_channel_post;
let inlineQuery = update.inline_query;
let chosenInlineResult = update.chosen_inline_result;
let callbackQuery = update.callback_query;
let shippingQuery = update.shipping_query;
let preCheckoutQuery = update.pre_checkout_query;
let pollQuery = update.poll;
let pollAnswerQuery = update.poll_answer;

if (message) {
this.emit("message", message);
} else if (editedMessage) {
this.emit("edited_message", editedMessage);
} else if (channelPost) {
this.emit("channel_post", channelPost);
} else if (editedChannelPost) {
this.emit("edited_channel_post", editedChannelPost);
} else if (callbackQuery) {
this.emit("callback_query", callbackQuery);
} else if (inlineQuery) {
this.emit("inline_query", inlineQuery);
} else if (chosenInlineResult) {
this.emit("chosen_inline_result", chosenInlineResult);
} else if (shippingQuery) {
this.emit("shipping_query", shippingQuery);
} else if (preCheckoutQuery) {
this.emit("pre_checkout_query", preCheckoutQuery);
} else if (pollQuery) {
this.emit("poll", pollQuery);
} else if (pollAnswerQuery) {
this.emit("poll_answer", pollAnswerQuery);
}
});
}

async getUpdates(
options?: IUpdateOptions,
callback?: (updates: IUpdate[]) => void
): Promise<IUpdate[]> {
) {
if (!options) {
options = {};
return await this.sendRequest(this.endpoint + "getUpdates");
return this.sendRequest(this.endpoint + "getUpdates").then(callback);
}

const qs = new URLSearchParams();
Expand All @@ -49,9 +103,27 @@ export class TelegramAPI extends EventEmitter {
qs.append(key, value);
}

return await this.sendRequest(this.endpoint + "getUpdates", {
return this.sendRequest(this.endpoint + "getUpdates", {
body: qs,
method: "POST",
});
}).then(callback);
}

startPolling() {
this.getUpdates({ timeout: 10, offset: this.offset })
.then((updates) => {
if (updates !== undefined) this.processUpdates(updates);
return null;
})
.catch((err) => {
throw err;
})
.finally(() => (this.timeout = setTimeout(() => this.startPolling, 100)));
}

stopPolling() {
clearTimeout(this.timeout);
}

async sendMessage() {}
}
2 changes: 1 addition & 1 deletion src/types/ISuccessfulPayment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IOrderInfo } from "../../dist/types/IOrderInfo";
import { IOrderInfo } from "./IOrderInfo";
/**
* This object contains basic information about a successful payment.
*/
Expand Down
42 changes: 21 additions & 21 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{
"compilerOptions": {
"strict": true,
"moduleResolution": "node",
"removeComments": false,
"alwaysStrict": true,
"pretty": true,
"target": "es2021",
"lib": ["ES2021", "ES2015"],
"sourceMap": true,
"inlineSources": true,
"module": "commonjs",
"declaration": true,
"declarationMap": true,
"outDir": "dist",
"incremental": true,
"esModuleInterop": true,
"skipLibCheck": true,
"resolveJsonModule": true
},
"include": ["src/**/**/*.ts"]
}
"compilerOptions": {
"strict": true,
"moduleResolution": "node",
"removeComments": false,
"alwaysStrict": true,
"pretty": true,
"target": "es2021",
"lib": ["ES2021", "ES2015"],
"sourceMap": true,
"inlineSources": true,
"module": "commonjs",
"declaration": true,
"declarationMap": true,
"outDir": "dist",
"incremental": true,
"esModuleInterop": true,
"skipLibCheck": true,
"resolveJsonModule": true
},
"include": ["src/**/**/*.ts"]
}

0 comments on commit b659bc4

Please sign in to comment.