Skip to content

Commit 74fb825

Browse files
authored
Merge pull request eternnoir#1067 from Aazerra/copyMessage_method
Added copyMessage method
2 parents cc299fe + b561e35 commit 74fb825

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

telebot/__init__.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,29 @@ def forward_message(self, chat_id, from_chat_id, message_id, disable_notificatio
752752
return types.Message.de_json(
753753
apihelper.forward_message(self.token, chat_id, from_chat_id, message_id, disable_notification, timeout))
754754

755+
def copy_message(self, chat_id, from_chat_id, message_id, caption=None, parse_mode=None, caption_entities=None,
756+
disable_notification=None, reply_to_message_id=None, allow_sending_without_reply=None, reply_markup=None,
757+
timeout=None):
758+
"""
759+
Use this method to copy messages of any kind.
760+
:param chat_id: which chat to forward
761+
:param from_chat_id: which chat message from
762+
: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:
770+
:param timeout:
771+
:return: API reply.
772+
"""
773+
return types.MessageID.de_json(
774+
apihelper.copy_message(self.token, chat_id, from_chat_id, message_id, caption, parse_mode, caption_entities,
775+
reply_to_message_id, allow_sending_without_reply, reply_markup,
776+
disable_notification, timeout))
777+
755778
def delete_message(self, chat_id, message_id, timeout=None):
756779
"""
757780
Use this method to delete message. Returns True on success.
@@ -865,7 +888,7 @@ def send_document(self, chat_id, data,reply_to_message_id=None, caption=None, re
865888
"""
866889
parse_mode = self.parse_mode if (parse_mode is None) else parse_mode
867890

868-
return types.Message.de_json(
891+
return types.Message.de_json(
869892
apihelper.send_data(self.token, chat_id, data, 'document', reply_to_message_id, reply_markup,
870893
parse_mode, disable_notification, timeout, caption, thumb))
871894

@@ -2269,6 +2292,11 @@ def send_dice(self, *args, **kwargs):
22692292
def forward_message(self, *args, **kwargs):
22702293
return TeleBot.forward_message(self, *args, **kwargs)
22712294

2295+
@util.async_dec()
2296+
def copy_message(self, *args, **kwargs):
2297+
return TeleBot.copy_message(self, *args, **kwargs)
2298+
2299+
22722300
@util.async_dec()
22732301
def delete_message(self, *args):
22742302
return TeleBot.delete_message(self, *args)

telebot/apihelper.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,30 @@ def forward_message(
345345
return _make_request(token, method_url, params=payload)
346346

347347

348+
def copy_message(token, chat_id, from_chat_id, message_id, caption=None, parse_mode=None, caption_entities=None,
349+
reply_to_message_id=None, allow_sending_without_reply=None, reply_markup=None,
350+
disable_notification=None, timeout=None):
351+
method_url = r'copyMessage'
352+
payload = {'chat_id': chat_id, 'from_chat_id': from_chat_id, 'message_id': message_id}
353+
if caption is not None:
354+
payload['caption'] = caption
355+
if parse_mode is not None:
356+
payload['parse_mode'] = parse_mode
357+
if caption_entities is not None:
358+
payload['caption_entities'] = _convert_entites(caption_entities)
359+
if reply_to_message_id is not None:
360+
payload['reply_to_message_id'] = reply_to_message_id
361+
if reply_markup is not None:
362+
payload['reply_markup'] = _convert_markup(reply_markup)
363+
if allow_sending_without_reply is not None:
364+
payload['allow_sending_without_reply'] = allow_sending_without_reply
365+
if disable_notification is not None:
366+
payload['disable_notification'] = disable_notification
367+
if timeout:
368+
payload['connect-timeout'] = timeout
369+
return _make_request(token, method_url, params=payload)
370+
371+
348372
def send_dice(
349373
token, chat_id,
350374
emoji=None, disable_notification=None, reply_to_message_id=None,
@@ -1312,6 +1336,12 @@ def _convert_markup(markup):
13121336
return markup
13131337

13141338

1339+
def _convert_entites(entites):
1340+
if isinstance(entites[0], types.JsonSerializable):
1341+
return [entity.to_json() for entity in entites]
1342+
return entites
1343+
1344+
13151345
def convert_input_media(media):
13161346
if isinstance(media, types.InputMedia):
13171347
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)