-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Upsonic/added-telegram-library
added-telegram-library
- Loading branch information
Showing
6 changed files
with
180 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ requests | |
python-dotenv==1.0.0 | ||
duckduckgo-search==5.3.0 | ||
tinydb==4.8.0 | ||
telethon==1.34.0 | ||
upsonic |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import asyncio | ||
import time | ||
|
||
from telethon.sync import TelegramClient | ||
|
||
|
||
def delete_message(number: str, message: str): | ||
""" | ||
:param number: str: | ||
:param message: str: | ||
""" | ||
import nest_asyncio | ||
|
||
nest_asyncio.apply() | ||
|
||
async def del_message(num, message): | ||
async with TelegramClient( | ||
"upsonic_tiger", 21659296, | ||
"7d0ebd20538d88ab0629eb926acb08f7") as client: | ||
await client.delete_messages(num, message) | ||
|
||
asyncio.run(del_message(number, message)) | ||
|
||
|
||
tool_name = "communication.telegram.delete_message" | ||
tool_obj = delete_message | ||
tool_requirements = ["telethon==1.34.0"] |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import asyncio | ||
import time | ||
|
||
from telethon.sync import TelegramClient | ||
|
||
|
||
def get_last_dialogs(limit=100): | ||
""" | ||
:param limit: (Default value = 100) | ||
""" | ||
import nest_asyncio | ||
|
||
nest_asyncio.apply() | ||
|
||
async def fetch_recent_chats(limit): | ||
async with TelegramClient( | ||
"upsonic_tiger", 21659296, | ||
"7d0ebd20538d88ab0629eb926acb08f7") as client: | ||
recent_chats = await client.get_dialogs(limit=limit) | ||
chat_names = {} | ||
for chat in recent_chats: | ||
number = "" | ||
type_of_entity = "" | ||
if chat.is_user: | ||
number = chat.entity.phone | ||
type_of_entity = "user" | ||
if chat.is_channel: | ||
number = chat.entity.username or chat.entity.id | ||
type_of_entity = "channel" | ||
if chat.is_group: | ||
number = chat.entity.id | ||
type_of_entity = "group" | ||
chat_names[chat.id] = { | ||
"number": number, | ||
"title": chat.name or chat.title, | ||
"type_of_entity": type_of_entity, | ||
"unread_count": chat.unread_count, | ||
} | ||
return chat_names | ||
|
||
chats = asyncio.run(fetch_recent_chats(limit=limit)) | ||
return chats | ||
|
||
|
||
tool_name = "communication.telegram.get_last_dialogs" | ||
tool_obj = get_last_dialogs | ||
tool_requirements = ["telethon==1.34.0"] |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import asyncio | ||
import time | ||
|
||
from telethon.sync import TelegramClient | ||
|
||
|
||
def get_last_messages(number: str, limit=100): | ||
""" | ||
:param number: str: | ||
:param limit: (Default value = 100) | ||
""" | ||
import nest_asyncio | ||
|
||
nest_asyncio.apply() | ||
|
||
async def get_messages(num, limit): | ||
async with TelegramClient( | ||
"upsonic_tiger", 21659296, | ||
"7d0ebd20538d88ab0629eb926acb08f7") as client: | ||
messages = await client.get_messages(num, limit=limit) | ||
the_messages_list = {} | ||
for each_ms in messages: | ||
the_messages_list[each_ms.id] = { | ||
"id": | ||
each_ms.id, | ||
"message": | ||
each_ms.text, | ||
"date": | ||
each_ms.date, | ||
"sender": | ||
(await | ||
client.get_entity(each_ms.peer_id.user_id)).username, | ||
} | ||
return the_messages_list | ||
|
||
messages = asyncio.run(get_messages(number, limit)) | ||
return messages | ||
|
||
|
||
tool_name = "communication.telegram.get_last_messages" | ||
tool_obj = get_last_messages | ||
tool_requirements = ["telethon==1.34.0"] |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import asyncio | ||
import time | ||
|
||
from telethon.sync import TelegramClient | ||
|
||
|
||
def send_message(number: str, message: str) -> str: | ||
""" | ||
:param number: str: | ||
:param message: str: | ||
""" | ||
import nest_asyncio | ||
|
||
nest_asyncio.apply() | ||
|
||
async def send_message(number, message): | ||
async with TelegramClient( | ||
"upsonic_tiger", 21659296, | ||
"7d0ebd20538d88ab0629eb926acb08f7") as client: | ||
return (await client.send_message(number, message)).id | ||
|
||
result = asyncio.run(send_message(number, message)) | ||
return result | ||
|
||
|
||
tool_name = "communication.telegram.send_message" | ||
tool_obj = send_message | ||
tool_requirements = ["telethon==1.34.0"] |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import asyncio | ||
import time | ||
|
||
from telethon.sync import TelegramClient | ||
|
||
|
||
def signin(): | ||
""" """ | ||
import nest_asyncio | ||
|
||
nest_asyncio.apply() | ||
|
||
async def send_message(): | ||
async with TelegramClient( | ||
"upsonic_tiger", 21659296, | ||
"7d0ebd20538d88ab0629eb926acb08f7") as client: | ||
message = await client.send_message("me", "upsonic_tiger_test") | ||
await time.sleep(2) | ||
await message.delelete | ||
|
||
result = asyncio.run(send_message()) | ||
return result | ||
|
||
|
||
tool_name = "communication.telegram.signin" | ||
tool_obj = signin | ||
tool_requirements = ["telethon==1.34.0"] |