Fix Things on copyMessage

This commit is contained in:
Alireza 2021-01-12 11:17:53 +03:30
parent 58281f0a10
commit b684c4f60d
4 changed files with 52 additions and 8 deletions

View File

@ -7,6 +7,8 @@ import sys
import threading
import time
from telebot.types import MessageID
logger = logging.getLogger('TeleBot')
formatter = logging.Formatter(
'%(asctime)s (%(filename)s:%(lineno)d %(threadName)s) %(levelname)s - %(name)s: "%(message)s"'
@ -751,19 +753,24 @@ class TeleBot:
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,
reply_to_message_id=None, allow_sending_without_reply=None, reply_markup=None,
disable_notification=None, timeout=None):
# FIXME: rewrite the docstring
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 disable_notification:
: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.Message.de_json(
return 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))

View File

@ -336,11 +336,11 @@ def copy_message(token, chat_id, from_chat_id, message_id, caption=None, parse_m
if parse_mode is not None:
payload['parse_mode'] = parse_mode
if caption_entities is not None:
payload['caption_entities'] = caption_entities
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'] = reply_markup
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:
@ -1309,6 +1309,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()

View File

@ -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

View File

@ -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)