Skip to content

Commit

Permalink
some fixes and implemented getUpdates
Browse files Browse the repository at this point in the history
  • Loading branch information
naseif committed Feb 10, 2022
1 parent 31fa347 commit 4371263
Show file tree
Hide file tree
Showing 26 changed files with 885 additions and 661 deletions.
11 changes: 8 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { TelegramAPI } from "./structure/TelegramAPI";

const test = new TelegramAPI("");


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

export class TelegramAPI extends EventEmitter {
private _token: string;
private endpoint: string
constructor(token: string) {
super();
this._token = token;
this.endpoint = "https://api.telegram.org"
private _token: string;
private endpoint: string;

constructor(token: string) {
super();
this._token = token;
if (!token) throw new Error("Invalid token");
this.endpoint = `https://api.telegram.org/bot${this._token}/`;
}

private async sendRequest(apiMethod: string, params?: RequestInit) {
const get = await fetch(`${apiMethod}`, params);
const { result } = await get.json();

return result;
}

async getMe(callback?: (user: IUser) => void): Promise<IUser> {
let result: IUser;
const fetch = await this.sendRequest(this.endpoint + "getMe");
result = fetch;
if (callback) callback(result);
return result;
}

private processUpdates(updates: IUpdate) {}

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

const qs = new URLSearchParams();

private async sendGETRequest(apiMethod: string) {
const get = await fetch(`${this.endpoint}/bot${this._token}/${apiMethod}`)
const { result } = await get.json();

return result
}


async getMe(): Promise<IUser> {
let result: IUser
const fetch = await this.sendGETRequest("getMe");
result = fetch
return result
}

async getUpdates(options?: IUpdateOptions, callback?: (updates: any) => void) {
if (!options) options = {};

return await this.sendGETRequest("getUpdates")
for (const [key, value] of Object.entries(options)) {
qs.append(key, value);
}

}
return await this.sendRequest(this.endpoint + "getUpdates", {
body: qs,
method: "POST",
});
}
}
2 changes: 1 addition & 1 deletion src/structure/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from "."
export * from ".";
59 changes: 29 additions & 30 deletions src/types/ICallbackQuery.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
import { IMessage } from "./IMessage";
import { IUser } from "./IUser";


