標籤:

【阿里雲短消息服務】每天一條問候簡訊

周末一個人在家無聊,寫點東西吧,今天教大家如果運用阿里雲短消息服務來發簡訊,每條簡訊4分5,非常便宜,廉價的簡訊,厚重的祝福,程序猿也可以很浪漫。

本文代碼基於Python

代碼運行平台為阿里雲ECS CentOS release 6.5

具體操作阿里雲官方API資料講的已經非常清楚了:阿里雲簡訊平台API

一、代碼

(加粗的部分是你要根據自己情況自己改的)

# -*- coding: utf-8 -*-Send a message to the specific phone using Aliyun SMS.Shane Wang20171008import sysimport datetimeimport randomfrom aliyunsdkdysmsapi.request.v20170525 import SendSmsRequestfrom aliyunsdkdysmsapi.request.v20170525 import QuerySendDetailsRequestfrom aliyunsdkcore.client import AcsClientimport uuidreload(sys)sys.setdefaultencoding(utf8)# Do NOT change the REGIONREGION = "cn-hangzhou"# ACCESS_KEY_ID/ACCESS_KEY_SECRET. Change them based on author info.ACCESS_KEY_ID = "xxxxxx"ACCESS_KEY_SECRET = "xxxxx"acs_client = AcsClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET, REGION)def send_sms(business_id, phone_numbers, sign_name, template_code, template_param=None): smsRequest = SendSmsRequest.SendSmsRequest() # The Template Code is necessary. smsRequest.set_TemplateCode(template_code) # The Template parameters are necessary. if template_param is not None: smsRequest.set_TemplateParam(template_param) # The request number is necessary. smsRequest.set_OutId(business_id) # The sign name is necessary. smsRequest.set_SignName(sign_name); # The destnation phone number is necessary. smsRequest.set_PhoneNumbers(phone_numbers) # Call the sms sending interface. Return json. smsResponse = acs_client.do_action_with_exception(smsRequest) # Add the service handling logic here. return smsResponsedef query_send_detail(biz_id, phone_number, page_size, current_page, send_date): queryRequest = QuerySendDetailsRequest.QuerySendDetailsRequest() # The phone number to be checked queryRequest.set_PhoneNumber(phone_number) # The BizId is selected. queryRequest.set_BizId(biz_id) # The sending date is necessary. Support to the date in 30 days. eg. yyyyMMdd queryRequest.set_SendDate(send_date) # The maxinum item number in the current page is necessary. queryRequest.set_CurrentPage(current_page) # The page size is necessary. queryRequest.set_PageSize(page_size) # Call the sms record checking interface. Return json. queryResponse = acs_client.do_action_with_exception(queryRequest) # Add the service handling logic here. return queryResponse # Gen the luck numdef gen_luck(): luck_num=random.randint(1, 19) while luck_num == 4 or luck_num == 14: luck_num=random.randint(1, 19) return luck_num # Calc the memorial daydate_now=datetime.datetime.now()date_start=datetime.datetime(2017, 4, 1)memorial_day=(date_now - date_start).days# To listSHANE_NAME="進擊的神娃"SHANE_NO="156xxxxxxxx"# Print the prompt infomationprint "
%s
===============================" % date_now.strftime(%Y-%m-%d %H:%M:%S)# Main__name__ = sendif __name__ == send: __business_id = uuid.uuid1() print __business_id params="{"name":"%s,","number":"%s","day":"%s"}" % (SHANE_NAME, gen_luck(), memorial_day) print send_sms(__business_id, SHANE_NO, "Shane娃", "SMS_9670xxxx", params)

二、效果驗證:

(myecs):/home/shanewa/bin/ali_sms:>

$ python2.6.6 /home/shanewa/bin/ali_sms/testing.py

然後就收到簡訊了

然後我希望每天的 07:00整 讓腳本自動向我和對象發簡訊,這就需要在伺服器上用crontab設置下計劃任務了:

(myecs):/home/shanewa/bin/ali_sms:>

$ crontab -l

0 7 * * * /usr/bin/python2.6.6 /home/shanewa/bin/ali_sms/morning.py >> /tmp/morning.py.record

謝謝!

如果有其他問題需要交流的話歡迎私信或者email(shanewa@qq.com )。

推薦閱讀:

暢銷榜上的深度學習和機器學習書單!
【20170707】雙坐標軸畫法和更改坐標軸刻度
Python入門 數據結構 list列表
最令人喜愛的9個Python深度學習庫
定位後端開發,有哪些書籍值得推薦?

TAG:Python | 阿里雲 |