def __init__(self, account_number, amount, money_request_type, msg=None): from balebot.models.messages import PhotoMessage, TextMessage if msg and isinstance(msg, PhotoMessage): self.msg = msg elif msg and isinstance(msg, TextMessage): self.msg = PhotoMessage(file_id=DefaultPhoto.file_id, access_hash=DefaultPhoto.access_hash, name=DefaultPhoto.name, file_size=DefaultPhoto.file_size, mime_type=DefaultPhoto.mime_type, thumb=DefaultPhoto.thumb, caption_text=msg, width=DefaultPhoto.width, height=DefaultPhoto.height, ext_width=DefaultPhoto.ext_width, ext_height=DefaultPhoto.ext_height, file_storage_version=1) else: self.msg = PhotoMessage(file_id=DefaultPhoto.file_id, access_hash=DefaultPhoto.access_hash, name=DefaultPhoto.name, file_size=DefaultPhoto.file_size, mime_type=DefaultPhoto.mime_type, thumb=DefaultPhoto.thumb, caption_text=TextMessage(""), width=DefaultPhoto.width, height=DefaultPhoto.height, ext_width=DefaultPhoto.ext_width, ext_height=DefaultPhoto.ext_height, file_storage_version=1) self.account_number = str(account_number) if amount: self.amount = str(amount) self.regex_amount = "[" + str(amount) + "]" else: self.amount = "0" self.regex_amount = "[]" self.money_request_type = str(money_request_type)
async def send_alarm(bot=dispatcher.bot): while (True): current_time = jdatetime.datetime.now() if current_time.hour == 21: for debt in search_debt_for_send(current_time): user_peer = Peer.load_from_json(debt.user_id) target_photo = get_photo_by_id(debt.photo_id) photo_message = PhotoMessage( target_photo.file_id, target_photo.access_hash, target_photo.name, target_photo.file_size, target_photo.mime_type, target_photo.thumb, target_photo.width, target_photo.height, target_photo.ext_width, target_photo.ext_height, target_photo.file_storage_version, TextMessage(target_photo.caption_text), target_photo.checksum, target_photo.algorithm) photo_message.caption_text = TextMessage(debt.creditor_name) purchase_message = PurchaseMessage(photo_message, debt.card_number, debt.amount, MoneyRequestType.normal) bot.send_message(purchase_message, user_peer, success_callback=success, failure_callback=failure) bot.send_message(TemplateMessage( TextMessage("بازگشت به منوی اصلی "), btn_list=button_list), user_peer, success_callback=success, failure_callback=failure) for alarm in search_alarm_for_send(current_time): user_peer = Peer.load_from_json(alarm.user_id) update_alarm_time(alarm) photo = PhotoMessage.load_from_json(alarm.photo_json) photo.caption_text = TextMessage(alarm.message) bot.send_message(photo, user_peer, success_callback=success, failure_callback=failure) bot.send_message(TemplateMessage( TextMessage("بازگشت به منوی اصلی "), btn_list=button_list), user_peer, success_callback=success, failure_callback=failure) await asyncio.sleep(30)
def db_pulling(): my_logger.info(LogMessage.reading_message_db) current_time = datetime.datetime.now() message_tobe_sent = session.query(Message).filter( Message.sent != MessageStatus.sent).filter( # not sent messages cast(Message.sending_time, Date) == current_time.date()).filter( Message.sending_time < current_time).all() for msg in message_tobe_sent: my_logger.info(LogMessage.db_has_message_to_send) notification = msg.notification user_peer = UserPeer(notification.peer_id, notification.peer_access_hash) if notification.file_id: message = PhotoMessage(file_id=notification.file_id, access_hash=notification.file_access_hash, name=BotMessages.photo_name, file_size=notification.file_size, mime_type=MimeType.image, caption_text=TextMessage(notification.text), file_storage_version=1, thumb=None) else: message = TextMessage(notification.text) if notification.type == ResponseValue.debt: if isinstance(message, TextMessage): message = PhotoMessage(file_id=DefaultPhoto.file_id, access_hash=DefaultPhoto.access_hash, name=BotMessages.photo_name, file_size=DefaultPhoto.file_size, mime_type=MimeType.image, caption_text=TextMessage( notification.text), file_storage_version=1, thumb=None) final_message = PurchaseMessage( msg=message, account_number=notification.card_number, amount=notification.money_amount, money_request_type=MoneyRequestType.normal) else: final_message = message loop.call_soon(send_message, final_message, user_peer, msg, SendingAttempt.first) loop.call_later(Config.db_message_checking_interval, db_pulling)
def success_upload_image(user_data, server_response): file_id = str(server_response.get("file_id", None)) access_hash = str(server_response.get("user_id", None)) photo_message = PhotoMessage(file_id=file_id, access_hash=access_hash, name=name, file_size=file_size, mime_type=mime_type, file_storage_version=file_storage_version, width=width, height=height, caption_text=TextMessage(text=caption_text), thumb=thumb) self.send_message(message=photo_message, peer=user_peer, success_callback=success_callback, failure_callback=failure_callback, kwargs=kwargs)
class BotMessages: money_request_photo_message = PhotoMessage(name="image.png", file_id="8052119474392862211", access_hash="201707397", file_size=210162, mime_type="image/png", thumb=BotTexts.thumb, file_storage_version=1, caption_text=TextMessage(""), height=424, width=728)
def send_message(id_index, loop): if id_index >= len(rows): self.check_next = True return 0 row = rows[id_index] content_to_category_obj = row.content_to_category[0] category = get_category_by_id( content_to_category_obj.category_id) logo = get_logo_by_id(row.logo_id) content_type = get_type_by_id(category.type_id) text_message = TextMessage( ReadyMessage.content_template.format( row.name, row.description, category.name.replace(" ", "_"), content_type.name.replace(" ", "_"), row.nick_name, row.nick_name)) photo_message = PhotoMessage(logo.file_id, logo.access_hash, "channel", logo.file_size, "image/jpeg", None, 250, 250, file_storage_version=1, caption_text=text_message) user_peer = Peer( PeerType.group, peer_id=BotConfig.channel_id, access_hash=BotConfig.channel_access_hash) kwargs = { "message": text_message, "content_id": row.id, "user_peer": user_peer, "try_times": 1 } self.bot.send_message( message=photo_message, peer=user_peer, success_callback=self.success_sent_message, failure_callback=self.failure_sent_message, kwargs=kwargs) id_index += 1 loop.call_later(BotConfig.send_delay, send_message, id_index, loop)
def get_file_message(self): if self.file_access_hash is not None and self.file_id is not None: if self.mimetype.startswith("image/"): return PhotoMessage(file_id=self.file_id, access_hash=self.file_access_hash, name=self.name, file_size=sys.getsizeof(self.file), mime_type=self.mimetype, caption_text=TextMessage(self.caption), file_storage_version=1, thumb=None) else: return DocumentMessage(file_id=self.file_id, access_hash=self.file_access_hash, name=self.name, file_size=sys.getsizeof(self.file), mime_type=self.mimetype, caption_text=TextMessage(text=self.caption), file_storage_version=1) else: return None
def file_upload_success(result, user_data): print("upload was successful : ", result) print(user_data) file_id = str(user_data.get("file_id", None)) access_hash = str(user_data.get("user_id", None)) file_size = str(user_data.get("file_size", None)) photo_message = PhotoMessage( file_id=file_id, access_hash=access_hash, name="Bale", file_size='11337', mime_type="image/jpeg", caption_text=TextMessage(text="Bale"), file_storage_version=1, thumb=None) bot.send_message(photo_message, user_peer, success_callback=success, failure_callback=failure)
class PurchaseMessage(BaseMessage): def __init__(self, account_number, amount, money_request_type, msg=None): from balebot.models.messages import PhotoMessage, TextMessage if msg and isinstance(msg, PhotoMessage): self.msg = msg elif msg and isinstance(msg, TextMessage): self.msg = PhotoMessage(file_id=DefaultPhoto.file_id, access_hash=DefaultPhoto.access_hash, name=DefaultPhoto.name, file_size=DefaultPhoto.file_size, mime_type=DefaultPhoto.mime_type, thumb=DefaultPhoto.thumb, caption_text=msg, width=DefaultPhoto.width, height=DefaultPhoto.height, ext_width=DefaultPhoto.ext_width, ext_height=DefaultPhoto.ext_height, file_storage_version=1) else: self.msg = PhotoMessage(file_id=DefaultPhoto.file_id, access_hash=DefaultPhoto.access_hash, name=DefaultPhoto.name, file_size=DefaultPhoto.file_size, mime_type=DefaultPhoto.mime_type, thumb=DefaultPhoto.thumb, caption_text=TextMessage(""), width=DefaultPhoto.width, height=DefaultPhoto.height, ext_width=DefaultPhoto.ext_width, ext_height=DefaultPhoto.ext_height, file_storage_version=1) self.account_number = str(account_number) if amount: self.amount = str(amount) self.regex_amount = "[" + str(amount) + "]" else: self.amount = "0" self.regex_amount = "[]" self.money_request_type = str(money_request_type) def get_json_object(self): data = { "$type": MessageType.purchase_message, "msg": self.msg.get_json_object(), "accountNumber": self.account_number, "amount": self.amount, "regexAmount": self.regex_amount, "moneyRequestType": { "$type": self.money_request_type } } return data def get_json_str(self): return json_handler.dumps(self.get_json_object()) @classmethod def load_from_json(cls, json): if isinstance(json, dict): json_dict = json elif isinstance(json, str): json_dict = json_handler.loads(json) else: raise ValueError(Error.unacceptable_json) msg = message_factory.MessageFactory.create_message(json_dict.get('msg', None)) account_number = json_dict.get('accountNumber', None) amount = json_dict.get('amount', None) if (msg is None) or (not account_number) or (amount is None): raise ValueError(Error.none_or_invalid_attribute) return cls(msg=msg, account_number=account_number, amount=amount)
class PurchaseMessage(BaseMessage): def __init__(self, account_number, amount, money_request_type, msg=None): from balebot.models.messages import PhotoMessage, TextMessage if msg and isinstance(msg, PhotoMessage): self.msg = msg elif msg and isinstance(msg, TextMessage): self.msg = PhotoMessage(file_id=DefaultPhoto.file_id, access_hash=DefaultPhoto.access_hash, name=DefaultPhoto.name, file_size=DefaultPhoto.file_size, mime_type=DefaultPhoto.mime_type, thumb=DefaultPhoto.thumb, caption_text=msg, width=DefaultPhoto.width, height=DefaultPhoto.height, ext_width=DefaultPhoto.ext_width, ext_height=DefaultPhoto.ext_height, file_storage_version=1) else: self.msg = PhotoMessage(file_id=DefaultPhoto.file_id, access_hash=DefaultPhoto.access_hash, name=DefaultPhoto.name, file_size=DefaultPhoto.file_size, mime_type=DefaultPhoto.mime_type, thumb=DefaultPhoto.thumb, caption_text=TextMessage(""), width=DefaultPhoto.width, height=DefaultPhoto.height, ext_width=DefaultPhoto.ext_width, ext_height=DefaultPhoto.ext_height, file_storage_version=1) self.account_number = str(account_number) if amount: self.amount = str(amount) self.regex_amount = "[" + str(amount) + "]" else: self.amount = "0" self.regex_amount = "[]" self.money_request_type = str(money_request_type) def get_json_object(self): data = { "$type": MessageType.purchase_message, "msg": self.msg.get_json_object(), "accountNumber": self.account_number, "amount": self.amount, "regexAmount": self.regex_amount, "moneyRequestType": { "$type": self.money_request_type } } return data def get_json_str(self): return json_handler.dumps(self.get_json_object()) @classmethod def load_from_json(cls, json): if isinstance(json, dict): json_dict = json elif isinstance(json, str): json_dict = json_handler.loads(json) else: raise ValueError(Error.unacceptable_json) msg = message_factory.MessageFactory.create_message( json_dict.get('msg', None)) account_number = json_dict.get('accountNumber', None) amount = json_dict.get('amount', None) if (msg is None) or (not account_number) or (amount is None): raise ValueError(Error.none_or_invalid_attribute) return cls(msg=msg, account_number=account_number, amount=amount)