mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
Merge pull request #1067 from Aazerra/copyMessage_method
Added copyMessage method
This commit is contained in:
commit
74fb8258b6
@ -752,6 +752,29 @@ class TeleBot:
|
||||
return types.Message.de_json(
|
||||
apihelper.forward_message(self.token, chat_id, from_chat_id, message_id, disable_notification, timeout))
|
||||
|
||||
def copy_message(self, chat_id, from_chat_id, message_id, caption=None, parse_mode=None, caption_entities=None,
|
||||
disable_notification=None, reply_to_message_id=None, allow_sending_without_reply=None, reply_markup=None,
|
||||
timeout=None):
|
||||
"""
|
||||
Use this method to copy messages of any kind.
|
||||
:param chat_id: which chat to forward
|
||||
:param from_chat_id: which chat message from
|
||||
:param message_id: message id
|
||||
:param caption:
|
||||
:param parse_mode:
|
||||
:param caption_entities:
|
||||
:param disable_notification:
|
||||
:param reply_to_message_id:
|
||||
:param allow_sending_without_reply:
|
||||
:param reply_markup:
|
||||
:param timeout:
|
||||
:return: API reply.
|
||||
"""
|
||||
return types.MessageID.de_json(
|
||||
apihelper.copy_message(self.token, chat_id, from_chat_id, message_id, caption, parse_mode, caption_entities,
|
||||
reply_to_message_id, allow_sending_without_reply, reply_markup,
|
||||
disable_notification, timeout))
|
||||
|
||||
def delete_message(self, chat_id, message_id, timeout=None):
|
||||
"""
|
||||
Use this method to delete message. Returns True on success.
|
||||
@ -865,7 +888,7 @@ class TeleBot:
|
||||
"""
|
||||
parse_mode = self.parse_mode if (parse_mode is None) else parse_mode
|
||||
|
||||
return types.Message.de_json(
|
||||
return types.Message.de_json(
|
||||
apihelper.send_data(self.token, chat_id, data, 'document', reply_to_message_id, reply_markup,
|
||||
parse_mode, disable_notification, timeout, caption, thumb))
|
||||
|
||||
@ -2269,6 +2292,11 @@ class AsyncTeleBot(TeleBot):
|
||||
def forward_message(self, *args, **kwargs):
|
||||
return TeleBot.forward_message(self, *args, **kwargs)
|
||||
|
||||
@util.async_dec()
|
||||
def copy_message(self, *args, **kwargs):
|
||||
return TeleBot.copy_message(self, *args, **kwargs)
|
||||
|
||||
|
||||
@util.async_dec()
|
||||
def delete_message(self, *args):
|
||||
return TeleBot.delete_message(self, *args)
|
||||
|
@ -345,6 +345,30 @@ def forward_message(
|
||||
return _make_request(token, method_url, params=payload)
|
||||
|
||||
|
||||
def copy_message(token, chat_id, from_chat_id, message_id, caption=None, parse_mode=None, caption_entities=None,
|
||||
reply_to_message_id=None, allow_sending_without_reply=None, reply_markup=None,
|
||||
disable_notification=None, timeout=None):
|
||||
method_url = r'copyMessage'
|
||||
payload = {'chat_id': chat_id, 'from_chat_id': from_chat_id, 'message_id': message_id}
|
||||
if caption is not None:
|
||||
payload['caption'] = caption
|
||||
if parse_mode is not None:
|
||||
payload['parse_mode'] = parse_mode
|
||||
if caption_entities is not None:
|
||||
payload['caption_entities'] = _convert_entites(caption_entities)
|
||||
if reply_to_message_id is not None:
|
||||
payload['reply_to_message_id'] = reply_to_message_id
|
||||
if reply_markup is not None:
|
||||
payload['reply_markup'] = _convert_markup(reply_markup)
|
||||
if allow_sending_without_reply is not None:
|
||||
payload['allow_sending_without_reply'] = allow_sending_without_reply
|
||||
if disable_notification is not None:
|
||||
payload['disable_notification'] = disable_notification
|
||||
if timeout:
|
||||
payload['connect-timeout'] = timeout
|
||||
return _make_request(token, method_url, params=payload)
|
||||
|
||||
|
||||
def send_dice(
|
||||
token, chat_id,
|
||||
emoji=None, disable_notification=None, reply_to_message_id=None,
|
||||
@ -1312,6 +1336,12 @@ def _convert_markup(markup):
|
||||
return markup
|
||||
|
||||
|
||||
def _convert_entites(entites):
|
||||
if isinstance(entites[0], types.JsonSerializable):
|
||||
return [entity.to_json() for entity in entites]
|
||||
return entites
|
||||
|
||||
|
||||
def convert_input_media(media):
|
||||
if isinstance(media, types.InputMedia):
|
||||
return media.convert_input_media()
|
||||
|
@ -261,6 +261,19 @@ class Chat(JsonDeserializable):
|
||||
self.location = location
|
||||
|
||||
|
||||
class MessageID(JsonDeserializable):
|
||||
@classmethod
|
||||
def de_json(cls, json_string):
|
||||
if(json_string is None):
|
||||
return None
|
||||
obj = cls.check_json(json_string)
|
||||
message_id = obj['message_id']
|
||||
return cls(message_id)
|
||||
|
||||
def __init__(self, message_id):
|
||||
self.message_id = message_id
|
||||
|
||||
|
||||
class Message(JsonDeserializable):
|
||||
@classmethod
|
||||
def de_json(cls, json_string):
|
||||
@ -548,7 +561,7 @@ class Message(JsonDeserializable):
|
||||
return self.__html_text(self.caption, self.caption_entities)
|
||||
|
||||
|
||||
class MessageEntity(JsonDeserializable):
|
||||
class MessageEntity(Dictionaryable, JsonSerializable, JsonDeserializable):
|
||||
@classmethod
|
||||
def de_json(cls, json_string):
|
||||
if (json_string is None): return None
|
||||
@ -569,6 +582,17 @@ class MessageEntity(JsonDeserializable):
|
||||
self.user = user
|
||||
self.language = language
|
||||
|
||||
def to_json(self):
|
||||
return json.dumps(self.to_dict())
|
||||
|
||||
def to_dict(self):
|
||||
return {"type": self.type,
|
||||
"offset": self.offset,
|
||||
"length": self.length,
|
||||
"url": self.url,
|
||||
"user": self.user,
|
||||
"language": self.language}
|
||||
|
||||
|
||||
class Dice(JsonSerializable, Dictionaryable, JsonDeserializable):
|
||||
@classmethod
|
||||
|
@ -293,6 +293,13 @@ class TestTeleBot:
|
||||
ret_msg = tb.forward_message(CHAT_ID, CHAT_ID, msg.message_id)
|
||||
assert ret_msg.forward_from
|
||||
|
||||
def test_copy_message(self):
|
||||
text = 'CI copy_message Test Message'
|
||||
tb = telebot.TeleBot(TOKEN)
|
||||
msg = tb.send_message(CHAT_ID, text)
|
||||
ret_msg = tb.copy_message(CHAT_ID, CHAT_ID, msg.message_id)
|
||||
assert ret_msg
|
||||
|
||||
def test_forward_message_dis_noti(self):
|
||||
text = 'CI forward_message Test Message'
|
||||
tb = telebot.TeleBot(TOKEN)
|
||||
|
Loading…
Reference in New Issue
Block a user