The original article is reprinted from liu Yue’s Technology blog v3u.cn/a_id_152
SMS service verification service is not what new, but the free mobile phone SMS service is rare, the use of Python3.0 based on Twilio and Tencent cloud services respectively to experience the international SMS and domestic SMS interface.
First, sign up for Twilio: www.twilio.com/
After successfully registering, obtain the ACCOUNT SID and AUTH TOKEN, which will be used later
Install dependent libraries
pip3 install twilio
Copy the code
Then write the send script
# import package into client
from twilio.rest import Client
# define SMS SID
account_sid = 'your sid'
Define the secret key
auth_token = 'Your secret key'
Define the client object
client = Client(account_sid,auth_token)
message = client.messages.create(
to="+ 8613423432818".# The mobile phone number that receives the SMS, that is, the mobile phone number that has been verified in the registration interface, please write the Chinese area code +86
from_="+ 12118627871".# Area code +1 for sending text messages
body="Hello from Python!")
Print the sent result
print(message)
Copy the code
The whole process is simpler, so you’ve got a free international mobile phone short message service interface, pay attention to send a phone number to fill out the phone number of country code, the other twilio lines is not infinite, there will be a $10 lines, each one will deduct 1 cent, so the test time go easy, don’t exceed the quota restrictions.
Domestic SMS business this piece do better ali cloud, Tencent cloud, and Baidu cloud, in addition, seven niuyun and patted cloud also have corresponding services, why must choose Tencent cloud? It’s really easy, because there are 100 free text messages to use.
First sign up for Tencent’s cloud service: cloud.tencent.com
After successful registration, Tencent Cloud will automatically generate a SMS application. If there is no default application, you need to create one
Let’s make a note of the appID and appkey of the app that we’re going to need in a second
Then configure the SMS signature. As the name implies, the signature is used to limit the permissions of the SMS interface and prevent malicious invocation. At the same time, the signature needs to be verified by Tencent customer service personnel, which takes about two hours.
It is recommended to use the mini program as the signature type, and the signature content is customized. It is easy to prove the type, just go to the wechat mini program background interface to take a picture.
After configuring signatures, you also need to configure an SMS template, which is simply the content of SMS messages sent to users
The {} symbol in the template is similar to the wildcard character, which should be replaced with variables when sending SMS messages. Note that the index number is indicated. The template also needs to be manually reviewed by Tencent customer service, which takes about two hours. In addition, the length of the message had better not exceed 70 words, more than after the length of the letter will be calculated by every 67 words, it is a great loss.
The above is the early configuration, the following installation of Tencent cloud SMS SDK
pip3 install qcloudsms_py
Copy the code
Write sending scripts
# SMS app SDK AppIDAppid = Your appID# SDK AppID starts with 1400
# SMS app SDK AppKey
appkey = "Your appkey"
# Phone number to send SMS
phone_numbers = ["The phone number you're sending."]
ID of the SMS template, which needs to be applied in the SMS applicationTemplate_id = In the template listSign #
sms_sign = "Liu Yue's Technology Blog"
from qcloudsms_py import SmsSingleSender
from qcloudsms_py.httpclient import HTTPError
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
ssender = SmsSingleSender(appid, appkey)
params = ["6666"."5"] # params = [] 'when template has no arguments
try:
result = ssender.send_with_param(86, phone_numbers[0],
template_id, params, sign=sms_sign, extend="", ext="") The signature parameter cannot be an empty string
print(result)
except HTTPError as e:
print(e)
except Exception as e:
print(e)
Copy the code
It’s not a difficult process, but one of the things to note here is that in order to avoid the SSL certificate problem you need to import the SSL library and set it up separately and see what happens, okay
At the same time, Tencent cloud service SMS experience is better than Twilio in that it has a detailed statistical background, which is convenient for us to monitor and count the arrival rate of SMS, and is friendly to mass users.
The original article is reprinted from liu Yue’s Technology blog v3u.cn/a_id_152