def test_de_json(self, bot): json_dict = { "url": self.url, "has_custom_certificate": self.has_custom_certificate, "pending_update_count": self.pending_update_count, "last_error_date": self.last_error_date, "max_connections": self.max_connections, "allowed_updates": self.allowed_updates, "ip_address": self.ip_address, "last_synchronization_error_date": self.last_synchronization_error_date, } webhook_info = WebhookInfo.de_json(json_dict, bot) assert webhook_info.url == self.url assert webhook_info.has_custom_certificate == self.has_custom_certificate assert webhook_info.pending_update_count == self.pending_update_count assert isinstance(webhook_info.last_error_date, datetime) assert webhook_info.last_error_date == from_timestamp(self.last_error_date) assert webhook_info.max_connections == self.max_connections assert webhook_info.allowed_updates == self.allowed_updates assert webhook_info.ip_address == self.ip_address assert isinstance(webhook_info.last_synchronization_error_date, datetime) assert webhook_info.last_synchronization_error_date == from_timestamp( self.last_synchronization_error_date ) none = WebhookInfo.de_json(None, bot) assert none is None
def de_json(cls, data: Optional[JSONDict], bot: "Bot") -> Optional["WebhookInfo"]: """See :meth:`telegram.TelegramObject.de_json`.""" data = cls._parse_data(data) if not data: return None data["last_error_date"] = from_timestamp(data.get("last_error_date")) data["last_synchronization_error_date"] = from_timestamp( data.get("last_synchronization_error_date")) return cls(bot=bot, **data)
def chat_member_updated(): return ChatMemberUpdated( Chat(1, "chat"), User(1, "", False), from_timestamp(int(time.time())), ChatMember(User(1, "", False), ChatMember.OWNER), ChatMember(User(1, "", False), ChatMember.OWNER), )
def test_from_timestamp_aware(self, timezone): # we're parametrizing this with two different UTC offsets to exclude the possibility # of an xpass when the test is run in a timezone with the same UTC offset test_datetime = dtm.datetime(2019, 11, 11, 0, 26, 16, 10**5) datetime = timezone.localize(test_datetime) assert (tg_dtm.from_timestamp( 1573431976.1 - timezone.utcoffset(test_datetime).total_seconds()) == datetime)
def de_json(cls, data: Optional[JSONDict], bot: "Bot") -> Optional["ChatInviteLink"]: """See :meth:`telegram.TelegramObject.de_json`.""" data = cls._parse_data(data) if not data: return None data["creator"] = User.de_json(data.get("creator"), bot) data["expire_date"] = from_timestamp(data.get("expire_date", None)) return cls(**data)
def de_json(cls, data: Optional[JSONDict], bot: "Bot") -> Optional["VideoChatScheduled"]: """See :meth:`telegram.TelegramObject.de_json`.""" data = cls._parse_data(data) if not data: return None data["start_date"] = from_timestamp(data["start_date"]) return cls(**data, bot=bot)
def de_json(cls, data: Optional[JSONDict], bot: "Bot") -> Optional["Poll"]: """See :meth:`telegram.TelegramObject.de_json`.""" data = cls._parse_data(data) if not data: return None data["options"] = [PollOption.de_json(option, bot) for option in data["options"]] data["explanation_entities"] = MessageEntity.de_list(data.get("explanation_entities"), bot) data["close_date"] = from_timestamp(data.get("close_date")) return cls(**data)
def de_json(cls, data: Optional[JSONDict], bot: "Bot") -> Optional["ChatMemberUpdated"]: """See :meth:`telegram.TelegramObject.de_json`.""" data = cls._parse_data(data) if not data: return None data["chat"] = Chat.de_json(data.get("chat"), bot) data["from_user"] = User.de_json(data.get("from"), bot) data["date"] = from_timestamp(data.get("date")) data["old_chat_member"] = ChatMember.de_json(data.get("old_chat_member"), bot) data["new_chat_member"] = ChatMember.de_json(data.get("new_chat_member"), bot) data["invite_link"] = ChatInviteLink.de_json(data.get("invite_link"), bot) return cls(**data)
def de_json(cls, data: Optional[JSONDict], bot: "Bot") -> Optional["ChatJoinRequest"]: """See :meth:`telegram.TelegramObject.de_json`.""" data = cls._parse_data(data) if not data: return None data["chat"] = Chat.de_json(data.get("chat"), bot) data["from_user"] = User.de_json(data.get("from"), bot) data["date"] = from_timestamp(data.get("date", None)) data["invite_link"] = ChatInviteLink.de_json(data.get("invite_link"), bot) return cls(bot=bot, **data)
def de_json(cls, data: Optional[JSONDict], bot: "Bot") -> Optional["ChatMember"]: """See :meth:`telegram.TelegramObject.de_json`.""" data = cls._parse_data(data) if not data: return None data["user"] = User.de_json(data.get("user"), bot) data["until_date"] = from_timestamp(data.get("until_date", None)) _class_mapping: Dict[str, Type["ChatMember"]] = { cls.OWNER: ChatMemberOwner, cls.ADMINISTRATOR: ChatMemberAdministrator, cls.MEMBER: ChatMemberMember, cls.RESTRICTED: ChatMemberRestricted, cls.LEFT: ChatMemberLeft, cls.BANNED: ChatMemberBanned, } if cls is ChatMember: return _class_mapping.get(data["status"], cls)(**data, bot=bot) return cls(**data)
PreCheckoutQuery, ShippingQuery, Update, User, ) from telegram._utils.datetime import from_timestamp message = Message(1, None, Chat(1, ""), from_user=User(1, "", False), text="Text") chat_member_updated = ChatMemberUpdated( Chat(1, "chat"), User(1, "", False), from_timestamp(int(time.time())), ChatMemberOwner(User(1, "", False), True), ChatMemberOwner(User(1, "", False), True), ) chat_join_request = ChatJoinRequest( chat=Chat(1, Chat.SUPERGROUP), from_user=User(1, "first_name", False), date=from_timestamp(int(time.time())), bio="bio", ) params = [ { "message": message },
def test_from_timestamp_naive(self): datetime = dtm.datetime(2019, 11, 11, 0, 26, 16, tzinfo=None) assert tg_dtm.from_timestamp(1573431976, tzinfo=None) == datetime
def test_from_timestamp_none(self): assert tg_dtm.from_timestamp(None) is None