add message

This commit is contained in:
baoliang 2020-11-28 18:23:56 +08:00
parent faa78f8cf0
commit 8424e12a9a
7 changed files with 86 additions and 14 deletions

View File

@ -225,3 +225,11 @@ class SMSNotifyRecord(models.Model):
added = models.DateField(auto_now_add=True) added = models.DateField(auto_now_add=True)
notice_id = models.CharField('notice_id', max_length=64) notice_id = models.CharField('notice_id', max_length=64)
updated = models.DateTimeField(auto_now=True) 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)

View File

@ -1,8 +1,8 @@
from background_task import background from background_task import background
from datetime import date, datetime, timedelta from datetime import date, datetime, timedelta
import time import time
from polls.models import Notice, SMSNotifyRecord from polls.models import Notice, SMSNotifyRecord, VoiceNotifyRecord
from polls.utils import sent_sms_notify from polls.utils import send_voice_notify, sent_sms_notify
@background(remove_existing_tasks=True) @background(remove_existing_tasks=True)
@ -12,18 +12,29 @@ def process_notify_task():
notices = Notice.objects.filter(is_read=False) notices = Notice.objects.filter(is_read=False)
for n in notices: for n in notices:
two_hours_later = n.added + timedelta(minutes=2) two_hours_later = n.added + timedelta(minutes=2)
four_hours_later = n.added + timedelta(minutes=4)
now = datetime.now() now = datetime.now()
if two_hours_later < now:
phone = n.user.username phone = n.user.username
today = now.date() today = now.date()
hour = now.hour hour = now.hour
if two_hours_later < now:
exists = SMSNotifyRecord.objects.filter( exists = SMSNotifyRecord.objects.filter(
phone=phone, added=today, notice_id=n.id).exists() phone=phone, added=today, notice_id=n.id).exists()
if exists: if exists:
break break
if hour < 8 or hour > 20: if hour < 8 or hour > 20:
break break
print('============', phone, n.type, n.id) print('============', "sms ", phone, n.type, n.id)
SMSNotifyRecord.objects.create(phone=phone, notice_id=n.id) SMSNotifyRecord.objects.create(phone=phone, notice_id=n.id)
sent_sms_notify(phone, n.type) 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) print('end task', time.process_time() - start)

View File

@ -72,7 +72,8 @@ urlpatterns = [
name='polls_message_read'), name='polls_message_read'),
path('app/intro.html', views.app_intro, name='polls_app_intro'), path('app/intro.html', views.app_intro, name='polls_app_intro'),
path('app/download/', views.download, name='polls_app_download'), 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) tasks.process_notify_task(repeat=5)

View File

@ -6,11 +6,16 @@ from django.conf import settings
from itertools import chain from itertools import chain
from yunpian_python_sdk.model import constant as YC from yunpian_python_sdk.model import constant as YC
from yunpian_python_sdk.ypclient import YunpianClient 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): def sent_sms_code(phone, code):
clnt = YunpianClient('304eb08353f7ebf00596737acfc31f53') 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) r = clnt.sms().single_send(param)
return r return r
@ -18,13 +23,35 @@ def sent_sms_code(phone, code):
def sent_sms_notify(phone, type): def sent_sms_notify(phone, type):
clnt = YunpianClient('304eb08353f7ebf00596737acfc31f53') clnt = YunpianClient('304eb08353f7ebf00596737acfc31f53')
if type == 1: if type == 1:
param = {YC.MOBILE: phone ,YC.TEXT:'【甘肃大未来科技】政务新媒体管理APP有新消息请及时登录处理。若其他管理员已处理请忽略*测试*'} param = {YC.MOBILE: phone,
YC.TEXT: '【甘肃大未来科技】政务新媒体管理APP有新消息请及时登录处理。若其他管理员已处理请忽略*测试*'}
else: else:
param = {YC.MOBILE: phone ,YC.TEXT:'【甘肃大未来科技】政务新媒体管理APP有新任务请及时登录处理。'} param = {YC.MOBILE: phone, YC.TEXT: '【甘肃大未来科技】政务新媒体管理APP有新任务请及时登录处理。'}
r = clnt.sms().single_send(param) r = clnt.sms().single_send(param)
return r 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(): def generate_code():
return random.randint(1000, 9999) 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", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
} }
r = requests.get(url, headers=headers) r = requests.get(url, headers=headers)
r.encoding='utf-8' r.encoding = 'utf-8'
print(r.text) print(r.text)
selector = Selector(text=r.text) selector = Selector(text=r.text)
if t == 'weixin': if t == 'weixin':
@ -106,8 +133,8 @@ def queryset_to_list(q, fields):
if __name__ == '__main__': if __name__ == '__main__':
r = sent_sms_code('13993199566', 4321) # r = sent_sms_code('13993199566', 4321)
print(r.code(), type(r.code())) # print(r.code(), type(r.code()))
# og_title, og_description, og_url, og_image = parse( # og_title, og_description, og_url, og_image = parse(
# 'https://mp.weixin.qq.com/s/EhX0Pm1e0FAfse0zz9ow8Q') # 'https://mp.weixin.qq.com/s/EhX0Pm1e0FAfse0zz9ow8Q')
# og_title, og_description, og_url, og_image = parse( # 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') # 'http://xgs.gansudaily.com.cn/pages/h5/hot/b3297046a53e47f594ed19db90c1183c.html')
# print(og_title, og_description, og_url, og_image) # print(og_title, og_description, og_url, og_image)
print(send_voice_notify('13993199566'))

View File

@ -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 .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 .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 from .news import news_list, news_top, news_detail

View File

@ -1,9 +1,15 @@
from polls.decorators import polls_login_required
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse, JsonResponse from django.http import HttpResponse, JsonResponse
from django.contrib.auth import get_user_model, authenticate, login from django.contrib.auth import get_user_model, authenticate, login
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from django_token.models import Token from django_token.models import Token
import datetime import datetime
from django.conf import settings
import tempfile
import shutil
import uuid
from polls.models import Notice, VerifyCode from polls.models import Notice, VerifyCode
from polls.utils import generate_code, sent_sms_code from polls.utils import generate_code, sent_sms_code
@ -214,3 +220,19 @@ def reset_password(request):
user.set_password(new_password) user.set_password(new_password)
user.save() user.save()
return JsonResponse({'status': 'success', 'message': '密码修改成功'}) 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})

View File

@ -11,3 +11,5 @@
#python-dateutil #python-dateutil
#channels_redis[cryptography] #channels_redis[cryptography]
#jieba #jieba
#aliyun-python-sdk-core
#aliyun-python-sdk-dyvmsapi