/**
* This object represents an incoming callback query from a callback button in an inline keyboard. If the button that originated the query was attached to a message sent by the bot, the field message will be present. If the button was attached to a message sent via the bot (in inline mode), the field inline_message_id will be present. Exactly one of the fields data or game_short_name will be present.
*/
export interface ICallbackQuery {
/**
* Unique identifier for this query
*/
id: string,
/**
* Sender
*/
from: IUser
/**
* Optional. Message with the callback button that originated the query. Note that message content and message date will not be available if the message is too old
*/
message?: IMessage
/**
* Optional. Identifier of the message sent via the bot in inline mode, that originated the query.
*/
inline_message_id?: string;
/**
* Global identifier, uniquely corresponding to the chat to which the message with the callback button was sent. Useful for high scores in games.
*/
chat_instance?: string;
/**
* Optional. Data associated with the callback button. Be aware that a bad client can send arbitrary data in this field.
*/
data?: string;
/**
* Optional. Short name of a Game to be returned, serves as the unique identifier for the game
*/
game_short_name?: string;
}
/**
* Unique identifier for this query
*/
id: string;
/**
* Sender
*/
from: IUser;
/**
* Optional. Message with the callback button that originated the query. Note that message content and message date will not be available if the message is too old
*/
message?: IMessage;
/**
* Optional. Identifier of the message sent via the bot in inline mode, that originated the query.
*/
inline_message_id?: string;
/**
* Global identifier, uniquely corresponding to the chat to which the message with the callback button was sent. Useful for high scores in games.
*/
chat_instance?: string;
/**
* Optional. Data associated with the callback button. Be aware that a bad client can send arbitrary data in this field.
*/
data?: string;
/**
* Optional. Short name of a Game to be returned, serves as the unique identifier for the game
*/
game_short_name?: string;
}
163 changes: 81 additions & 82 deletions src/types/IChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,85 +7,84 @@ import { IMessage } from "./IMessage";
* This object represents a chat.
*/
export interface IChat {
/**
* Unique identifier for this chat.
*/
id: number,
/**
* Type of chat, can be either “private”, “group”, “supergroup” or “channel”
*/
type: string;
/**
* Optional. Title, for supergroups, channels and group chats
*/
title?: string;
/**
* Optional. Username, for private chats, supergroups and channels if available
*/
username?: string;
/**
* Optional. First name of the other party in a private chat
*/
first_name?: string;
/**
* Optional. Last name of the other party in a private chat
*/
last_name?: string;
/**
* Optional. Chat photo. Returned only in getChat.
*/
photo?: IChatPhoto;
/**
* Optional. Bio of the other party in a private chat. Returned only in getChat.
*/
bio?: string;
/**
* Optional. True, if privacy settings of the other party in the private chat allows to use tg://user?id=<user_id> links only in chats with the user. Returned only in getChat.
*/
has_private_forwards?: boolean
/**
* Optional. Description, for groups, supergroups and channel chats. Returned only in getChat.
*/
description?: string;
/**
* Optional. Primary invite link, for groups, supergroups and channel chats. Returned only in getChat.
*/
invite_link?: string;
/**
* Optional. The most recent pinned message (by sending date). Returned only in getChat.
*/
pinned_message?: IMessage;
/**
* Optional. Default chat member permissions, for groups and supergroups. Returned only in getChat.
*/
permissions?: IChatPermissions;
/**
* Optional. For supergroups, the minimum allowed delay between consecutive messages sent by each unpriviledged user; in seconds. Returned only in getChat.
*/
slow_mode_delay?: number;
/**
* Optional. The time after which all messages sent to the chat will be automatically deleted; in seconds. Returned only in getChat.
*/
message_auto_delete_time?: number;
/**
* Optional. True, if messages from the chat can't be forwarded to other chats. Returned only in getChat.
*/
has_protected_content?: boolean;
/**
* Optional. For supergroups, name of group sticker set. Returned only in getChat.
*/
sticker_set_name?: string
/**
* Optional. True, if the bot can change the group sticker set. Returned only in getChat.
*/
can_set_sticker_set?: boolean;
/**
* Optional. Unique identifier for the linked chat, i.e. the discussion group identifier for a channel and vice versa; for supergroups and channel chats
*/
linked_chat_id?: number;
/**
* Optional. For supergroups, the location to which the supergroup is connected. Returned only in getChat.
*/
location?: IChatLocation;

}
/**
* Unique identifier for this chat.
*/
id: number;
/**
* Type of chat, can be either “private”, “group”, “supergroup” or “channel”
*/
type: string;
/**
* Optional. Title, for supergroups, channels and group chats
*/
title?: string;
/**
* Optional. Username, for private chats, supergroups and channels if available
*/
username?: string;
/**
* Optional. First name of the other party in a private chat
*/
first_name?: string;
/**
* Optional. Last name of the other party in a private chat
*/
last_name?: string;
/**
* Optional. Chat photo. Returned only in getChat.
*/
photo?: IChatPhoto;
/**
* Optional. Bio of the other party in a private chat. Returned only in getChat.
*/
bio?: string;
/**
* Optional. True, if privacy settings of the other party in the private chat allows to use tg://user?id=<user_id> links only in chats with the user. Returned only in getChat.
*/
has_private_forwards?: boolean;
/**
* Optional. Description, for groups, supergroups and channel chats. Returned only in getChat.
*/
description?: string;
/**
* Optional. Primary invite link, for groups, supergroups and channel chats. Returned only in getChat.
*/
invite_link?: string;
/**
* Optional. The most recent pinned message (by sending date). Returned only in getChat.
*/
pinned_message?: IMessage;
/**
* Optional. Default chat member permissions, for groups and supergroups. Returned only in getChat.
*/
permissions?: IChatPermissions;
/**
* Optional. For supergroups, the minimum allowed delay between consecutive messages sent by each unpriviledged user; in seconds. Returned only in getChat.
*/
slow_mode_delay?: number;
/**
* Optional. The time after which all messages sent to the chat will be automatically deleted; in seconds. Returned only in getChat.
*/
message_auto_delete_time?: number;
/**
* Optional. True, if messages from the chat can't be forwarded to other chats. Returned only in getChat.
*/
has_protected_content?: boolean;
/**
* Optional. For supergroups, name of group sticker set. Returned only in getChat.
*/
sticker_set_name?: string;
/**
* Optional. True, if the bot can change the group sticker set. Returned only in getChat.
*/
can_set_sticker_set?: boolean;
/**
* Optional. Unique identifier for the linked chat, i.e. the discussion group identifier for a channel and vice versa; for supergroups and channel chats
*/
linked_chat_id?: number;
/**
* Optional. For supergroups, the location to which the supergroup is connected. Returned only in getChat.
*/
location?: IChatLocation;
}
Loading

0 comments on commit 4371263

Please sign in to comment.