From 8424e12a9aceccdd7146051d3824e11b0db9d7b2 Mon Sep 17 00:00:00 2001 From: baoliang Date: Sat, 28 Nov 2020 18:23:56 +0800 Subject: [PATCH] add message --- polls/models.py | 8 ++++++++ polls/tasks.py | 23 +++++++++++++++++------ polls/urls.py | 3 ++- polls/utils.py | 40 ++++++++++++++++++++++++++++++++++------ polls/views/__init__.py | 2 +- polls/views/user.py | 22 ++++++++++++++++++++++ requirements.txt | 2 ++ 7 files changed, 86 insertions(+), 14 deletions(-) diff --git a/polls/models.py b/polls/models.py index 118bbb5..c966d9e 100644 --- a/polls/models.py +++ b/polls/models.py @@ -225,3 +225,11 @@ class SMSNotifyRecord(models.Model): added = models.DateField(auto_now_add=True) notice_id = models.CharField('notice_id', max_length=64) updated = models.DateTimeField(auto_now=True) + + +class VoiceNotifyRecord(models.Model): + id = models.UUIDField('id', primary_key=True, default=uuid.uuid4) + phone = models.CharField('phone', max_length=32) + added = models.DateField(auto_now_add=True) + notice_id = models.CharField('notice_id', max_length=64) + updated = models.DateTimeField(auto_now=True) \ No newline at end of file diff --git a/polls/tasks.py b/polls/tasks.py index a7c793a..2167442 100644 --- a/polls/tasks.py +++ b/polls/tasks.py @@ -1,8 +1,8 @@ from background_task import background from datetime import date, datetime, timedelta import time -from polls.models import Notice, SMSNotifyRecord -from polls.utils import sent_sms_notify +from polls.models import Notice, SMSNotifyRecord, VoiceNotifyRecord +from polls.utils import send_voice_notify, sent_sms_notify @background(remove_existing_tasks=True) @@ -12,18 +12,29 @@ def process_notify_task(): notices = Notice.objects.filter(is_read=False) for n in notices: two_hours_later = n.added + timedelta(minutes=2) + four_hours_later = n.added + timedelta(minutes=4) now = datetime.now() + phone = n.user.username + today = now.date() + hour = now.hour if two_hours_later < now: - phone = n.user.username - today = now.date() - hour = now.hour exists = SMSNotifyRecord.objects.filter( phone=phone, added=today, notice_id=n.id).exists() if exists: break if hour < 8 or hour > 20: break - print('============', phone, n.type, n.id) + print('============', "sms ", phone, n.type, n.id) SMSNotifyRecord.objects.create(phone=phone, notice_id=n.id) sent_sms_notify(phone, n.type) + if four_hours_later < now: + exists = VoiceNotifyRecord.objects.filter( + phone=phone, added=today, notice_id=n.id).exists() + if exists: + break + if hour < 8 or hour > 20: + break + print('============', "voice ", phone, n.type, n.id) + VoiceNotifyRecord.objects.create(phone=phone, notice_id=n.id) + send_voice_notify(phone, n.type) print('end task', time.process_time() - start) diff --git a/polls/urls.py b/polls/urls.py index 2f656b0..bf199ab 100644 --- a/polls/urls.py +++ b/polls/urls.py @@ -72,7 +72,8 @@ urlpatterns = [ name='polls_message_read'), path('app/intro.html', views.app_intro, name='polls_app_intro'), path('app/download/', views.download, name='polls_app_download'), - path('app/has/update/', views.has_update, name='polls_app_has_update') + path('app/has/update/', views.has_update, name='polls_app_has_update'), + path('app/profile/image/upload/', views.upload_image, name='polls_profile_upload'), ] tasks.process_notify_task(repeat=5) \ No newline at end of file diff --git a/polls/utils.py b/polls/utils.py index 6fc70e7..84107f6 100644 --- a/polls/utils.py +++ b/polls/utils.py @@ -6,11 +6,16 @@ from django.conf import settings from itertools import chain from yunpian_python_sdk.model import constant as YC from yunpian_python_sdk.ypclient import YunpianClient +from aliyunsdkdyvmsapi.request.v20170525 import SingleCallByTtsRequest +from aliyunsdkcore.client import AcsClient +import uuid +from aliyunsdkcore.profile import region_provider def sent_sms_code(phone, code): clnt = YunpianClient('304eb08353f7ebf00596737acfc31f53') - param = {YC.MOBILE: phone ,YC.TEXT:'【甘肃大未来科技】您的验证码是%s。如非本人操作,请忽略本短信' %(code,)} + param = {YC.MOBILE: phone, + YC.TEXT: '【甘肃大未来科技】您的验证码是%s。如非本人操作,请忽略本短信' % (code,)} r = clnt.sms().single_send(param) return r @@ -18,13 +23,35 @@ def sent_sms_code(phone, code): def sent_sms_notify(phone, type): clnt = YunpianClient('304eb08353f7ebf00596737acfc31f53') if type == 1: - param = {YC.MOBILE: phone ,YC.TEXT:'【甘肃大未来科技】政务新媒体管理APP有新消息,请及时登录处理。(若其他管理员已处理,请忽略)*测试*'} + param = {YC.MOBILE: phone, + YC.TEXT: '【甘肃大未来科技】政务新媒体管理APP有新消息,请及时登录处理。(若其他管理员已处理,请忽略)*测试*'} else: - param = {YC.MOBILE: phone ,YC.TEXT:'【甘肃大未来科技】政务新媒体管理APP有新任务,请及时登录处理。'} + param = {YC.MOBILE: phone, YC.TEXT: '【甘肃大未来科技】政务新媒体管理APP有新任务,请及时登录处理。'} r = clnt.sms().single_send(param) return r +acs_client = AcsClient("LTAI4GDaSp41vp1X4mCb7uCa", + "am8fsIou5TZULV7mEAIEUwYfxN3cHK", "cn-hangzhou") +region_provider.add_endpoint( + "Dyvmsapi", "cn-hangzhou", "dyvmsapi.aliyuncs.com") + +def send_voice_notify(phone): + business_id = uuid.uuid4() + ttsRequest = SingleCallByTtsRequest.SingleCallByTtsRequest() + # 申请的语音通知tts模板编码,必填 + ttsRequest.set_TtsCode("TTS_205890042") + # 设置业务请求流水号,必填。后端服务基于此标识区分是否重复请求的判断 + ttsRequest.set_OutId(business_id) + # 语音通知的被叫号码,必填。 + ttsRequest.set_CalledNumber(phone) + # 语音通知显示号码,必填。 + ttsRequest.set_CalledShowNumber("") + + # 调用tts文本呼叫接口,返回json + ttsResponse = acs_client.do_action_with_exception(ttsRequest) + return ttsResponse + def generate_code(): return random.randint(1000, 9999) @@ -49,7 +76,7 @@ def parse(url): "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", } r = requests.get(url, headers=headers) - r.encoding='utf-8' + r.encoding = 'utf-8' print(r.text) selector = Selector(text=r.text) if t == 'weixin': @@ -106,8 +133,8 @@ def queryset_to_list(q, fields): if __name__ == '__main__': - r = sent_sms_code('13993199566', 4321) - print(r.code(), type(r.code())) + # r = sent_sms_code('13993199566', 4321) + # print(r.code(), type(r.code())) # og_title, og_description, og_url, og_image = parse( # 'https://mp.weixin.qq.com/s/EhX0Pm1e0FAfse0zz9ow8Q') # og_title, og_description, og_url, og_image = parse( @@ -118,3 +145,4 @@ if __name__ == '__main__': # 'http://xgs.gansudaily.com.cn/pages/h5/hot/b3297046a53e47f594ed19db90c1183c.html') # print(og_title, og_description, og_url, og_image) + print(send_voice_notify('13993199566')) \ No newline at end of file diff --git a/polls/views/__init__.py b/polls/views/__init__.py index 5f45fd7..8491776 100644 --- a/polls/views/__init__.py +++ b/polls/views/__init__.py @@ -1,4 +1,4 @@ -from .user import index, status_500, status_401, polls_login, send_code, register_step_one, register_step_two, password_recover_step_one, password_recover_step_two, reset_password +from .user import index, status_500, status_401, polls_login, send_code, register_step_one, register_step_two, password_recover_step_one, password_recover_step_two, reset_password, upload_profile from .notice import notices, notice_top, read_notice, pass_notice, reject_notice, detail_notice, unread_notice_count from .media import medias, my_medias, create_media, update_media, media_detail, delete_media, media_admin_change from .news import news_list, news_top, news_detail diff --git a/polls/views/user.py b/polls/views/user.py index dc92a55..cbd4921 100644 --- a/polls/views/user.py +++ b/polls/views/user.py @@ -1,9 +1,15 @@ +from polls.decorators import polls_login_required from django.shortcuts import render from django.http import HttpResponse, JsonResponse from django.contrib.auth import get_user_model, authenticate, login from django.views.decorators.csrf import csrf_exempt from django_token.models import Token import datetime +from django.conf import settings +import tempfile +import shutil +import uuid + from polls.models import Notice, VerifyCode from polls.utils import generate_code, sent_sms_code @@ -214,3 +220,19 @@ def reset_password(request): user.set_password(new_password) user.save() return JsonResponse({'status': 'success', 'message': '密码修改成功'}) + + +@csrf_exempt +@polls_login_required +def upload_profile(request): + source = request.FILES['photo'] + ext = source.split('.')[-1] + if ext not in ['jpg', 'png', 'jpeg', 'gif']: + return JsonResponse({'status': 'error', 'message': '上传的文件不是图片'}) + + filename = "%s.%s" % (uuid.uuid4(), ext) + # fd, filepath = tempfile.mkstemp( + # prefix=source.name, dir=settings.MEDIA_ROOT) + # with open(filepath, 'wb') as dest: + # shutil.copyfileobj(source, dest) + return JsonResponse({'status': 'success', 'message': filename}) diff --git a/requirements.txt b/requirements.txt index 0c7ab04..b9e4f60 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,3 +11,5 @@ #python-dateutil #channels_redis[cryptography] #jieba +#aliyun-python-sdk-core +#aliyun-python-sdk-dyvmsapi