Skip to content

Commit b684c4f

Browse files
author
Alireza
committed
Fix Things on copyMessage
1 parent 58281f0 commit b684c4f

4 files changed

Lines changed: 52 additions & 8 deletions

File tree

‎telebot/__init__.py‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import threading
88
import time
99

10+
from telebot.types import MessageID
11+
1012
logger = logging.getLogger('TeleBot')
1113
formatter = logging.Formatter(
1214
'%(asctime)s (%(filename)s:%(lineno)d %(threadName)s) %(levelname)s - %(name)s: "%(message)s"'
@@ -751,19 +753,24 @@ def forward_message(self, chat_id, from_chat_id, message_id, disable_notificatio
751753
apihelper.forward_message(self.token, chat_id, from_chat_id, message_id, disable_notification, timeout))
752754

753755
def copy_message(self, chat_id, from_chat_id, message_id, caption=None, parse_mode=None, caption_entities=None,
754-
reply_to_message_id=None, allow_sending_without_reply=None, reply_markup=None,
755-
disable_notification=None, timeout=None):
756-
# FIXME: rewrite the docstring
756+
disable_notification=None, reply_to_message_id=None, allow_sending_without_reply=None, reply_markup=None,
757+
timeout=None):
757758
"""
758759
Use this method to copy messages of any kind.
759-
:param disable_notification:
760760
:param chat_id: which chat to forward
761761
:param from_chat_id: which chat message from
762762
:param message_id: message id
763+
:param caption:
764+
:param parse_mode:
765+
:param caption_entities:
766+
:param disable_notification:
767+
:param reply_to_message_id:
768+
:param allow_sending_without_reply:
769+
:param reply_markup:
763770
:param timeout:
764771
:return: API reply.
765772
"""
766-
return types.Message.de_json(
773+
return MessageID.de_json(
767774
apihelper.copy_message(self.token, chat_id, from_chat_id, message_id, caption, parse_mode, caption_entities,
768775
reply_to_message_id, allow_sending_without_reply, reply_markup,
769776
disable_notification, timeout))

‎telebot/apihelper.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,11 @@ def copy_message(token, chat_id, from_chat_id, message_id, caption=None, parse_m
336336
if parse_mode is not None:
337337
payload['parse_mode'] = parse_mode
338338
if caption_entities is not None:
339-
payload['caption_entities'] = caption_entities
339+
payload['caption_entities'] = _convert_entites(caption_entities)
340340
if reply_to_message_id is not None:
341341
payload['reply_to_message_id'] = reply_to_message_id
342342
if reply_markup is not None:
343-
payload['reply_markup'] = reply_markup
343+
payload['reply_markup'] = _convert_markup(reply_markup)
344344
if allow_sending_without_reply is not None:
345345
payload['allow_sending_without_reply'] = allow_sending_without_reply
346346
if disable_notification is not None:
@@ -1309,6 +1309,12 @@ def _convert_markup(markup):
13091309
return markup
13101310

13111311

1312+
def _convert_entites(entites):
1313+
if isinstance(entites[0], types.JsonSerializable):
1314+
return [entity.to_json() for entity in entites]
1315+
return entites
1316+
1317+
13121318
def convert_input_media(media):
13131319
if isinstance(media, types.InputMedia):
13141320
return media.convert_input_media()

‎telebot/types.py‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,19 @@ def __init__(self, id, type, title=None, username=None, first_name=None,
261261
self.location = location
262262

263263

264+
class MessageID(JsonDeserializable):
265+
@classmethod
266+
def de_json(cls, json_string):
267+
if(json_string is None):
268+
return None
269+
obj = cls.check_json(json_string)
270+
message_id = obj['message_id']
271+
return cls(message_id)
272+
273+
def __init__(self, message_id):
274+
self.message_id = message_id
275+
276+
264277
class Message(JsonDeserializable):
265278
@classmethod
266279
def de_json(cls, json_string):
@@ -548,7 +561,7 @@ def html_caption(self):
548561
return self.__html_text(self.caption, self.caption_entities)
549562

550563

551-
class MessageEntity(JsonDeserializable):
564+
class MessageEntity(Dictionaryable, JsonSerializable, JsonDeserializable):
552565
@classmethod
553566
def de_json(cls, json_string):
554567
if (json_string is None): return None
@@ -569,6 +582,17 @@ def __init__(self, type, offset, length, url=None, user=None, language=None):
569582
self.user = user
570583
self.language = language
571584

585+
def to_json(self):
586+
return json.dumps(self.to_dict())
587+
588+
def to_dict(self):
589+
return {"type": self.type,
590+
"offset": self.offset,
591+
"length": self.length,
592+
"url": self.url,
593+
"user": self.user,
594+
"language": self.language}
595+
572596

573597
class Dice(JsonSerializable, Dictionaryable, JsonDeserializable):
574598
@classmethod

‎tests/test_telebot.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,13 @@ def test_forward_message(self):
293293
ret_msg = tb.forward_message(CHAT_ID, CHAT_ID, msg.message_id)
294294
assert ret_msg.forward_from
295295

296+
def test_copy_message(self):
297+
text = 'CI copy_message Test Message'
298+
tb = telebot.TeleBot(TOKEN)
299+
msg = tb.send_message(CHAT_ID, text)
300+
ret_msg = tb.copy_message(CHAT_ID, CHAT_ID, msg.message_id)
301+
assert ret_msg
302+
296303
def test_forward_message_dis_noti(self):
297304
text = 'CI forward_message Test Message'
298305
tb = telebot.TeleBot(TOKEN)

0 commit comments

Comments
 (0)