Skip to content
Open
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
65 changes: 46 additions & 19 deletions client_classes/DiscordClient.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,46 @@
import discord
from client_classes.Message import Message


class DiscordClient(discord.Client):
def __init__(self, compute_message_func):
super(DiscordClient, self).__init__()
self.compute_massage = compute_message_func

async def on_message(self, message):
if message.author != self.user:
author_name = message.author.name
chat_name = None
if type(message.channel) != discord.channel.DMChannel:
chat_name = message.author.guild.name + "/" + message.channel.name
self.compute_massage(Message((message.channel.id, "DS"), message.content, message.author.id, author_name, chat_name))

def send_msg(self, id, text):
self.loop.create_task(self.get_channel(id).send(text))
import discord
import requests
import config
from client_classes.Message import Message


class DiscordClient(discord.Client):
def __init__(self, compute_message_func):
super(DiscordClient, self).__init__()
self.compute_massage = compute_message_func

def __get_photo(self, attachments):
url = []
for el in attachments:
link = str(el)
ext = link[link.rfind(".") + 1:]
if ext in config.PHOTO_EXT:
url.append(link)
return url

async def on_ready(self):
print("DS client started.")

async def on_message(self, message):
if message.author != self.user:
photo = self.__get_photo(message.attachments)
from_id = message.channel.id
text = message.content
author_id = message.author.id
author_name = message.author.name
if type(message.channel) != discord.channel.DMChannel:
chat_name = message.author.guild.name + "/" + message.channel.name
is_owner = message.author.guild_permissions.administrator
self.compute_massage(Message((from_id, "DS"), text, author_id, author_name, chat_name=chat_name, is_owner=is_owner, photos=photo))
else:
self.compute_massage(Message((from_id, "DS"), text, author_id, author_name, photos=photo))

def send_msg(self, id, text, photo):
files = []
for i in range(len(photo)):
out = open(config.TEMP_IMAGE_FOLDER + str(i) + ".jpg", "wb")
out.write(requests.get(photo[i]).content)
out.close()
files.append(discord.File(config.TEMP_IMAGE_FOLDER + str(i) + ".jpg"))

self.loop.create_task(self.get_channel(id).send(content=text, files=files))
53 changes: 29 additions & 24 deletions client_classes/Message.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
class Message:
def __init__(self, from_id, text, author_id, author_name, chat_name):
self.from_id = from_id
self.text = text
self.author_id = author_id
self.author_name = author_name
self.chat_name = chat_name

def is_chat_command(self):
return len(self.text) > 0 and self.text[0] == "!"

def get_author_id(self):
return str(self.author_id) + self.from_id[1]

def get_chat_command(self):
if not self.is_chat_command():
return None
return self.text[1:].strip().lower()

def get_text_to_forwarding(self):
description = ""
if self.author_name is not None:
description += self.author_name + ":\n"
return description + self.text
class Message:
def __init__(self, from_id, text, author_id, author_name, chat_name=None, is_owner=None, photos=None):
self.from_id = from_id
self.text = text
self.author_id = author_id
self.author_name = author_name
self.chat_name = chat_name
self.is_owner = is_owner
self.photos = photos

def is_chat_command(self):
return len(self.text) > 0 and self.text[0] == "!"

def get_chat_id(self):
return str(self.from_id[0]) + self.from_id[1]

def get_author_id(self):
return str(self.author_id) + self.from_id[1]

def get_chat_command(self):
if not self.is_chat_command():
return None
return self.text[1:].strip()

def get_content_to_forwarding(self):
description = ""
if self.author_name is not None:
description += self.author_name + ":\n"
return description + self.text, self.photos
92 changes: 63 additions & 29 deletions client_classes/TelegramClient.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,63 @@
import threading
import telebot
from client_classes.Message import Message


class TelegramClient:
def __init__(self, compute_message_func):
self.client = None
self.handler_thread = None
self.compute_massage = compute_message_func

