-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
some fixes and implemented getUpdates
- Loading branch information
Showing
26 changed files
with
885 additions
and
661 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export * from "." | ||
export * from "."; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.