Exemplo n.º 1
0
    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
Exemplo n.º 2
0
    def on_hook(cls, data):
        text = data.get('text')

        try:
            exchange = Exchange.objects.get(out_id=data.get('from_id'))
        except Exchange.DoesNotExist:
            exchange = Exchange(out_id=data.get('from_id'))
            exchange.save()

        if cls.match_command('/start', text):
            if exchange.title and exchange.phone:
                exchange.active = True
                exchange.save()
            cls.send(
                chat_id=data.get('chat_id'),
                text=Text.format('EX_MSG_WELCOME'),
                reply_markup=ReplyKeyboardHide().to_json()
            )
            return

        if cls.match_command('/id', text):
            cls.send(
                chat_id=data.get('chat_id'),
                text=Text.format('EX_TLGRM_ID', data.get('chat_id')),
                reply_markup=ReplyKeyboardHide().to_json()
            )
            return
        elif cls.match_command('/stop', text):
            exchange.active = False
            exchange.save()
            cls.send(
                chat_id=data.get('chat_id'),
                text=Text.format('EX_MSG_STOP'),
                reply_markup=ReplyKeyboardHide().to_json()
            )
            return

        data['exchange'] = exchange
        data['exchange_id'] = exchange.get_id()

        if cls.match_command('/update', text):
            cls.updating[exchange.get_id()] = {'step': 0}
        elif cls.match_command('/confirm', text):
            data['args'] = cls.match_command('/confirm', text).get('args')
            cls._confirm(data)
            return

        if exchange.get_id() in cls.updating:
            cls._updating(data)
Exemplo n.º 3
0
    def process(self, bot):
        offer_type = bot.match_command(
            bot.t(['SELL_CREDIT', 'SELL_FUTURES', 'SELL_FACTORING']))
        if offer_type and self.type is None:
            self.set_type(bot, offer_type.get('command'))
            if self.type == self.TYPE_CREDIT:
                return bot.send(bot.t('CREDIT_ENTER_DESC'),
                                reply_markup=ReplyKeyboardHide())
            elif self.type == self.TYPE_FUTURES:
                return bot.send(bot.t('FUTURES_ENTER_DESC'),
                                reply_markup=ReplyKeyboardHide())
            elif self.type == self.TYPE_FACTORING:
                return bot.send(bot.t('FACTORING_ENTER_DESC'),
                                reply_markup=ReplyKeyboardHide())

        if self.type == self.TYPE_CREDIT:
            self._process_credit(bot)
        elif self.type == self.TYPE_FUTURES:
            self._process_futures(bot)
        elif self.type == self.TYPE_FACTORING:
            self._process_factoring(bot)
Exemplo n.º 4
0
 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)
Exemplo n.º 5
0
    def _on_choose_type(cls, **kwargs):
        text = kwargs.get('text')
        request = kwargs.get('request')

        currencies = {}
        for item in Currency.objects:
            currencies[item.direct + '_' + item.code.lower()] = item.value

        for k, v in awa_client.CURRENCY_EXCHANGE.items():
            if text == v:
                request.type = k
                request.step = awa_client.STEP_INPUT_AMOUNT
                request.course = currencies.get(k)
                request.save()

                cls.send(chat_id=kwargs.get('chat_id'),
                         text=awa_client.CURRENCY_EXCHANGE_QUESTION.get(k),
                         reply_markup=ReplyKeyboardHide().to_json())

                return
        cls._on_start(**kwargs)
Exemplo n.º 6
0
    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'))
Exemplo n.º 7
0
 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)
Exemplo n.º 8
0
 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)
Exemplo n.º 9
0
    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)
Exemplo n.º 10
0
    def _confirm(cls, data=None, request=None, exchange=None, external_id=None):
        if data is None:
            data = {}
        if exchange is None and request is None and external_id is None:
            number = utils.to_int(data.get('args')[0], -1)
            if number < 0:
                cls.send(
                    chat_id=data.get('chat_id'),
                    text=Text.format('EX_BAD_NUMBER'),
                    reply_markup=ReplyKeyboardHide().to_json()
                )
                return

            try:
                external_id = utils.to_int(data.get('args')[1], -1)
                if external_id < 0:
                    raise Exception
            except:
                cls.send(
                    chat_id=data.get('chat_id'),
                    text=Text.format('EX_BAD_EXTERNAL_ID'),
                    reply_markup=ReplyKeyboardHide().to_json()
                )
                return

            try:
                request = Request.objects(number=number).get()
            except:
                cls.send(
                    chat_id=data.get('chat_id'),
                    text=Text.format('EX_REQUEST_NOT_FOUND'),
                    reply_markup=ReplyKeyboardHide().to_json()
                )
                return
            if request.fail_sended:
                cls.send(
                    chat_id=data.get('chat_id'),
                    text=Text.format('EX_REQUEST_TIMEOUT'),
                    reply_markup=ReplyKeyboardHide().to_json()
                )
                return

            exchange = data['exchange']

        if request.exchange is None:
            request.exchange = exchange
            request.step = STEP_SEARCH_SUCCESS
            request.external_id = external_id
            try:
                request.distance = vincenty(request.geo.get('coordinates'),
                                            exchange.geo.get('coordinates')).meters / 1000.0
            except:
                pass
            request.user = request.user
            request.save()

            cls.send(
                chat_id=exchange.out_id,
                text=Text.format('EX_SUCCESS', external_id),
                reply_markup=ReplyKeyboardHide().to_json()
            )

            from bots.awa_client import AwaClientBot
            AwaClientBot.send(
                chat_id=request.user.out_id,
                text=Text.format('CL_SUCCESS_RESERV', exchange.address, exchange.phone, request.external_id)
            )
            AwaClientBot.sendLocation(
                chat_id=request.user.out_id,
                latitude=exchange.geo.get('coordinates')[0],
                longitude=exchange.geo.get('coordinates')[1]
            )

            time = datetime.datetime.now()
            time += datetime.timedelta(seconds=Interval.seconds('reserve_time'))

            AwaClientBot.send(
                chat_id=request.user.out_id,
                text=Text.format('CL_SUCCESS_WARNING', time.strftime('%d.%m.%Y %H:%M'))
            )
        else:
            cls.send(
                chat_id=data.get('chat_id'),
                text=Text.format('EX_ALREADY_SEND'),
                reply_markup=ReplyKeyboardHide().to_json()
            )