def __handler(self):
@self.client.message_handler(content_types=["text"])
def on_message(message):
author_name = message.from_user.last_name + " " + message.from_user.first_name
chat_name = None
if message.from_user.id != message.chat.id:
chat_name = message.chat.title
self.compute_massage(Message((message.chat.id, "TG"), message.text, message.from_user.id, author_name, chat_name))

self.client.infinity_polling()

def send_msg(self, id, text):
self.client.send_message(id, text)

def run(self, token):
self.client = telebot.TeleBot(token)
self.handler_thread = threading.Thread(target=self.__handler)
self.handler_thread.start()
import threading
import telebot
import requests
import config
from client_classes.Message import Message


class TelegramClient:
def __init__(self, compute_message_func):
self.token = None
self.client = None
self.handler_thread = None
self.compute_massage = compute_message_func

def __get_photo(self, message):
if message.content_type == "photo":
return ["https://api.telegram.org/file/bot" + self.token + "/" + self.client.get_file(message.photo[-1].file_id).file_path]
elif message.content_type == "document":
link = "https://api.telegram.org/file/bot" + self.token + "/" + self.client.get_file(message.document.file_id).file_path
ext = link[link.rfind(".") + 1:]
if ext in config.PHOTO_EXT:
return [link]
return []

def __handler(self):
print("TG client started.")

@self.client.message_handler(content_types=["text", "photo", "document"])
def on_message(message):
photo = self.__get_photo(message)
from_id = message.chat.id
text = ""
if message.content_type == "text":
text = message.text
elif message.caption is not None:
text = message.caption
author_id = message.from_user.id
author = self.client.get_chat_member(from_id, author_id)
author_name = message.from_user.last_name + " " + message.from_user.first_name
if from_id != author_id:
chat_name = message.chat.title
is_owner = author.status == "creator"
self.compute_massage(Message((from_id, "TG"), text, author_id, author_name, chat_name=chat_name, is_owner=is_owner, photos=photo))
else:
self.compute_massage(Message((from_id, "TG"), text, author_id, author_name, photos=photo))

self.client.infinity_polling()

def send_msg(self, id, text, photo):
if photo == []:
return self.client.send_message(id, text)

self.client.send_photo(id, requests.get(photo[0]).content, caption=text)
if len(photo) > 1:
self.client.send_message(id, "Other photos:")
for i in range(1, len(photo)):
self.client.send_photo(id, requests.get(photo[i]).content)

def run(self, token):
self.token = token
self.client = telebot.TeleBot(token)
self.handler_thread = threading.Thread(target=self.__handler)
self.handler_thread.start()
139 changes: 88 additions & 51 deletions client_classes/VkClient.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,88 @@
import threading
import vk_api
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
from client_classes.Message import Message


class VkClient:
def __init__(self, compute_message_func):
self.client = None
self.group_id = None
self.handler_thread = None
self.compute_massage = compute_message_func

def __get_peer_id_by_id(self, id):
return id + 2000000000

def __get_user(self, id):
return self.client.method("users.get", {"user_ids": id})[0]

def __get_chat(self, id):
chat = self.client.method("messages.getConversationsById", {"peer_ids": self.__get_peer_id_by_id(id)})
if chat["count"] == 0:
return {"chat_settings": {"title": "NULL"}}
return chat["items"][0]

def __handler(self):
longpoll = VkBotLongPoll(self.client, self.group_id)

for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
author = self.__get_user(event.object["message"]["from_id"])
author_name = author["last_name"] + " " + author["first_name"]
chat_id = event.object["message"]["from_id"]
chat_name = None
if event.from_chat:
chat_id = event.chat_id
chat = self.__get_chat(event.chat_id)
chat_name = chat["chat_settings"]["title"]
self.compute_massage(Message((chat_id, "VK"), event.object["message"]["text"], event.object["message"]["from_id"], author_name, chat_name))

def send_msg(self, id, text, to_chat):
if to_chat:
self.client.method("messages.send", {"chat_id": id, "message": text, "random_id": 0})
else:
self.client.method("messages.send", {"user_id": id, "message": text, "random_id": 0})

