1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00

Added edit support

This commit is contained in:
Steffo 2017-02-26 20:19:54 +01:00
parent 789d94a3f9
commit 65caeb9cea

View file

@ -34,7 +34,10 @@ class Bot:
async def get_updates(self): async def get_updates(self):
"""Get the latest updates from the Telegram API with /getUpdates.""" """Get the latest updates from the Telegram API with /getUpdates."""
try:
data = await self.api_request("getUpdates", offset=self.offset, timeout=0) data = await self.api_request("getUpdates", offset=self.offset, timeout=0)
except asyncio.TimeoutError:
return
for update in data: for update in data:
try: try:
self.updates.append(Update(update)) self.updates.append(Update(update))
@ -51,7 +54,17 @@ class Bot:
chat = self.find_chat(update.message.chat.chat_id) chat = self.find_chat(update.message.chat.chat_id)
if update.message.sent_from not in chat.users: if update.message.sent_from not in chat.users:
chat.users.append(update.message.sent_from) chat.users.append(update.message.sent_from)
if not update.message.edited:
chat.messages.append(update.message) chat.messages.append(update.message)
else:
try:
i = chat.messages.index(chat.find_message(update.message.msg_id))
except ValueError:
pass
else:
chat.messages[i] = update.message
del self.updates[0] del self.updates[0]
def find_chat(self, chat_id): def find_chat(self, chat_id):
@ -87,11 +100,11 @@ class Update:
if "message" in upd_dict: if "message" in upd_dict:
self.message = Message(upd_dict["message"]) self.message = Message(upd_dict["message"])
elif "edited_message" in upd_dict: elif "edited_message" in upd_dict:
self.message = Message(upd_dict["edited_message"]) self.message = Message(upd_dict["edited_message"], edited=True)
elif "channel_post" in upd_dict: elif "channel_post" in upd_dict:
self.message = Message(upd_dict["channel_post"]) self.message = Message(upd_dict["channel_post"])
elif "edited_channel_post" in upd_dict: elif "edited_channel_post" in upd_dict:
self.message = Message(upd_dict["edited_channel_post"]) self.message = Message(upd_dict["edited_channel_post"], edited=True)
else: else:
raise NotImplementedError("No inline support yet.") raise NotImplementedError("No inline support yet.")
@ -144,6 +157,12 @@ class Chat:
else: else:
TypeError("Can't compare Chat to a different object.") TypeError("Can't compare Chat to a different object.")
def find_message(self, msg_id):
for msg in self.messages:
if msg.msg_id == msg_id:
return msg
class User: class User:
def __init__(self, user_dict): def __init__(self, user_dict):
@ -175,10 +194,11 @@ class User:
class Message: class Message:
def __init__(self, msg_dict): def __init__(self, msg_dict, edited=False):
self.msg_id = msg_dict["message_id"] self.msg_id = msg_dict["message_id"]
self.date = datetime.datetime.fromtimestamp(msg_dict["date"]) self.date = datetime.datetime.fromtimestamp(msg_dict["date"])
self.chat = Chat(msg_dict["chat"]) self.chat = Chat(msg_dict["chat"])
self.edited = edited
if "from" in msg_dict: if "from" in msg_dict:
self.sent_from = User(msg_dict["from"]) self.sent_from = User(msg_dict["from"])
else: else: