def _on_confirm(cls, **kwargs): text = kwargs.get('text') request = kwargs.get('request') user = kwargs.get('user') if text == Text.format('CL_CONFIRM_YES'): request.step = awa_client.STEP_ENTER_GEO request.save() cls.send(chat_id=kwargs.get('chat_id'), text=Text.format('CL_SEND_GEO'), reply_markup=ReplyKeyboard(keyboard=[[ KeyboardButton(text=u'Отправить гео-позицию', request_location=True) ]]).to_json()) elif text == Text.format('CL_CONFIRM_NO'): user.current_request = None user.save() cls.send(chat_id=kwargs.get('chat_id'), text=Text.format('CL_OPERATION_CANCEL'), reply_markup=cls._start_keyboard()) else: keyboard = ReplyKeyboard(keyboard=[[ Text.format('CL_CONFIRM_YES'), Text.format('CL_CONFIRM_NO') ]]) cls.send(chat_id=kwargs.get('chat_id'), text=Text.format('CL_CONFIRM_REPEAT'), reply_markup=keyboard.to_json())
def _process_credit(self, bot): if self.description is None: self.description = bot.text bot.send(bot.t('CREDIT_ENTER_ZIP')) elif self.zip_code is None: self.zip_code = bot.text bot.send(bot.t('CREDIT_ENTER_REG_SERVICE')) elif self.reg_service is None: self.reg_service = bot.text bot.send(bot.t('CREDIT_ENTER_LOAN_ID')) elif self.options.get('loan_id') is None: self.options['loan_id'] = bot.text bot.send(bot.t('CREDIT_ENTER_LOAN_AMOUNT')) elif self.options.get('loan_amount') is None: self.options['loan_amount'] = utils.to_int(bot.text, None) if self.options['loan_amount'] is None: return bot.send(bot.t('ENTER_NUMBER')) bot.send(bot.t('CREDIT_ENTER_INTEREST_RATE')) elif self.options.get('interest_rate') is None: self.options['interest_rate'] = bot.text bot.send(bot.t('CREDIT_ENTER_LOAN_LENGTH')) elif self.options.get('loan_length') is None: self.options['loan_length'] = bot.text bot.send(bot.t('CREDIT_ENTER_LOAN_STATUS'), reply_markup=ReplyKeyboard([[bot.t('LOAN_STATUS_EARLY')], [bot.t('LOAN_STATUS_NORMAL')], [bot.t('LOAN_STATUS_LATE')]])) elif self.options.get('loan_status') is None: self.options['loan_status'] = bot.text bot.send(bot.t('CREDIT_ENTER_SELLERS_WARRANTY'), reply_markup=ReplyKeyboard([[ bot.t('WARRANTY_FULL'), bot.t('WARRANTY_PARTLY'), bot.t('WARRANTY_NONE') ]], one_time_keyboard=True)) elif self.options.get('sellers_warranty') is None: self.options['sellers_warranty'] = bot.text bot.send(bot.t('CREDIT_ENTER_PRICE'), reply_markup=ReplyKeyboardHide()) elif self.price is None: self.price = utils.to_int(bot.text, None) if self.price is None: return bot.send(bot.t('ENTER_NUMBER')) self._create_contract_send(bot) else: self._create_contract_process(bot)
def _updating(cls, data): updating = cls.updating[data.get('exchange_id')] step = updating.get('step', 0) exchange = data['exchange'] kwargs = {} if step == 0: title = str(exchange.title) if title != 'None': keyboard = ReplyKeyboardHide(keyboard=[[title]]) kwargs['reply_markup'] = keyboard.to_json() cls.send(chat_id=data.get('chat_id'), text=Text.format('EX_MSG_ENTER_TITLE'), **kwargs) elif step == 1: exchange.title = data.get('text') exchange.save() phone = str(exchange.phone) if phone != 'None': keyboard = ReplyKeyboardHide(keyboard=[[phone]]) kwargs['reply_markup'] = keyboard.to_json() cls.send(chat_id=data.get('chat_id'), text=Text.format('EX_MSG_ENTER_PHONE'), **kwargs) elif step == 2: exchange.phone = data.get('text') exchange.save() keyboard = ReplyKeyboard(keyboard=[[KeyboardButton(text=u'Отправить гео-позицию', request_location=True)]]) kwargs['reply_markup'] = keyboard.to_json() cls.send(chat_id=data.get('chat_id'), text=Text.format('EX_MSG_ENTER_LOCATION'), **kwargs) elif step == 3: location = data.get('location') if location is None: cls.send(chat_id=data.get('chat_id'), text=Text.format('EX_MSG_ENTER_LOCATION')) return else: exchange.geo = [location.get('latitude'), location.get('longitude')] exchange.save() cls.send( chat_id=data.get('chat_id'), text=Text.format('EX_MSG_ENTER_ADDRESS'), reply_markup=ReplyKeyboardHide().to_json() ) elif step == 4: exchange.address = data.get('text') exchange.active = True exchange.save() cls.send( chat_id=data.get('chat_id'), text=Text.format('EX_MSG_UPDATED'), reply_markup=ReplyKeyboardHide().to_json() ) del cls.updating[data.get('exchange_id')] return updating['step'] = step + 1 cls.updating[data.get('exchange_id')] = updating
def _on_start(self, **kwargs): self.user.reset() self.user.save() welcome_text = kwargs.get('welcome_text') keyboard = ReplyKeyboard([[self.t('DEAL_SELL'), self.t('DEAL_BUY')]]) self.send(welcome_text if welcome_text else self.t('WELCOME'), reply_markup=kwargs.get('keyboard') if kwargs.get('keyboard') else keyboard)
def _on_hook(self, data): if self.match_command('/lang'): self.user.lang = None self.user.save() if self.user.lang is None and self._choose_lang(): return if self.match_command('/start'): return self._on_start(**data) elif self.match_command(self.t('THNX_UNDERSTAND')): data['welcome_text'] = self.t('CHOOSE_DIRECTION') return self._on_start(**data) if self.match_command(self.t('DEAL_SELL')): self.user.current_action = User.ACTION_SELL self.user.save() return self.send( self.t('DEAL_SELL_INTRO'), reply_markup=ReplyKeyboard( [[self.t('SELL_CREDIT')], [self.t('SELL_FUTURES')], [self.t('SELL_FACTORING')]], one_time_keyboard=True)) elif self.match_command(self.t('DEAL_BUY')): self.user.current_action = User.ACTION_BUY self.user.save() return self.send( self.t('DEAL_BUY_INTRO'), reply_markup=ReplyKeyboard( [[self.t('BUY_CREDIT')], [self.t('BUY_FUTURES')], [self.t('BUY_FACTORING')]], one_time_keyboard=True)) if self.user.current_action == User.ACTION_SELL: self._selling(**data) elif self.user.current_action == User.ACTION_BUY: self._buying(**data)
def _choose_lang(self): EN = emoji.emojize(':flag_for_United_States:') + ' English' RU = emoji.emojize(':flag_for_Russia:') + ' Русский' if self.match_command(EN): self.user.lang = Text.LANG_EN elif self.match_command(RU): self.user.lang = Text.LANG_RU else: self.send(self.t('CHOOSE_LANG'), reply_markup=ReplyKeyboard([[EN, RU]])) return True self.user.save() if self.user.current_action is None: self.text = '/start' else: return True
def _create_contract_process(self, bot): if bot.match_command(bot.t('YES_APPROVE')): bot.send(bot.t('REGISTER_BC_BEGIN'), reply_markup=ReplyKeyboardHide()) # рега в реестре yield gen.sleep(1) self.bc_hash = PasswordHelper.get_hash(datetime.now().isoformat()) # генерация картинок yield self.generate_img() self.salesman = self.salesman self.save() bot._on_start(welcome_text=bot.t('REGISTER_BC_END'), keyboard=ReplyKeyboard([[bot.t('THNX_UNDERSTAND')]])) elif bot.match_command(bot.t('NO_FEAR')): bot._on_start(welcome_text=bot.t('FEAR_BLOCKCHAIN_WIKI')) elif bot.match_command(bot.t('WHAT_IS_BLOCKCHAIN')): bot.send(bot.t('WHAT_IS_BLOCKCHAIN_WIKI'))
def _process_factoring(self, bot): if self.description is None: self.description = bot.text bot.send(bot.t('FACTORING_ENTER_ZIP')) elif self.zip_code is None: self.zip_code = bot.text bot.send(bot.t('FACTORING_ENTER_REG_SERVICE')) elif self.reg_service is None: self.reg_service = bot.text bot.send(bot.t('FACTORING_ENTER_LOAN_ID')) elif self.options.get('loan_id') is None: self.options['loan_id'] = bot.text bot.send(bot.t('FACTORING_PAY_REQS'), reply_markup=ReplyKeyboard([ [bot.t('FACTORING_REGRESS')], [bot.t('FACTORING_NO_REGRESS')], ], one_time_keyboard=True)) elif self.options.get('pay_reqs') is None: self.options['pay_reqs'] = bot.text bot.send(bot.t('FACTORING_TITLE_SUPPLIER'), reply_markup=ReplyKeyboardHide()) elif self.options.get('title_supplier') is None: self.options['title_supplier'] = bot.text bot.send(bot.t('FACTORING_SUM_REQS')) elif self.options.get('sum_reqs') is None: self.options['sum_reqs'] = bot.text bot.send(bot.t('FACTORING_DATE_REQS_PAY')) elif self.options.get('date_reqs_pay') is None: self.options['date_reqs_pay'] = bot.text bot.send(bot.t('FACTORING_PRICE')) elif self.price is None: self.price = utils.to_int(bot.text, None) if self.price is None: return bot.send(bot.t('ENTER_NUMBER')) self._create_contract_send(bot) else: self._create_contract_process(bot)
def _process_futures(self, bot): if self.description is None: self.description = bot.text bot.send(bot.t('FUTURES_ENTER_ZIP')) elif self.zip_code is None: self.zip_code = bot.text bot.send(bot.t('FUTURES_ENTER_REG_SERVICE')) elif self.reg_service is None: self.reg_service = bot.text bot.send(bot.t('FUTURES_ENTER_LOAN_ID')) elif self.options.get('loan_id') is None: self.options['loan_id'] = bot.text bot.send(bot.t('FUTURES_CHOOSE_CONTRACT_TYPE'), reply_markup=ReplyKeyboard([ [bot.t('FUTURES_TYPE_SETTLEMENT')], [bot.t('FUTURES_TYPE_DELIVERABLE')], ], one_time_keyboard=True)) elif self.options.get('contract_type') is None: self.options['contract_type'] = bot.text bot.send(bot.t('FUTURES_ENTER_CONTRACT_SIZE'), reply_markup=ReplyKeyboardHide()) elif self.options.get('contract_size') is None: self.options['contract_size'] = bot.text bot.send(bot.t('FUTURES_CONTRACT_MATURITY')) elif self.options.get('contract_maturity') is None: self.options['contract_maturity'] = bot.text bot.send(bot.t('FUTURES_ENTER_DELIVERY_DATE')) elif self.options.get('delivery_date') is None: self.options['delivery_date'] = bot.text bot.send(bot.t('FUTURES_PRICE')) elif self.price is None: self.price = utils.to_int(bot.text, None) if self.price is None: return bot.send(bot.t('ENTER_NUMBER')) self._create_contract_send(bot) else: self._create_contract_process(bot)
def rating_request_send(): date = datetime.datetime.now(tz=timezone('UTC')) date -= datetime.timedelta( seconds=Interval.seconds('rating_request_timeout')) q = Q(rating_request_send=False) & \ Q(update_date__lte=date) & \ Q(step=awa_client.STEP_SEARCH_SUCCESS) requests = Request.objects(q).order_by('-update_date') users = set() for request in requests: if request.user.get_id() not in users: request.rating_request_send = True request.user = request.user request.save() keyboard = ReplyKeyboard(keyboard=[ [u'Не менял', emoji.emojize(':star:', True)], [ emoji.emojize(':star::star:', True), emoji.emojize(':star::star::star:', True) ], [ emoji.emojize(':star::star::star::star:', True), emoji.emojize(':star::star::star::star::star:', True) ] ]) yield DollarublSubBot(chat_id=request.user.out_id, parent_bot=SekonomBot()).send( Text.format('CL_REQUEST_RATING'), reply_markup=keyboard) users.add(request.user.get_id())
def _create_contract_send(self, bot): try: bot.send(bot.t('GENERATE_PREVIEW_START')) yield self.generate_img(lang=bot.user.lang) path = self.get_image_path(bot.user.lang) if not path: raise Exception with open(path, 'rb') as f: yield bot.send_photo(files=(('photo', path, f.read()), ), caption=bot.t('GENERATE_PREVIEW_END')) except Exception as e: bot.send(bot.t('GENERATE_PREVIEW_FAIL')) traceback.print_exc() bot.send(bot.t('SAIL_YOU_CREATE_CONTRACT'), reply_markup=ReplyKeyboard([[ bot.t('YES_APPROVE'), bot.t('NO_FEAR'), bot.t('WHAT_IS_BLOCKCHAIN') ]], one_time_keyboard=False))
def _on_start(cls, **kwargs): keyboard = [] currs = {} codes = set() for item in Currency.objects: codes.add(item.code.upper()) currs[item.code.upper() + '_' + item.direct] = item.value codes = sorted(list(codes), key=lambda x: ['USD', 'EUR', 'GBR', 'CHF'].index(x)) welcome_currencies = [] for code in codes: if code == 'USD': keyboard.append([ awa_client.CURRENCY_EXCHANGE[Request.TYPE_BUY_USD], awa_client.CURRENCY_EXCHANGE[Request.TYPE_SAIL_USD] ]) welcome_currencies.append('доллар {0}/{1}'.format( currs.get('USD_sale'), currs.get('USD_buy'))) elif code == 'EUR': keyboard.append([ awa_client.CURRENCY_EXCHANGE[Request.TYPE_BUY_EUR], awa_client.CURRENCY_EXCHANGE[Request.TYPE_SAIL_EUR] ]) welcome_currencies.append('евро {0}/{1}'.format( currs.get('EUR_sale'), currs.get('EUR_buy'))) elif code == 'GBR': keyboard.append([ awa_client.CURRENCY_EXCHANGE[Request.TYPE_BUY_GBR], awa_client.CURRENCY_EXCHANGE[Request.TYPE_SAIL_GBR] ]) welcome_currencies.append('фунт стерлингов {0}/{1}'.format( currs.get('GBR_sale'), currs.get('GBR_buy'))) elif code == 'CHF': keyboard.append([ awa_client.CURRENCY_EXCHANGE[Request.TYPE_BUY_CHF], awa_client.CURRENCY_EXCHANGE[Request.TYPE_SAIL_CHF] ]) welcome_currencies.append('швейцарский франк {0}/{1}'.format( currs.get('CHF_sale'), currs.get('CHF_buy'))) keyboard = ReplyKeyboard(keyboard=keyboard) cls.send(chat_id=kwargs.get('chat_id'), text=Text.format('CL_MSG_WELCOME', "\n".join(welcome_currencies)), reply_markup=keyboard.to_json()) user = kwargs.get('user') request = kwargs.get('request') if request.step > awa_client.STEP_ZERO: request = Request(user=user) request.step = awa_client.STEP_INPUT_OPERATION request.save() user.current_request = request user.save()
def _start_keyboard(cls): return ReplyKeyboard(keyboard=[[Text.format('CL_NEW_EXCHANGE')]], one_time_keyboard=False).to_json()
def _on_enter_geo(cls, **kwargs): request = kwargs.get('request') location = kwargs.get('location') text = kwargs.get('text') user = kwargs.get('user') if location is None and request.geo is None: cls.send(chat_id=kwargs.get('chat_id'), text=Text.format('CL_SEND_GEO')) else: if location is not None: location = [ location.get('latitude'), location.get('longitude') ] request.geo = location request.save() if options.enable_testing_question: keyboard = ReplyKeyboard(keyboard=[[ Text.format('CL_YES_REAL_RESERVE'), Text.format('CL_NO_TESTING') ]]) if request.step == awa_client.STEP_ENTER_GEO: request.step = awa_client.STEP_CONFIRM_NO_TESTING request.save() return cls.send(chat_id=kwargs.get('chat_id'), text=Text.format('CL_CONFIRM_TESTING'), reply_markup=keyboard.to_json()) elif request.step == awa_client.STEP_CONFIRM_NO_TESTING: if text == Text.format('CL_YES_REAL_RESERVE'): pass elif text == Text.format('CL_NO_TESTING'): request.step = awa_client.STEP_TEST_REQUEST request.save() user.current_request = None user.save() return cls.send(chat_id=kwargs.get('chat_id'), text=Text.format('CL_CANCEL_REQUEST'), reply_markup=cls._start_keyboard()) else: return cls.send(chat_id=kwargs.get('chat_id'), text=Text.format('CL_CONFIRM_TESTING'), reply_markup=keyboard.to_json()) request.step = awa_client.STEP_SEARCH_EXCHANGE request.save() min_distance = 0 is_found = False logger.info('Location: {0}'.format(repr(location))) for max_distance in [Interval.get('max_radius') * 1000.0]: exchanges = Exchange.objects(active=True, geo__near=request.geo, geo__min_distance=min_distance, geo__max_distance=max_distance) if len(exchanges) > 0: logger.info('Found {0} exchanges in {1} distance'.format( len(exchanges), max_distance)) is_found = True for exchange in exchanges: logger.info('Exchange location: {0}'.format( repr(exchange.geo))) from bots.awa_exchange import AwaExchangeBot course = request.course text = Text.format('EX_EXCHANGE_REQUEST', request.number, request.type, request.amount, '%.2f' % course) AwaExchangeBot.send(chat_id=exchange.out_id, text=text) break else: logger.warning( 'Not found exchanges in {0} distance'.format( max_distance)) min_distance = max_distance if not is_found: request.step = awa_client.STEP_SEARCH_FAIL request.save() user.current_request = None user.save() cls.send(chat_id=kwargs.get('chat_id'), text=Text.format( 'CL_RESERV' if is_found else 'CL_FAIL_RESERV'), reply_markup=cls._start_keyboard())
def _on_enter_amount(cls, **kwargs): text = kwargs.get('text') amount = utils.to_int(text, None) request = kwargs.get('request') if amount is None: cls.send(chat_id=kwargs.get('chat_id'), text=u'Введите число') else: request.amount = amount request.step = awa_client.STEP_CONFIRM request.save() keyboard = ReplyKeyboard(keyboard=[[ Text.format('CL_CONFIRM_YES'), Text.format('CL_CONFIRM_NO') ]]) currencies = {} for item in Currency.objects: currencies[item.code.upper() + '_' + item.direct] = float( item.value) if request.type == Request.TYPE_BUY_USD: course = str(currencies.get('USD_buy', 0)) + u' руб/$' operation = [ u'покупаете', cls.num_split(request.amount), u'$', course, cls.num_split(request.amount * currencies.get('USD_buy', 0)) ] elif request.type == Request.TYPE_SAIL_USD: course = str(currencies.get('USD_sale', 0)) + u' руб/$' operation = [ u'продаете', cls.num_split(request.amount), u'$', course, cls.num_split(request.amount * currencies.get('USD_sale', 0)) ] elif request.type == Request.TYPE_BUY_EUR: course = str(currencies.get('EUR_buy', 0)) + u' руб/€' operation = [ u'покупаете', cls.num_split(request.amount), u'€', course, cls.num_split(request.amount * currencies.get('EUR_buy', 0)) ] elif request.type == Request.TYPE_SAIL_EUR: course = str(currencies.get('EUR_sale', 0)) + u' руб/€' operation = [ u'продаете', cls.num_split(request.amount), u'€', course, cls.num_split(request.amount * currencies.get('EUR_sale', 0)) ] elif request.type == Request.TYPE_BUY_GBR: course = str(currencies.get('GBR_buy', 0)) + u' руб/' + GBR operation = [ u'покупаете', cls.num_split(request.amount), GBR, course, cls.num_split(request.amount * currencies.get('GBR_buy', 0)) ] elif request.type == Request.TYPE_SAIL_GBR: course = str(currencies.get('GBR_sale', 0)) + u' руб/' + GBR operation = [ u'продаете', cls.num_split(request.amount), GBR, course, cls.num_split(request.amount * currencies.get('GBR_sale', 0)) ] elif request.type == Request.TYPE_BUY_CHF: course = str(currencies.get('CHF_buy', 0)) + u' руб/' + CHF operation = [ u'покупаете', cls.num_split(request.amount), CHF, course, cls.num_split(request.amount * currencies.get('CHF_buy', 0)) ] elif request.type == Request.TYPE_SAIL_CHF: course = str(currencies.get('CHF_sale', 0)) + u' руб/' + CHF operation = [ u'продаете', cls.num_split(request.amount), CHF, course, cls.num_split(request.amount * currencies.get('CHF_sale', 0)) ] else: return cls.send(chat_id=kwargs.get('chat_id'), text=Text.format('CL_CONFIRM', *operation), reply_markup=keyboard.to_json())
def _buying(self, **kwargs): buy_keyboard = ReplyKeyboard([[ self.t('DEAL_BUY_PREV'), self.t('DEAL_BUY_NEW_SEARCH'), self.t('DEAL_BUY_NEXT'), ]], one_time_keyboard=False) if kwargs.get('callback_query'): try: offer = Offer.objects.get(id=kwargs.get('callback_query')) except Exception: return deal = Deal() deal.user = self.user deal.offer = offer deal.save() self.user.buy_step = User.STEP_BLOCKCHAIN self.user.current_deal = deal self.user.save() self.send(self.t('BUY_YOU_CREATE_CONTRACT'), reply_markup=ReplyKeyboard(keyboard=[[ self.t('YES_APPROVE'), self.t('NO_FEAR'), self.t('WHAT_IS_BLOCKCHAIN'), ]])) self.answer_callback_query( callback_query_id=kwargs.get('callback_query_id')) return if self.user.buy_step == User.STEP_BLOCKCHAIN: if self.match_command(self.t('YES_APPROVE')): self.send(self.t('REGISTER_BC_BEGIN'), reply_markup=ReplyKeyboardHide()) yield gen.sleep(5) self.user.current_deal.bc_hash = PasswordHelper.get_hash( datetime.now().isoformat()) self.user.current_deal.user = self.user.current_deal.user self.user.current_deal.save() return self._on_start(welcome_text=self.t('BEGIN_DEAL'), keyboard=ReplyKeyboard( [[self.t('THNX_UNDERSTAND')]])) elif self.match_command(self.t('NO_FEAR')): return self._on_start( welcome_text=self.t('FEAR_BLOCKCHAIN_WIKI')) elif self.match_command(self.t('WHAT_IS_BLOCKCHAIN')): return self.send(self.t('WHAT_IS_BLOCKCHAIN_WIKI')) offer_type = self.match_command( self.t(['BUY_CREDIT', 'BUY_FUTURES', 'BUY_FACTORING'])) if self.user.filter_buy_type is None: if not offer_type: return self.send(self.t('ENTER_CORRECT_CONTRACT_TYPE')) self.user.set_filter_buy_type(self, offer_type.get('command')) self.user.save() if self.user.filter_buy_type == Offer.TYPE_CREDIT: return self.send(self.t('DEAL_BUY_CREDIT_PRICE'), reply_markup=ReplyKeyboardHide()) elif self.user.filter_buy_type == Offer.TYPE_FUTURES: return self.send(self.t('DEAL_BUY_FUTURES_PRICE'), reply_markup=ReplyKeyboardHide()) elif self.user.filter_buy_type == Offer.TYPE_FACTORING: return self.send(self.t('DEAL_BUY_FACTORING_PRICE'), reply_markup=ReplyKeyboardHide()) elif self.user.filter_price is None: self.user.filter_price = utils.to_int(self.text, None) self.user.save() if self.user.filter_price is None: return self.send(self.t('ENTER_NUMBER')) return self.send(self.t('DEAL_BUY_MANUAL'), reply_markup=buy_keyboard) if self.match_command(self.t('DEAL_BUY_PREV')): self._prev_item(**kwargs) elif self.match_command(self.t('DEAL_BUY_NEW_SEARCH')): self.user.reset() self.user.save() self.text = self.t('DEAL_BUY') self._on_hook(kwargs) elif self.match_command(self.t('DEAL_BUY_NEXT')): self._next_item(**kwargs)