def run(self, token, group_id):
self.client = vk_api.VkApi(token=token)
self.group_id = group_id
self.handler_thread = threading.Thread(target=self.__handler)
self.handler_thread.start()
import threading
import requests
import vk_api
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
import config
from client_classes.Message import Message


class VkClient:
def __init__(self, compute_message_func):
self.client = None
self.upload = None
self.group_id = None
self.handler_thread = None
self.compute_massage = compute_message_func

def __get_peer_id_by_id(self, id):
return id + 2000000000

def __get_user(self, id):
return self.client.method("users.get", {"user_ids": id})[0]

def __get_chat(self, id):
chat = self.client.method("messages.getConversationsById", {"peer_ids": self.__get_peer_id_by_id(id)})
if chat["count"] == 0:
return {"chat_settings": {"title": "NULL", "owner_id": "NULL"}}
return chat["items"][0]

def __get_photo(self, attachments):
url = []
for el in attachments:
if el["type"] == "photo":
for photo in el["photo"]["sizes"]:
if photo["type"] != "z":
continue

url.append(photo["url"])
break
elif el["type"] == "doc" and el["doc"]["ext"] in config.PHOTO_EXT:
url.append(el["doc"]["url"])
return url

def __handler(self):
print("VK client started.")
longpoll = VkBotLongPoll(self.client, self.group_id)

for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
photo = self.__get_photo(event.object.message["attachments"])
text = event.object.message["text"]
author = self.__get_user(event.object.message["from_id"])
author_id = event.object.message["from_id"]
author_name = author["last_name"] + " " + author["first_name"]
if event.from_chat:
from_id = event.chat_id
chat = self.__get_chat(event.chat_id)
chat_name = chat["chat_settings"]["title"]
is_owner = author_id == chat["chat_settings"]["owner_id"]
self.compute_massage(Message((from_id, "VK"), text, author_id, author_name, chat_name=chat_name, is_owner=is_owner, photos=photo))
else:
from_id = event.object.message["from_id"]
self.compute_massage(Message((from_id, "VK"), text, author_id, author_name, photos=photo))

def send_msg(self, id, text, photo, to_chat):
files = []
for i in range(len(photo)):
out = open(config.TEMP_IMAGE_FOLDER + str(i) + ".jpg", "wb")
out.write(requests.get(photo[i]).content)
out.close()
files.append(config.TEMP_IMAGE_FOLDER + str(i) + ".jpg")
response = self.upload.photo_messages(photos=files, peer_id=self.__get_peer_id_by_id(id))

attachment = ""
for el in response:
attachment += "photo" + str(el["owner_id"]) + "_" + str(el["id"]) + "_" + str(el["access_key"]) + ","
attachment = attachment[:-1]

if to_chat:
self.client.method("messages.send", {"chat_id": id, "message": text, "attachment": attachment, "random_id": 0})
else:
self.client.method("messages.send", {"user_id": id, "message": text, "attachment": attachment, "random_id": 0})

def run(self, token, group_id):
self.client = vk_api.VkApi(token=token)
self.upload = vk_api.VkUpload(self.client)
self.group_id = group_id
self.handler_thread = threading.Thread(target=self.__handler)
self.handler_thread.start()
25 changes: 13 additions & 12 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
VK_TOKEN = ""
VK_GROUP_ID = 0

DISCORD_TOKEN = ""

TELEGRAM_TOKEN = ""

TOKEN_LENGTH = 10

GRAPH_STORAGE_NAME = "data/graph.txt"
ERROR_LOG_NAME = "data/error_log.txt"
USERS_INFORMATION_DB_NAME = "data/users_inf.db"
VK_TOKEN = ""
VK_GROUP_ID = 0

DISCORD_TOKEN = ""

TELEGRAM_TOKEN = ""

GRAPH_STORAGE_NAME = "data/graph.txt"
ERROR_LOG_NAME = "data/error_log.txt"
USERS_INFORMATION_DB_NAME = "data/users_inf.db"
TEMP_IMAGE_FOLDER = "data/temp_img/"

PHOTO_EXT = {"jpeg", "jpg", "png"}
Loading