From c3f54b0b1a989499900311797e3b08805230a6bf Mon Sep 17 00:00:00 2001 From: user Date: Sun, 18 Jun 2023 23:50:15 +0200 Subject: [PATCH] add: send read messsages --- index.js | 86 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 21 deletions(-) diff --git a/index.js b/index.js index 83569c7..494bd8b 100644 --- a/index.js +++ b/index.js @@ -1,35 +1,79 @@ -import { Client } from 'tdl'; -import { TDLib } from 'tdl-tdlib-addon'; -import path from 'path'; -import dotenv from 'dotenv' +import { Client } from "tdl"; +import { TDLib } from "tdl-tdlib-addon"; +import path from "path"; +import dotenv from "dotenv"; -dotenv.config() +dotenv.config(); const __dirname = path.dirname(new URL(import.meta.url).pathname); -function onUpdate(update){ - console.log('UPDATE HERE', update) +function onUpdate(update) { + if (update.chat_id == "91956172") { + const last_msg = update.last_message.content.text.text; + console.log(last_msg); + } + //console.log('Saved messages', update.last_message.content.text.text) } -const client = new Client(new TDLib(path.join(__dirname, 'libtdjson.so')), { +const client = new Client(new TDLib(path.join(__dirname, "libtdjson.so")), { apiId: process.env.APP_ID, - apiHash: process.env.APP_HASH -}) + apiHash: process.env.APP_HASH, +}); -client.login() +const savedMessages = process.env.SAVED_MESSAGES_ID; -client.on('update', onUpdate) -client.on('error', console.error) +client.login(); -async function main(){ +client.on("update", onUpdate); +client.on("error", console.error); -const chats = await client.invoke({ - _: 'getChats', - chat_list: { _: 'chatListMain' }, - limit: 4000 -}) - -console.log('current chats', chats) +async function sendText() { + //👻 + await client + .invoke({ + _: "sendMessage", + chat_id: savedMessages, + input_message_content: { + _: "inputMessageText", + text: { + _: "formattedText", + text: "a", + }, + }, + }) + .then(console.log("Message sent")) + .catch((error) => console.error(error)); } +async function showSavedMessages() { + let imlooping = true + let msgTxt = '' + while (imlooping) { + await client + .invoke({ + _: "getChat", + chat_id: savedMessages, + }) + .then((message) => { + const msgContent = message.last_message.content + msgTxt = '' + if(msgContent._ == 'messageText') { + msgTxt = msgContent.text.text + } else { + console.log('message type not detected') + } + }) + .catch((error) => { + console.error(error, savedMessages); + imlooping = false + }); + } + console.log(msgTxt) +} +async function main() { + showSavedMessages(); + sendText(); + console.log("Logging chats"); +} +main();