def make_call(self): """ Метод инициирует звонок """ url = self.base_url + 'calls/makecall/' print('\nurl', url) phone = get_numbers(self.params.get('phone')) if not phone: self.errors['phone'] = 'Номер отсутствует' user, errors = self.get_user() headers = self.get_headers(user) data = { 'from': self.display_number, 'to': '+%s' % phone, } log = Log.objects.create(event_type='make_call', to_number=phone) rq = requests.post(url, headers=headers, data=data) rq_content = json.loads(rq.content.decode('utf-8')) print('rq_content', rq_content) log.from_number = rq_content['data']['caller']['name'] log.data = dict( data=data, status_code=rq.status_code, reason=rq.reason, content=rq_content, ) log.save() return log.id, self.errors
def get(self, request, *args, **kwargs): self.params = self.request.GET.dict() self.params.update( person=self.request.user.person if self.request.user.person else None ) print('GET params', self.params) telephony = MightyCall(params=self.params) try: answer, errors = getattr(telephony, self.event)() except AttributeError: answer, errors = None, dict(event='Нет такого метода (%s)' % self.event) if errors: return JsonResponse(dict( message=dict(type='error', errors=errors) ), safe=False) context = dict( answer=answer, errors=errors, message=dict(type='success', text='Звонок на +%s' % get_numbers(self.params.get('phone'))) ) if request.GET.get('debug'): from django.shortcuts import render_to_response return render_to_response('debug.jinja2', locals()) return JsonResponse(context, safe=False)
def clean_phone(self): data = get_numbers(self.cleaned_data.get('phone')) if len(data) != 11: raise forms.ValidationError('Номер телефона 11 цифр') if Person.objects.filter(phones__value=data).exists(): raise forms.ValidationError('Этот телефон уже используется') return data
def handle(self, *args, **options): api_id = '1F7A19AE-9F45-C080-89AD-28B3A2AD1632' print('\nStart sms send\n') deal_remind_1day() now = datetime.now().replace(minute=0, microsecond=0) for sms in Sms.objects.filter(status='wait'): timezone = sms.deal.branch.city.timezone if sms.deal else 0 local_time = now + timedelta(hours=timezone) if sms.phone: phone = sms.phone else: phone = str(sms.person.phones.first()) phone = get_numbers(phone) print('\n', sms.id, phone, sms) if not 9 <= local_time.hour < 20: # СМС отправляем только в рабочие часы print('Ждем рабочие часы: 8-20', now, local_time) continue string = 'https://sms.ru/sms/send' params = dict(api_id=api_id, to=phone, msg=sms.text, json=1) if SMS_TEST: params['test'] = 1 response = requests.get(url=string, params=params).json() if response['status'] == 'OK' and response['sms'][phone]['status']: sms.time_sent = datetime.now() sms.phone = phone try: sms.status_text = response['sms'][phone]['status_text'] except KeyError: pass if response['sms'][phone]['status'] == 'ERROR': sms.status = 'error' elif response['sms'][phone]['status'] == 'OK': sms.status = 'ok' sms.save() if sms.deal: sms.deal.sms_processed(sms, response) print('\nFinish sms send')
def handle(self, *args, **options): template_q = SmsTemplate.objects.get(name='2020-03') count = 0 list_related = getattr(Deal, 'list_related', []) queryset = Deal.objects \ .filter(branch__city__name='Москва', stage__name='cancel') \ .select_related(*list_related) \ .order_by('start_datetime') for deal in queryset.all(): print('\n', deal.id, deal.start_datetime) for person in deal.persons.all(): try: phone = get_numbers(person.phones.first()) except: continue if phone and (phone[0] == '7' and len(phone) == 11): print(phone) else: print('no!', phone, len(phone)) continue if Sms.objects.filter(phone=phone, template=template_q).exists(): print('exists') continue Sms.objects.create(deal=deal, person=person, phone=phone, text=template_q.template, template=template_q) count += 1 if count == 1000: break print('\nFinish sms spam') print('count:', count)
def save(self, *args, **kwargs): self.value = get_numbers(self.value) if self.value: if self.value[0] == '8': self.value = '7%s' % self.value[1:] super().save(*args, **kwargs)
def clean_phone(self): data = get_numbers(self.cleaned_data.get('phone')) if len(data) != 11: raise forms.ValidationError('Номер телефона 11 цифр') return data
def dispatch(self, request, *args, **kwargs): self.sip_id = self.request.user.person.sip_id self.phone = get_numbers(self.request.GET.get('phone')) return super(MangoCall, self).dispatch(request, *args, **kwargs)