From 7d7af08ca19f904cf346d8cb74dd492718a12510 Mon Sep 17 00:00:00 2001 From: azkadev Date: Tue, 8 Feb 2022 07:02:23 +0700 Subject: [PATCH] update --- node-js/tdl-lib/package.json | 4 +- node-js/tdl-lib/src/js/main.js | 30 +- node-js/tdl-lib/src/js/telegram.js | 540 ++++++++++++++++++++------ node-js/tdl-lib/src/js/update.js | 83 +++- node-js/telegram_client/package.json | 2 +- node-js/telegram_client/src/js/api.js | 132 ++++++- 6 files changed, 621 insertions(+), 170 deletions(-) diff --git a/node-js/tdl-lib/package.json b/node-js/tdl-lib/package.json index 7ec372c8..fcd866fe 100644 --- a/node-js/tdl-lib/package.json +++ b/node-js/tdl-lib/package.json @@ -1,13 +1,13 @@ { "name": "tdl-lib", - "version": "0.0.27", + "version": "0.0.60", "description": "", "main": "src/js/index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], - "author": "", + "author": "azkadev", "license": "ISC", "dependencies": { "tdl": "^7.0.0", diff --git a/node-js/tdl-lib/src/js/main.js b/node-js/tdl-lib/src/js/main.js index 8fb4cf4d..6fb0a826 100644 --- a/node-js/tdl-lib/src/js/main.js +++ b/node-js/tdl-lib/src/js/main.js @@ -1,12 +1,17 @@ var { Client } = require("tdl"); var { TDLib } = require("tdl-tdlib-addon"); -var lib = require("./index"); var folder = process.cwd(); class telegram { - constructor(api_id, api_hash, pathDb = "./client/", pathTdLib = "./libtdjson.so") { + constructor(pathDb = "./client/", client= {}) { + if (typeof client != "object"){ + client = {}; + } + if (!client){ + client = {}; + } var option = { - "apiId": api_id, - "apiHash": api_hash, + "apiId": 1917085, + "apiHash": "a612212e6ac3ff1f97a99b2e0f050894", "databaseDirectory": `${folder}/${pathDb}`, "filesDirectory": `${folder}/${pathDb}`, "skipOldUpdates": true, @@ -15,11 +20,14 @@ class telegram { "enable_storage_optimizer": true, "system_language_code": 'en', "application_version": "v1", - "device_model": "node js", + "device_model": "Desktop", "system_version": "v5" - } + }, + "pathTdLib": `${folder}/./libtdjson.so`, + ...client }; - var tdlib = new TDLib(`${folder}/${pathTdLib}`); + var tdlib = new TDLib(option["pathTdLib"]); + this.option = option; this.client = new Client(tdlib, option); } @@ -32,13 +40,13 @@ class telegram { this.client.connect().catch(e => { console.log(e.message); return false; - }) + }); this.client.login(() => { type: 'user' }).catch(e => { console.log(e.message); return false; - }) + }); return true; } catch (e) { console.log(e.message); @@ -60,13 +68,15 @@ class telegram { async on(type, callback) { if (RegExp("^update$", "i").exec(type)) { var clients = this.client; + var option = this.option; this.client.on("update", async function (updateTd) { var lib = require("./index"); var tg = new lib.telegramApi(clients); var updateApi = new lib.updateApi(tg); var update = await updateApi.update(updateTd); - return callback(update, updateTd); + return callback(update, updateTd, tg, option); }); + } if (RegExp("^raw$", "i").exec(type)) { this.client.on("update", async function (updateTd) { diff --git a/node-js/tdl-lib/src/js/telegram.js b/node-js/tdl-lib/src/js/telegram.js index b8a44b0b..65a59107 100644 --- a/node-js/tdl-lib/src/js/telegram.js +++ b/node-js/tdl-lib/src/js/telegram.js @@ -1,9 +1,10 @@ var { updateApi } = require("./update"); - +var timers = require("timers/promises"); class telegramApi { - constructor(handle) { - this.handle = handle; + constructor(client, on_update = false) { + this.handle = client; + this.on_update = on_update; } async request(method, option = {}) { @@ -32,6 +33,28 @@ class telegramApi { } } + + if (RegExp(`^promoteChatMember$`, "i").exec(method)) { + var optionparam = { + "custom_title": option["custom_title"], + "can_manage_chat": option["can_manage_chat"], + "can_post_messages": option["can_post_messages"], + "can_edit_messages": option["can_edit_messages"], + "can_delete_messages": option["can_delete_messages"], + "can_manage_voice_chats": option["can_manage_voice_chats"], + "can_restrict_members": option["can_restrict_members"], + "can_promote_members": option["can_promote_members"], + "can_change_info": option["can_change_info"], + "can_invite_users": option["can_invite_users"], + "can_pin_messages": option["can_pin_messages"] + }; + return await this.promoteChatMember(option["chat_id"], option["user_id"], optionparam); + } + + if (RegExp(`^banChatMember$`, "i").exec(method)) { + return await this.banChatMember(option["chat_id"], option["user_id"], option["banned_until_date"], option["revoke_messages"]); + } + if (RegExp(`^deleteMessage$`, "i").exec(method)) { return await this.deleteMessage(option["chat_id"], option["message_id"], true); } @@ -124,7 +147,7 @@ class telegramApi { } if (new RegExp(`^sendMessage$`, "i").exec(method)) { - return await this.sendMessage(option["chat_id"], option["text"], option["parse_mode"], option["entities"], option["disable_web_page_preview"], option["disable_notification"], option["reply_to_message_id"], option["reply_markup"]) + return await this.sendMessage(option["chat_id"], option["text"], option["parse_mode"], option["entities"], option["disable_web_page_preview"], option["disable_notification"], option["reply_to_message_id"], option["reply_markup"]); } if (new RegExp(`^sendPhoto$`, "i").exec(method)) { @@ -198,6 +221,9 @@ class telegramApi { if (new RegExp(`^editMessageText$`, "i").exec(method)) { return await this.editMessageText(option["chat_id"], option["message_id"], option["text"], option["parse_mode"], option["entities"], option["disable_web_page_preview"], option["reply_markup"]) } + if (new RegExp(`^editMessageReplyMarkup$`, "i").exec(method)) { + return await this.editMessageReplyMarkup(option["chat_id"], option["message_id"], option["reply_markup"]) + } if (new RegExp(`^forwardMessage$`, "i").exec(method)) { return await this.forwardMessage(option["chat_id"], option["from_chat_id"], option["message_id"], false); @@ -208,47 +234,23 @@ class telegramApi { } if (new RegExp(`^getChats$`, "i").exec(method)) { - var getchats = await this.getChats() - - if (new RegExp("^chats$", "i").exec(getchats["_"])) { - var json = {}; - if (getchats["total_count"] > 0) { - json["total_count"] = getchats["total_count"]; - var array_data = [] - for (var i = 0; i < getchats["chat_ids"].length; i++) { - var loop_data = getchats["chat_ids"][i]; - var json_loop = {}; - if (new RegExp("-.*", "i").exec(loop_data)) { - try { - var data = { - "chat_id": loop_data - }; - var getSupergroup = await this.request("getSupergroup", data); - if (getSupergroup["ok"]) { - json_loop["id"] = loop_data; - var result = getSupergroup["result"]; - if (result["username"]) { - json_loop["username"] = result["username"]; - } - if (result["type"]) { - json_loop["type"] = result["type"]; - } - json_loop["detail"] = result["detail"]; - } - } catch (e) { - - } - } - array_data.push(json_loop); - } - json["data"] = array_data; - return { "ok": true, "result": json }; - } else { - return { "ok": false }; + var getchats = await this.getChats(); + var list_chats = []; + for (var index = 0; index < getchats["chat_ids"].length; index++) { + var loop_data = getchats["chat_ids"][index]; + try { + var result = await this.request("getChat", { + "chat_id": loop_data + }); + list_chats.push(result["result"]); + } catch (e) { } - } else { - return { "ok": false }; } + return { + "status_code": 200, + "status_bool": true, + "result": list_chats + }; } if (new RegExp(`^getSupergroupMembers$`, "i").exec(method)) { @@ -290,13 +292,13 @@ class telegramApi { json["language_code"] = getUser["language_code"]; } json["detail"] = { - "contact": getUser["is_contact"], - "mutual": getUser["is_mutual_contact"], - "verified": getUser["is_verified"], - "support": getUser["is_support"], - "scam": getUser["is_scam"], - "fake": getUser["is_fake"], - "acces": getUser["have_access"] + "is_contact": getUser["is_contact"], + "is_mutual_contact": getUser["is_mutual_contact"], + "is_verified": getUser["is_verified"], + "is_support": getUser["is_support"], + "is_scam": getUser["is_scam"], + "is_fake": getUser["is_fake"], + "have_acces": getUser["have_access"] }; array.push(json); } @@ -418,13 +420,13 @@ class telegramApi { json["language_code"] = getMe["language_code"]; } json["detail"] = { - "contact": getMe["is_contact"], - "mutual": getMe["is_mutual_contact"], - "verified": getMe["is_verified"], - "support": getMe["is_support"], - "scam": getMe["is_scam"], - "fake": getMe["is_fake"], - "acces": getMe["have_access"] + "is_contact": getMe["is_contact"], + "is_mutual_contact": getMe["is_mutual_contact"], + "is_verified": getMe["is_verified"], + "is_support": getMe["is_support"], + "is_scam": getMe["is_scam"], + "is_fake": getMe["is_fake"], + "have_acces": getMe["have_access"] }; return { "ok": true, "result": json }; } else { @@ -464,14 +466,27 @@ class telegramApi { json["language_code"] = getUser["language_code"]; } json["detail"] = { - "contact": getUser["is_contact"], - "mutual": getUser["is_mutual_contact"], - "verified": getUser["is_verified"], - "support": getUser["is_support"], - "scam": getUser["is_scam"], - "fake": getUser["is_fake"], - "acces": getUser["have_access"] + "is_contact": getUser["is_contact"], + "is_mutual_contact": getUser["is_mutual_contact"], + "is_verified": getUser["is_verified"], + "is_support": getUser["is_support"], + "is_scam": getUser["is_scam"], + "is_fake": getUser["is_fake"], + "have_acces": getUser["have_access"] }; + try { + var getUserFullInfo = await this.getUserFullInfo(option["chat_id"]); + if (typeof getUserFullInfo == "object") { + if (getUserFullInfo["bio"]) { + json["bio"] = getUserFullInfo["bio"]; + } + if (typeof getUserFullInfo["photo"] == "object" && getUserFullInfo["photo"].length > 0) { + json["profile_photo"] = getUserFullInfo["photo"][getUserFullInfo["photo"].length - 1]["file_id"]; + } + } + } catch (e) { + + } return { "ok": true, "result": json }; } else { return { "ok": false, "result": getUser }; @@ -496,6 +511,9 @@ class telegramApi { json["type"] = "channel"; json["detail"] = { "member_count": getSupergroup["member_count"], + "administrator_count": 0, + "restricted_count": 0, + "banned_count": 0, "has_linked_chat": getSupergroup["has_linked_chat"], "has_location": getSupergroup["has_location"], "sign_messages": getSupergroup["sign_messages"], @@ -505,6 +523,20 @@ class telegramApi { "is_scam": getSupergroup["is_scam"], "is_fake": getSupergroup["is_fake"] }; + + try { + var getSupergroupFullInfo = await this.getSupergroupFullInfo(option["chat_id"]); + json["member_count"] = getSupergroupFullInfo["member_count"]; + json["administrator_count"] = getSupergroupFullInfo["administrator_count"]; + json["restricted_count"] = getSupergroupFullInfo["restricted_count"]; + json["banned_count"] = getSupergroupFullInfo["banned_count"]; + if (typeof getSupergroupFullInfo["photo"] == "object" && typeof getSupergroupFullInfo["photo"]["sizes"] == "object" && getSupergroupFullInfo["photo"]["sizes"].length > 0) { + var getSupergroupPhotos = getSupergroupFullInfo["photo"]["sizes"]; + json["profile_photo"] = getSupergroupPhotos[getSupergroupPhotos.length - 1]["photo"]["remote"]["id"]; + } + } catch (e) { + + } return { ok: true, result: json }; } else if (RegExp("supergroup", "i").exec(type_chat)) { var getSupergroup = await this.getSupergroup(option["chat_id"]); @@ -520,6 +552,9 @@ class telegramApi { json["type"] = "supergroup"; json["detail"] = { "member_count": getSupergroup["member_count"], + "administrator_count": 0, + "restricted_count": 0, + "banned_count": 0, "has_linked_chat": getSupergroup["has_linked_chat"], "has_location": getSupergroup["has_location"], "sign_messages": getSupergroup["sign_messages"], @@ -529,6 +564,19 @@ class telegramApi { "is_scam": getSupergroup["is_scam"], "is_fake": getSupergroup["is_fake"] }; + try { + var getSupergroupFullInfo = await this.getSupergroupFullInfo(option["chat_id"]); + json["detail"]["member_count"] = getSupergroupFullInfo["member_count"]; + json["detail"]["administrator_count"] = getSupergroupFullInfo["administrator_count"]; + json["detail"]["restricted_count"] = getSupergroupFullInfo["restricted_count"]; + json["detail"]["banned_count"] = getSupergroupFullInfo["banned_count"]; + if (typeof getSupergroupFullInfo["photo"] == "object" && typeof getSupergroupFullInfo["photo"]["sizes"] == "object" && getSupergroupFullInfo["photo"]["sizes"].length > 0) { + var getSupergroupPhotos = getSupergroupFullInfo["photo"]["sizes"]; + json["profile_photo"] = getSupergroupPhotos[getSupergroupPhotos.length - 1]["photo"]["remote"]["id"]; + } + } catch (e) { + + } return { ok: true, result: json }; } else if (RegExp("BasicGroup", "i").exec(type_chat)) { var getBasicGroup = await this.getBasicGroup(option["chat_id"]); @@ -540,8 +588,17 @@ class telegramApi { } json["type"] = "group"; json["detail"] = { - "memberCount": getBasicGroup["member_count"] + "member_count": getBasicGroup["member_count"] }; + try { + var getBasicGroupFullInfo = await this.getBasicGroupFullInfo(option["chat_id"]); + if (typeof getBasicGroupFullInfo["photo"] == "object" && typeof getBasicGroupFullInfo["photo"]["sizes"] == "object" && getBasicGroupFullInfo["photo"]["sizes"].length > 0) { + var getBasicGroupPhotos = getBasicGroupFullInfo["photo"]["sizes"]; + json["profile_photo"] = getBasicGroupPhotos[getBasicGroupPhotos.length - 1]["photo"]["remote"]["id"]; + } + } catch (e) { + + } return { ok: true, result: json }; } else if (RegExp("private", "i").exec(type_chat)) { var getUser = await this.getUser(option["chat_id"]); @@ -575,14 +632,29 @@ class telegramApi { json["language_code"] = getUser["language_code"] } json["detail"] = { - "contact": getUser["is_contact"], - "mutual": getUser["is_mutual_contact"], - "verified": getUser["is_verified"], - "support": getUser["is_support"], - "scam": getUser["is_scam"], - "fake": getUser["is_fake"], - "acces": getUser["have_access"] + "is_contact": getUser["is_contact"], + "is_mutual_contact": getUser["is_mutual_contact"], + "is_verified": getUser["is_verified"], + "is_support": getUser["is_support"], + "is_scam": getUser["is_scam"], + "is_fake": getUser["is_fake"], + "have_acces": getUser["have_access"] }; + + try { + var getUserFullInfo = await this.getUserFullInfo(option["chat_id"]); + if (typeof getUserFullInfo == "object") { + if (getUserFullInfo["bio"]) { + json["bio"] = getUserFullInfo["bio"]; + } + if (typeof getUserFullInfo["photo"] == "object" && getUserFullInfo["photo"].length > 0) { + json["profile_photo"] = getUserFullInfo["photo"][getUserFullInfo["photo"].length - 1]["file_id"]; + } + } + } catch (e) { + + } + return { "ok": true, "result": json }; } else { return { "ok": false, "result": getUser }; @@ -700,21 +772,25 @@ class telegramApi { } - async invoke(method, parameters = false) { - var data = { - '_': method - }; - if (parameters) { - libForEach(parameters, (nilai, index) => { - data[index] = nilai - }); + async invoke(method, parameters = {}) { + if (typeof method != "string") { + throw { + "message": "method false" + }; } - return await this.handle.invoke(data); + if (typeof parameters != "object") { + throw { + "message": "parameters false" + }; + } + delete parameters["_"]; + var option = { + "_": method, + ...parameters + }; + return await this.handle.invoke(option); } - - - typeFile(content) { var data = {} @@ -801,6 +877,47 @@ class telegramApi { }; return await this.handle.invoke(data) } + + async requestSendMessage(parameters) { + var sendMessage = await this.invoke("sendMessage", parameters); + if (this.on_update) { + var request = this; + var value = { + "status_code": 200, + "status_bool": true, + "result": {} + }; + await timers.setTimeout(1000, request.handle.addListener("update", async function (update) { + if (typeof update == "object") { + if (update["_"] == "updateMessageSendSucceeded") { + try { + var result = await request.request("getMessage", { + "chat_id": update["message"]["chat_id"], + "message_id": update["message"]["id"] + }); + return value["result"] = result["message"]; + } catch (e) { + value["status_code"] = 500; + value["status_bool"] = false; + return value["result"] = {} + } + } else { + value["status_code"] = 400; + value["status_bool"] = false; + return value["result"] = update; + } + } else { + value["status_code"] = 405; + value["status_bool"] = false; + return value["result"] = {} + } + })); + return value; + } else { + return sendMessage; + } + } + async sendMessage(chat_id, text, parse_mode = false, entities = false, disable_web_page_preview = false, disable_notification = false, reply_to_message_id = false, reply_markup = false) { var pesan = this.parseMode(text, parse_mode, entities); var data = { @@ -823,18 +940,18 @@ class telegramApi { "disable_web_page_preview": disable_web_page_preview, "clear_draft": false }; - return await this.handle.invoke(data); + return await this.requestSendMessage(data); } async editMessageText(chat_id, message_id, text, parse_mode = false, entities = false, disable_web_page_preview = false, reply_markup = false) { - var pesan = this.parseMode(text, parse_mode, entities) + var pesan = this.parseMode(text, parse_mode, entities); var data = { '_': "editMessageText", "chat_id": chat_id, "message_id": message_id, "input_message_content": {} - } + }; if (reply_markup) { data["reply_markup"] = this.reply_markup(reply_markup) } @@ -846,6 +963,17 @@ class telegramApi { }; return await this.handle.invoke(data); } + async editMessageReplyMarkup(chat_id, message_id, reply_markup = false) { + var data = { + '_': "editMessageReplyMarkup", + "chat_id": chat_id, + "message_id": message_id + }; + if (typeof reply_markup == "object") { + data["reply_markup"] = this.reply_markup(reply_markup); + } + return await this.handle.invoke(data); + } reply_markup(keyboard) { try { @@ -892,27 +1020,51 @@ class telegramApi { async sendChatAction(chat_id, type = 'typing') { var action = 'chatActionTyping'; switch (type.toLowerCase()) { - case 'photo': - action = 'chatActionUploadingPhoto' + case 'cancel': + action = 'chatActionCancel'; + break; + case 'contact': + action = 'chatActionChoosingContact'; break; - case 'document': - action = 'chatActionUploadingDocument' + case 'location': + action = 'chatActionChoosingLocation'; break; - case 'video': - action = 'chatActionUploadingVideo' + case 'sticker': + action = 'chatActionChoosingSticker'; break; - case 'voice': - case 'audio': - action = 'chatActionRecordingVoiceNote' + case 'record_video': + action = 'chatActionRecordingVideo'; break; - case 'location': - case 'venue': - action = 'chatActionChoosingLocation' + case 'record_video_note': + action = 'chatActionRecordingVideoNote'; break; - case 'cancel': - action = 'chatActionCancel' + case 'record_voice': + action = 'chatActionRecordingVoiceNote'; + break; + case 'game': + action = 'chatActionStartPlayingGame'; break; case 'typing': + action = 'chatActionTyping'; + break; + case 'upload_document': + action = 'chatActionUploadingDocument'; + break; + case 'upload_photo': + action = 'chatActionUploadingPhoto'; + break; + case 'upload_video': + action = 'chatActionUploadingVideo'; + break; + case 'upload_video_note': + action = 'chatActionUploadingVideoNote'; + break; + case 'upload_voice': + action = 'chatActionUploadingVoiceNote'; + break; + case 'watch_animation': + action = 'chatActionWatchingAnimations'; + break; default: action = 'chatActionTyping' break; @@ -921,7 +1073,9 @@ class telegramApi { return await this.handle.invoke({ '_': "sendChatAction", "chat_id": chat_id, - 'action': { '_': action } + 'action': { + '_': action + } }); } @@ -951,11 +1105,10 @@ class telegramApi { if (reply_markup) { data["reply_markup"] = this.reply_markup(reply_markup); } - return await this.handle.invoke(data); + return await this.requestSendMessage(data); } async copyMessage(chat_id, from_chat_id, message_id, new_caption = false, parse_mode = "html", entities = false, disable_notification = false, reply_to_message_id = false, reply_markup = false) { - var data = { '_': "sendMessage", "chat_id": chat_id, @@ -984,7 +1137,7 @@ class telegramApi { if (reply_markup) { data["reply_markup"] = this.reply_markup(reply_markup); } - return await this.handle.invoke(data); + return await this.requestSendMessage(data); } @@ -1040,6 +1193,45 @@ class telegramApi { return await this.handle.invoke(data); } + async getBlockedMessageSenders(offset, limit = 50) { + var data = { + '_': "getBlockedMessageSenders", + "offest": offset, + "limit": limit + }; + return await this.handle.invoke(data); + } + + async getAllBlockMessageSender() { + var limit = 40; + var total_count = 0; + var getBlockedMessageSenders = await this.handle.invoke({ + '_': "getBlockedMessageSenders", + "offest": 0, + "limit": limit + }); + total_count = getBlockedMessageSenders["total_count"]; + var list_user = []; + list_user = getBlockedMessageSenders["senders"]; + var start_offset = limit; + for (let index = 0; index < (total_count / limit); index++) { + if (index > 0) { + await timers.setTimeout(2000); + var getBlockedMessageSender = await this.handle.invoke({ + '_': "getBlockedMessageSenders", + "offest": start_offset, + "limit": limit + }); + await timers.setTimeout(2000); + for (var i = 0; i < getBlockedMessageSender["senders"].length; i++) { + list_user.push(getBlockedMessageSender["senders"][i]); + } + start_offset = (start_offset + limit); + } + } + return { "ok": true, "result": list_user }; + } + async getUserFullInfo(user_id, nau = false) { var param = { '_': "getUserFullInfo", @@ -1142,12 +1334,12 @@ class telegramApi { '_': "inputMessagePhoto", "photo": detailData, }; - if (caption) { + if (typeof caption == "string") { var text = this.parseMode(caption, parse_mode, caption_entities); data["input_message_content"]["caption"] = text; } - return await this.handle.invoke(data); + return await this.requestSendMessage(data); } async sendDocument(chat_id, document, caption = false, parse_mode = false, caption_entities = false, disable_notification = false, reply_to_message_id = false, reply_markup = false) { @@ -1180,7 +1372,7 @@ class telegramApi { data["input_message_content"]["caption"] = text; } - return await this.handle.invoke(data) + return await this.requestSendMessage(data); } async sendVideo(chat_id, video, caption = false, parse_mode = false, caption_entities = false, disable_notification = false, reply_to_message_id = false, reply_markup = false) { @@ -1212,8 +1404,7 @@ class telegramApi { var text = this.parseMode(caption, parse_mode, caption_entities); data["input_message_content"]["caption"] = text; } - - return this.handle.invoke(data) + return await this.requestSendMessage(data); } async sendAudio(chat_id, audio, caption = false, parse_mode = false, caption_entities = false, disable_notification = false, reply_to_message_id = false, reply_markup = false) { @@ -1248,7 +1439,7 @@ class telegramApi { data["input_message_content"]["caption"] = text; } - return await this.handle.invoke(data); + return await this.requestSendMessage(data); } async sendVoice(chat_id, voice, caption = false, parse_mode = false, caption_entities = false, disable_notification = false, reply_to_message_id = false, reply_markup = false) { @@ -1284,7 +1475,7 @@ class telegramApi { data["input_message_content"]["caption"] = text; } - return await this.handle.invoke(data); + return await this.requestSendMessage(data); } async sendSticker(chat_id, sticker, disable_notification = false, reply_to_message_id = false, reply_markup = false) { @@ -1312,7 +1503,7 @@ class telegramApi { if (typeof reply_markup == "object") { data["reply_markup"] = this.reply_markup(reply_markup); } - return await this.handle.invoke(data); + return await this.requestSendMessage(data); } async answerCallbackQuery(callback_query_id, text = false, show_alert = false, url = false, cache_time = false) { @@ -1368,12 +1559,13 @@ class telegramApi { return await this.handle.invoke(data); } - async getChats() { + async getChats(limit = 4000) { return await this.handle.invoke({ '_': 'getChats', - "offset_order": '9223372036854775807', - "offset_chat_id": 0, - "limit": Math.floor(Math.random() * 9999999) + "chat_list": { + "_": 'chatListMain' + }, + "limit": limit }); } @@ -1396,6 +1588,14 @@ class telegramApi { } + async getBasicGroupFullInfo(chat_id) { + var chat_id = String(chat_id).replace(/(-100|-)/ig, ""); + var data = { + '_': "getBasicGroupFullInfo", + "basic_group_id": Number(chat_id) + }; + return await this.handle.invoke(data); + } async getChatAdministrators(chat_id) { var data = { '_': "getChatAdministrators", @@ -1430,7 +1630,7 @@ class telegramApi { json_user["language_code"] = getUser["language_code"]; } var json_user_detail = {}; - json_user_detail.contact = getUser.is_contact + json_user_detail["contact"] = getUser.is_contact json_user_detail.mutual = getUser.is_mutual_contact json_user_detail.verified = getUser.is_verified json_user_detail.support = getUser.is_support @@ -1451,6 +1651,88 @@ class telegramApi { } } + async promoteChatMember(chat_id, user_id, options) { + var data = { + '_': "setChatMemberStatus", + "chat_id": chat_id, + "member_id": { + "_": "messageSenderUser", + "user_id": user_id + }, + "status": { + "_": "chatMemberStatusAdministrator", + "custom_title": "Admins" + } + }; + if (typeof options == "boolean") { + var status = { + "_": "chatMemberStatusAdministrator", + "custom_title": "Admins" + }; + for (var key in options) { + if (Object.hasOwnProperty.call(options, key)) { + var loop_data = options[key]; + if (typeof loop_data == "boolean") { + status[String(key).toLocaleLowerCase()] = loop_data; + } + } + } + data["status"] = status; + } + return await this.handle.invoke(data); + } + async setChatMemberStatus(chat_id, user_id, status = "ban", banned_until_date = 0) { + var data = { + '_': "setChatMemberStatus", + "chat_id": chat_id, + "member_id": { + "_": "messageSenderUser", + "user_id": user_id + }, + "status": { + "_": "chatMemberStatusBanned", + "banned_until_date": banned_until_date + } + }; + return await this.handle.invoke(data); + } + + async banChatMember(chat_id, user_id, banned_until_date = 0, revoke_messages = false) { + var data = { + '_': "banChatMember", + "chat_id": chat_id, + "member_id": { + "_": "messageSenderUser", + "user_id": user_id + } + }; + if (typeof banned_until_date == "number") { + data["banned_until_date"] = banned_until_date; + } + if (typeof revoke_messages == "boolean") { + data["revoke_messages"] = revoke_messages; + } + return await this.handle.invoke(data); + } + + async banChatSenderChat(chat_id, sender_chat_id, banned_until_date = 0, revoke_messages = false) { + var data = { + '_': "getChatMember", + "chat_id": chat_id, + "member_id": { + "_": "messageSenderChat", + "chat_id": sender_chat_id + } + }; + if (typeof banned_until_date == "number") { + data["banned_until_date"] = banned_until_date; + } + if (typeof revoke_messages == "boolean") { + data["revoke_messages"] = revoke_messages; + } + return await this.handle.invoke(data); + } + async getChatMember(chat_id, user_id) { var data = { '_': "getChatMember", @@ -1461,7 +1743,6 @@ class telegramApi { } }; return await this.handle.invoke(data); - } async getChatList() { var { chat_ids } = await this.getChats() @@ -1659,6 +1940,27 @@ class telegramApi { }); } + async setAuthenticationPhoneNumber(phone_number) { + return await this.handle.invoke({ + '_': "setAuthenticationPhoneNumber", + "phone_number": phone_number + }); + } + + async checkAuthenticationCode(code) { + return await this.handle.invoke({ + '_': "checkAuthenticationCode", + "code": code + }); + } + + async checkAuthenticationPassword(password) { + return await this.handle.invoke({ + '_': "checkAuthenticationPassword", + "password": password + }); + } + } function libForEach(obj, fn) { diff --git a/node-js/tdl-lib/src/js/update.js b/node-js/tdl-lib/src/js/update.js index f21e3aba..dc141581 100644 --- a/node-js/tdl-lib/src/js/update.js +++ b/node-js/tdl-lib/src/js/update.js @@ -59,9 +59,12 @@ class updateApi { json["chat"] = chatJson; } - if (update["message"]["date"]) { + if (typeof update["message"]["date"] == "number") { json["date"] = Number(update["message"]["date"]); } + if (typeof update["message"]["author_signature"] == "string"){ + json["author_signature"] = update["message"]["author_signature"]; + } if (update["message"]["forward_info"]) { var forward_info = update["message"]["forward_info"]; if (forward_info["origin"]) { @@ -96,7 +99,13 @@ class updateApi { if (update["message"]["reply_to_message_id"] && update["message"]["reply_in_chat_id"]) { var getMessage = await this.tg.getMessage(update["message"]["reply_in_chat_id"], update["message"]["reply_to_message_id"]); var replyTo = await this.replyMessage({ message: getMessage }); - json["reply_to_message"] = replyTo["message"] + if (typeof replyTo["message"] == "object") { + json["reply_to_message"] = replyTo["message"]; + + } + if (typeof replyTo["channel_post"] == "object") { + json["reply_to_message"] = replyTo["channel_post"]; + } } if (update["message"]["content"]) { @@ -313,7 +322,11 @@ class updateApi { json["caption"] = update["message"]["content"]["caption"]["text"]; } } - return { channel_post: json }; + if (update["message"]["reply_markup"]) { + var { reply_markup } = update["message"]; + json["reply_markup"] = this.replyMarkup(reply_markup); + }; + return { "channel_post": json }; } else { var json = {}; if (update["message"]["is_outgoing"] ? true : true) { @@ -403,6 +416,7 @@ class updateApi { } catch (e) { } } + if (update["message"]["content"]) { if (update["message"]["content"]["text"] && new RegExp("^messageText$", "i").exec(update["message"]["content"]["_"]) && new RegExp("^formattedText$", "i").exec(update["message"]["content"]["text"]["_"])) { json["type_content"] = "text"; @@ -693,25 +707,45 @@ class updateApi { } json["entities"] = array_entities } + + if (update["message"]["content"]["_"] == "messageChatAddMembers") { + var new_chat_members = []; + if (typeof update["message"]["content"]["member_user_ids"] == "object" && update["message"]["content"]["member_user_ids"].length > 0) { + var member_user_ids = update["message"]["content"]["member_user_ids"]; + for (var i = 0; i < member_user_ids.length; i++) { + var loop_data = member_user_ids[i]; + try { + var getUser = await this.tg.request("getUser", { "chat_id": loop_data }); + if (getUser["ok"]) { + new_chat_members.push(getUser["result"]); + } + } catch (e) { + + } + } + } + json["new_chat_members"] = new_chat_members; + } } - if (update["message"].reply_markup) { + + if (update["message"]["reply_markup"]) { var { reply_markup } = update["message"]; json["reply_markup"] = this.replyMarkup(reply_markup); }; - return { message: json }; + return { "message": json }; } //--! callback_query !--\\ } else if (new RegExp(`^updateNewCallbackQuery$`, "i").exec(update["_"])) { var json_data = { "callback_query": {} }; - var json = json_data.callback_query + var json = json_data["callback_query"]; if (update["id"]) { - json["id"] = update["id"] + json["id"] = update["id"]; } - var senderUserId = update.sender_user_id ? update.sender_user_id : false + var senderUserId = update["sender_user_id"] ? update["sender_user_id"] : false var fromJson = {} if (senderUserId) { - var getUser = await this.tg.request("getUser", { chat_id: senderUserId }); + var getUser = await this.tg.request("getUser", { "chat_id": senderUserId }); if (getUser["ok"]) { fromJson = getUser["result"]; } else { @@ -723,8 +757,12 @@ class updateApi { if (update["chat_id"]) { if (update["message_id"]) { var getMessage = await this.tg.getMessage(update["chat_id"], update["message_id"]); - var replyTo = await this.replyMessage({ message: getMessage }); - json["message"] = replyTo["message"]; + var replyTo = await this.replyMessage({ "message": getMessage }); + if (replyTo["message"]) { + json["message"] = replyTo["message"]; + } else if (replyTo["channel_post"]) { + json["message"] = replyTo["channel_post"]; + } } var chatJson = {}; var chatId = update["chat_id"]; @@ -741,7 +779,7 @@ class updateApi { return json_data; } else if (new RegExp(`^updateChatMember$`, "i").exec(update["_"])) { var json_data = { "chat_member": {} }; - var json = json_data.chat_member; + var json = json_data["chat_member"]; if (update["chat_id"]) { var chatJson = {}; var chatId = update["chat_id"]; @@ -753,9 +791,9 @@ class updateApi { } json["chat"] = chatJson; } - if (update.actor_user_id) { + if (update["actor_user_id"]) { var fromJson = {}; - var getUser = await this.tg.request("getUser", { chat_id: update.actor_user_id }); + var getUser = await this.tg.request("getUser", { "chat_id": update["actor_user_id"] }); if (getUser["ok"]) { fromJson = getUser["result"]; } else { @@ -773,17 +811,17 @@ class updateApi { var data_json = { "user": {} }; var fromJson = {}; if (oldChatMemberUserId) { - var getUser = await this.tg.request("getUser", { chat_id: oldChatMemberUserId }); + var getUser = await this.tg.request("getUser", { "chat_id": oldChatMemberUserId }); if (getUser["ok"]) { fromJson = getUser["result"]; } else { throw new Error(getUser); } } - data_json["join_date"] = update["old_chat_member"].joined_chat_date; + data_json["join_date"] = update["old_chat_member"]["joined_chat_date"]; data_json["status"] = update["old_chat_member"]["status"]["_"].replace(/(chatMemberStatus)/ig, "").toLocaleLowerCase(); data_json["user"] = fromJson; - json["old_chat_member"] = data_json + json["old_chat_member"] = data_json; } if (update["new_chat_member"] && update["new_chat_member"]["member_id"] && update["new_chat_member"]["member_id"]["user_id"] && update["new_chat_member"]["_"]) { @@ -803,6 +841,7 @@ class updateApi { data_json["user"] = fromJson; json["new_chat_member"] = data_json } + try { if (update["invite_link"] && update["invite_link"]["_"]) { var data_json = { "user": {} }; @@ -825,7 +864,7 @@ class updateApi { } catch (e) { } - return json_data + return json_data; } else if (RegExp("^updateMessageSendSucceeded$", "i").exec(update["_"])) { return { message_send: update }; } else if (RegExp("^updateMessageSendFailed$", "i").exec(update["_"])) { @@ -1104,7 +1143,11 @@ class updateApi { json["caption"] = update["message"]["content"]["caption"]["text"]; } } - return { channel_post: json }; + if (update["message"].reply_markup) { + var { reply_markup } = update["message"]; + json["reply_markup"] = this.replyMarkup(reply_markup); + }; + return { "channel_post": json }; } else { var json = {}; if (update["message"]["is_outgoing"] ? true : true) { @@ -1451,7 +1494,7 @@ class updateApi { var { reply_markup } = update["message"]; json["reply_markup"] = this.replyMarkup(reply_markup); }; - return { message: json }; + return { "message": json }; } } diff --git a/node-js/telegram_client/package.json b/node-js/telegram_client/package.json index 957765da..40daf5b9 100644 --- a/node-js/telegram_client/package.json +++ b/node-js/telegram_client/package.json @@ -1,6 +1,6 @@ { "name": "telegram_client", - "version": "0.0.3", + "version": "0.0.8", "description": "library to make bot or user bot telegram auto update latest bot api version telegram only bot api", "main": "src/index.js", "homepage": "https://github.com/azkadev", diff --git a/node-js/telegram_client/src/js/api.js b/node-js/telegram_client/src/js/api.js index e87c52f4..4d329c67 100644 --- a/node-js/telegram_client/src/js/api.js +++ b/node-js/telegram_client/src/js/api.js @@ -1,33 +1,75 @@ // azka dev const nodefetch = require("node-fetch"); +const FormData = require('form-data'); +const form = new FormData(); +var fs = require("fs"); class Api { constructor(token, options) { this.token = token; this.options = options; } - async request(method, data = false) { - if (!method) { + async request(method, parameter = false, is_form_data = false) { + if (typeof method != "string" && String(method).length == 0) { throw { - message: "please use token" + "message": "please use token" }; } - var option = { - 'method': 'POST', - 'headers': { - 'Accept': 'application/json', - 'Content-Type': 'application/json' + if (typeof is_form_data != "boolean") { + throw { + "message": "parameter form must be boolean" + }; + } + var option = {}; + if (is_form_data) { + option = { + 'method': 'post' + }; + for (const key in parameter) { + if (Object.hasOwnProperty.call(parameter, key)) { + var element = parameter[key]; + if (typeof element == "object") { + if (element["is_post_file"]) { + form.append(key, element["buffer"], element["option"]); + } else { + form.append(key, JSON.stringify(element)); + } + } else if (typeof element != "string") { + form.append(key, String(element)); + } else { + form.append(key, element); + } + } + } + if (typeof parameter == "object") { + option["body"] = form; + } + } else { + option = { + 'method': 'POST', + 'headers': { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + } + }; + if (typeof parameter == "object") { + option["body"] = JSON.stringify(parameter); } - }; - if (data) { - option.body = JSON.stringify(data); } - var url = this.options.api + String(this.options.type).toLocaleLowerCase(); - var response = await nodefetch(url + this.token + "/" + method, option); - if (this.options.logger) { + + var url = this["options"]["api"] + String(this["options"]["type"]).toLocaleLowerCase(); + var response = await nodefetch(url + this["token"] + "/" + method, option); + if (this["options"]["logger"]) { console.log(JSON.stringify(response, null, 2)); } if (response.status == 200) { - return await response.json(); + if (String(method).toLocaleLowerCase() == "getfile") { + var getFile = await response.json(); + var url = this["options"]["api"] + "file/" + String(this["options"]["type"]).toLocaleLowerCase(); + getFile["result"]["links"] = url + this["token"] + "/" + getFile["result"]["file_path"]; + return getFile; + } else { + return await response.json(); + } } else { var msg = await response.json(); throw { @@ -37,8 +79,59 @@ class Api { } - requestForm(method, data) { - return await this.request(method, data); + async requestForm(method, data) { + return await this.request(method, data, true); + } + + file(path, option = false) { + var json = { + "is_post_file": true + }; + if (typeof path != "string") { + + } + if (RegExp("^(./|\/)", "i").exec(path)) { + var filename = String(path).replace(/(.*[\/\\])/i, ""); + if (typeof option == "object") { + json["option"] = { + "filename": filename, + ...option + }; + } else { + json["option"] = { + "filename": filename, + }; + } + json["buffer"] = fs.readFileSync(path); + return json; + } else { + return path; + } + } + + fileUpload(data, option) { + var json = { + "is_post_file": true + }; + if (typeof path != "string") { + + } + if (typeof data === "object") { + if (typeof option == "object") { + json["option"] = { + "filename": data["name"], + ...option + }; + } else { + json["option"] = { + "filename": data["name"], + }; + } + json["buffer"] = Buffer.from(data["data"]["data"]); + return json; + } else { + return data; + } } typeFile(data) { @@ -292,7 +385,10 @@ class Api { var json = { "file_id": file_id }; - return await this.request("getFile", json); + var getFile = await this.request("getFile", json); + var url = this["options"]["api"] + "file/" + String(this["options"]["type"]).toLocaleLowerCase(); + getFile["result"]["links"] = url + this["token"] + "/" + getFile["result"]["file_path"]; + return getFile; } async banChatMember(chat_id, user_id, parameter = {}) { var json = {