Skip to content

Commit

Permalink
Initial commit, added types and implemented the TelegramAPI class
Browse files Browse the repository at this point in the history
  • Loading branch information
naseif committed Jan 31, 2022
1 parent 2c67c27 commit 31fa347
Show file tree
Hide file tree
Showing 29 changed files with 1,910 additions and 0 deletions.
1,026 changes: 1,026 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "node-telegram-api",
"version": "1.0.0",
"description": "Telegram Bot API for Node.js written in TypeScript",
"main": "dist/src/main.js",
"scripts": {
"start": "tsc && node dist/main.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/naseif/node-telegram-api.git"
},
"keywords": [
"telegram"
],
"author": "naseif",
"license": "MIT",
"bugs": {
"url": "https://github.com/naseif/node-telegram-api/issues"
},
"homepage": "https://github.com/naseif/node-telegram-api#readme",
"devDependencies": {
"@types/node": "^17.0.13",
"@types/node-fetch": "^2.5.12",
"prettier": "^2.5.1",
"tslint": "^6.1.3",
"typescript": "^4.5.5"
},
"dependencies": {
"eventemitter3": "^4.0.7",
"node-fetch": "^2.6.7"
}
}
8 changes: 8 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { TelegramAPI } from "./structure/TelegramAPI";

const test = new TelegramAPI("");


(async () => {
console.log(await test.getMe())
})()
38 changes: 38 additions & 0 deletions src/structure/TelegramAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import EventEmitter from "eventemitter3"
import fetch, { Response } from "node-fetch"
import { Message } from "../types/IMessage";
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 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")
}

}
1 change: 1 addition & 0 deletions src/structure/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "."
37 changes: 37 additions & 0 deletions src/types/ICallbackQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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;
}
91 changes: 91 additions & 0 deletions src/types/IChat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { IChatLocation } from "./IChatLocation";
import { IChatPermissions } from "./IChatPermission";
import { IChatPhoto } from "./IChatPhoto";
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;

}
43 changes: 43 additions & 0 deletions src/types/IChatInviteLink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { IUser } from "./IUser";

/**
* Represents an invite link for a chat.
*/
export interface IChatInviteLink {
/**
* The invite link. If the link was created by another chat administrator, then the second part of the link will be replaced with “…”.
*/
invite_link: string;
/**
* Creator of the link
*/
creator: IUser;
/**
* True, if users joining the chat via the link need to be approved by chat administrators
*/
creates_join_request: boolean
/**
* True, if the link is primary
*/
is_primary: boolean
/**
* True, if the link is revoked
*/
is_revoked: boolean
/**
* Optional. Invite link name
*/
name?: string;
/**
* Optional. Point in time (Unix timestamp) when the link will expire or has been expired
*/
expire_date?: number;
/**
* Optional. Maximum number of users that can be members of the chat simultaneously after joining the chat via this invite link; 1-99999
*/
member_limit?: number;
/**
* Optional. Number of pending join requests created using this link
*/
pending_join_request_count?: number;
}
30 changes: 30 additions & 0 deletions src/types/IChatJoinRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { IChat } from "./IChat";
import { IChatInviteLink } from "./IChatInviteLink";
import { IUser } from "./IUser";

/**
* Represents a join request sent to a chat.
*/

export interface IChatJoinRequest {
/**
* Chat to which the request was sent
*/
chat: IChat
/**
* User that sent the join request
*/
from: IUser;
/**
* Date the request was sent in Unix time
*/
date: number;
/**
* Optional. Bio of the user.
*/
bio?: string;
/**
* Optional. Chat invite link that was used by the user to send the join request
*/
invite_link?: IChatInviteLink
}
15 changes: 15 additions & 0 deletions src/types/IChatLocation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ILocation } from "./ILocation";

/**
* Represents a location to which a chat is connected.
*/
export interface IChatLocation {
/**
* The location to which the supergroup is connected. Can't be a live location.
*/
location: ILocation;
/**
* Location address; 1-64 characters, as defined by the chat owner
*/
address: string
}
39 changes: 39 additions & 0 deletions src/types/IChatPermission.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Describes actions that a non-administrator user is allowed to take in a chat.
*/

export interface IChatPermissions {
/**
* Optional. True, if the user is allowed to send text messages, contacts, locations and venues
*/
can_send_messages?: boolean
/**
* Optional. True, if the user is allowed to send audios, documents, photos, videos, video notes and voice notes, implies can_send_messages
*/
can_send_media_messages?: boolean
/**
* Optional. True, if the user is allowed to send polls, implies can_send_messages
*/
can_send_polls?: boolean
/**
* Optional. True, if the user is allowed to send animations, games, stickers and use inline bots, implies can_send_media_messages
*/
can_send_other_messages?: boolean
/**
* Optional. True, if the user is allowed to add web page previews to their messages, implies can_send_media_messages
*/
can_add_web_page_previews?: boolean
/**
* Optional. True, if the user is allowed to change the chat title, photo and other settings. Ignored in public supergroups
*/
can_change_info?: boolean
/**
* Optional. True, if the user is allowed to invite new users to the chat
*/
can_invite_users?: boolean
/**
* Optional. True, if the user is allowed to pin messages. Ignored in public supergroups
*/
can_pin_messages?: boolean

}
22 changes: 22 additions & 0 deletions src/types/IChatPhoto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* This object represents a chat photo.
*/

export interface IChatPhoto {
/**
* File identifier of small (160x160) chat photo. This file_id can be used only for photo download and only for as long as the photo is not changed.
*/
small_file_id: string;
/**
* Unique file identifier of small (160x160) chat photo, which is supposed to be the same over time and for different bots. Can't be used to download or reuse the file.
*/
small_file_unique_id: string;
/**
* File identifier of big (640x640) chat photo. This file_id can be used only for photo download and only for as long as the photo is not changed.
*/
big_file_id: string;
/**
* Unique file identifier of big (640x640) chat photo, which is supposed to be the same over time and for different bots. Can't be used to download or reuse the file.
*/
big_file_unique_id: string;
}
Loading

0 comments on commit 31fa347

Please sign in to comment.