Integrated SMS verification registration of django project

First, I use have a content cloud communication with:

https://doc.yuntongxun.com/p/5a533e0c3b8496dd00dce08c

Second, check out the official use of the document:

  1, download the SDK archive SMS was then compressed package obtained SDK directory into the project directory must have third-party components, this time my directory:

/ Xiangmu / xiangmuapi / libs / sms / yuntongxun / sdk /

  2, official documents are python2.7 get demo, demo on the shining rewrite python3

Code Description: Coding = UTF- 8 or GBK
  from CCPRestSDK Import REST
  Import ConfigParser 
 
 accountSid = ' your primary account ' ; 
  # Description: primary account number, login cloud communication site, you can see the main developer account ACCOUNT console home page SID. 
 
 accountToken = ' your primary account Token ' ; 
  # Description: Primary Account Number Token, after landing cloud communication site, you can see the main developer account AUTH TOKEN in the console home page. 
 
 appId = ' Your application ID ' ; 
  # use the management console applications that have been created APPID. 
 
 serverIP = ' app.cloopen.com ' ;
  # Note: the request address, to the production environment configured app.cloopen.com.
 
 the serverPort = ' 8883 ' ; 
  # Description: request port, the production environment 8883. 
 
 softVersion = ' 2013-12-26 ' ; # Description: REST API version number remains unchanged.

  Get more configuration items can be placed constants file, or the configuration file dev.py, I placed dev.py, as follows:

SMS = {
    "accountSid": "8aaf07086e0115bb016ea14165ca5eff",
    "accountToken": "af3d92b841c7498c9f6fb66b2b594fea",
    "appId": "8aaf07086e0115bb016ea141661d5f05",
    "serverIP": "sandboxapp.cloopen.com",
    "serverPort": "8883",
    ":"softVersion'2013-12-26'
}

 3, will then have to re-send SMS method sdk file rewrites python3 ,:

  As a file named sms.py, the path is

  /xiangmu/xiangmuapi/libs/sms/yuntongxun/sdk/sms.py
# - * - Coding: UTF-8 - * - 
from .CCPRestSDK Import REST
 from django.conf Import Settings 

accountSid = settings.SMS [ " accountSid " ]
 # Description: The primary account number, login cloud communications sites can Panel Home see the main developer account aCCOUNT SID. 

accountToken = settings.SMS [ " accountToken " ]
 # Description: The primary account number Token, after landing cloud communication site, you can see the main developer account AUTH TOKEN in the console home page. 

appId = settings.SMS [ " appId " ]
 # Please use the management console APPID application has been created. 

serverIP = settings.SMS [ " serverIP" ]
 # Description: the request address, the production environment is configured to app.cloopen.com. 

The serverPort = settings.SMS [ " the serverPort " ]
 # Description: request port, the production environment 8883. 

softVersion = settings.SMS [ " softVersion " ] 


DEF sendTemplateSMS (to, DATAS, tempID):
     # initialize the SDK REST 
    REST = REST (serverIP, serverPort, softVersion) 
    rest.setAccount (accountSid, accountToken) 
    rest.setAppId (appId) 
    the Result = rest.sendTemplateSMS (to, DATAS, tempID)
     # { 'statusCode': '000000' , #result result 
    # 'templateSMS': {'smsMessageSid': '23e68c7b54484b938c1fd7d77f6077ae', 'dateCreated': '20200107090615'}}
    if result.get("statusCode") == "000000":
        return 0
    return -1

  4, and then views view files have to send text messages to call interfaces:

    Here, I conducted for the convenience behind SMS verification, use the redis storage, SMS verification code will be saved as:

      setex (sms_ phone number, expiration date, verification code content)

      setex (mobile_ phone number, SMS cooling time, -)

class SMSCodeAPIView (APIView):
     DEF GET (Self, Request, Mobile):
         "" " 
        SMS verification 
        : param Request: 
        : return: 
        " "" 
        redis_conn = get_redis_connection ( " sms_code " ) 
        RET = redis_conn.get ( " mobile_% S " % (Mobile))
         IF RET:
             return the Response ({ " the message " : " SMS has been sent, please pay attention to your cell phone text messages, frequently do not click! " }, Status = Status.HTTP_400_BAD_REQUEST) 

        # forming short codes
        = the random.randint sms_code (0000, 999999)            # message authentication code content 
        sms_time = constancs.SMS_EXPIRATION_TIME            # message expiration time 
        # pipes, use may be provided redis redis execute multiple command disposable 
        # 3. things save the message codes to redis 
        = pipe redis_conn.pipeline () 
        pipe.multi () 
        pipe.setex ( " SMS_% S " % (Mobile), constancs.SMS_EXPIRATION_TIME, sms_code) 
        pipe.setex ( " mobile_% S " % (Mobile), constancs.SMS_INTERVAL_TIME, " - " ) 
        pipe.execute () 
        # sending text messages
        = sendTemplateSMS RET (Mobile, [sms_code, sms_time // 60 ], constancs.SMS_TEMPLATE_ID)
         IF RET == 0:
             return the Response ({ " Message " : " SMS success " }, Status = status.HTTP_200_OK)
         return the Response ({ " Message " : " SMS failed " }, Status = status.HTTP_502_BAD_GATEWAY)

  5, to transmit a message authentication code, a registered route:

urlpatterns = [
    ...
    re_path('sms/(?P<mobile>1[3-9]\d{9})/', views.SMSCodeAPIView.as_view()),
]

  6, front-end call / sms / phone number / interface can

Guess you like

Origin www.cnblogs.com/shangguanruoling/p/12160680.html