Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Telegram Library #7

Merged
merged 8 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ requests
python-dotenv==1.0.0
duckduckgo-search==5.3.0
tinydb==4.8.0
telethon==1.34.0
upsonic
29 changes: 29 additions & 0 deletions tiger/tools/communication/telegram/delete_message.py
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"]
49 changes: 49 additions & 0 deletions tiger/tools/communication/telegram/get_last_dialogs.py
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"]
44 changes: 44 additions & 0 deletions tiger/tools/communication/telegram/get_last_messages.py
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"]
30 changes: 30 additions & 0 deletions tiger/tools/communication/telegram/send_message.py
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"]
27 changes: 27 additions & 0 deletions tiger/tools/communication/telegram/signin.py
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"]