cloopen_sms.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #coding=utf-8
  2. '''
  3. Created on 2018年12月11日
  4. @author: xiaojincai
  5. '''
  6. import os
  7. import json
  8. import random
  9. import datetime
  10. from hashlib import md5
  11. import threading
  12. import logging
  13. import base64
  14. import requests
  15. logger = logging.getLogger(__name__)
  16. SMS_TEMPLATE_DCT = {
  17. "version":"v2.4",
  18. "sign":u"【网安云防御】",
  19. "396732":{
  20. "template_id":"396732",
  21. "title":u"用户信息邮件变更通知",
  22. "content":u"您的注册邮箱地址在{}由{}变更为{}。如有疑问,请及时与运营人员联系!"
  23. },
  24. "400033":{
  25. "template_id":"400033",
  26. "title":u"用户信息手机号变更通知",
  27. "content":u"您的注册手机号码在{}由{}变更为{}。如有疑问,请及时与运营人员联系!"
  28. },
  29. "396735":{
  30. "template_id":"396735",
  31. "title":u"账号到期提醒通知",
  32. "content":u"您的账号{}将于{}到期,届时该账号下的网站将被停止安全防护服务,请及时与运营人员联系!"
  33. },
  34. "396737":{
  35. "template_id":"396737",
  36. "title":u"账号到期提醒通知(运营)",
  37. "content":u"{}账号将于{}到期,请及时处理!"
  38. },
  39. "396739":{
  40. "template_id":"396739",
  41. "title":u"网站迁出提醒通知(用户)",
  42. "content":u"经检测发现,您的网站{}在{}已迁出云防御系统,处于未防御状态,请予以关注!"
  43. },
  44. "396740":{
  45. "template_id":"396740",
  46. "title":u"网站迁出提醒通知(运营)",
  47. "content":u"经检测发现,网站{}在{}已迁出云防御系统,处于未防御状态,请及时处理!"
  48. },
  49. "396742":{
  50. "template_id":"396742",
  51. "title":u"防御状态变更通知(用户)",
  52. "content":u"您的网站{}在{}变更为{}状态,请予以关注!"
  53. },
  54. "396744":{
  55. "template_id":"396744",
  56. "title":u"防御状态变更通知(运营)",
  57. "content":u"网站{}在{}变更为{}状态,请及时处理!"
  58. },
  59. "396746":{
  60. "template_id":"396746",
  61. "title":u"网站删除通知",
  62. "content":u"网站{}在{}被用户{}删除,云防御系统将无法继续提供安全防护服务,请予以关注!"
  63. },
  64. "397198":{
  65. "template_id":"397198",
  66. "title":u"网站防护策略修改通知",
  67. "content":u"网站{}在{}被用户{}修改了{}策略,请予以关注!"
  68. },
  69. "400981":{
  70. "template_id":"400981",
  71. "title":u"网站到期提醒",
  72. "content":u"您的网站{}将于{}到期,届时该网站将会被自动回源,停止安全防护服务,请及时与运营人员联系!"
  73. },
  74. "400984":{
  75. "template_id":"400984",
  76. "title":u"网站到期提醒(运营)",
  77. "content":u"{}网站将于{}到期,请及时处理!"
  78. },
  79. "401151":{
  80. "template_id":"401151",
  81. "title":u"重置密码短信验证码",
  82. "content":u"您正在进行重置密码操作,验证码是{},请于{}分钟内正确输入!"
  83. },
  84. "423122":{
  85. "template_id":"423122",
  86. "title":u"网站可用状态变更",
  87. "content":u"您的网站{}在{}{}为{}状态,请予以关注!"
  88. }
  89. }
  90. class CloopenSMS(object):
  91. """融联短信接口
  92. """
  93. def __init__(self):
  94. self.sid = "8a216da8588b296f01588ecd0a0001ab"
  95. self.token = "f0d2b53782374ebc8a76fa8dd06bb4d2"
  96. self.appId = "8a216da8589ae4840158a89b5c8f0272"
  97. self.base_url = "https://app.cloopen.com:8883/2013-12-26/Accounts/%s/SMS/TemplateSMS" % self.sid
  98. def send(self,phones,template_id,content):
  99. """
  100. @attention: 发送短信
  101. @param content: 短信内容模板变量数组
  102. @param phones: 电话号码集合,格式为数组
  103. """
  104. #构造url
  105. now = datetime.datetime.now()
  106. batch = now.strftime("%Y%m%d%H%M%S")
  107. signature = self.sid + self.token + batch
  108. sig = md5(signature).hexdigest().upper()
  109. url = self.base_url + "?sig=" + sig
  110. #构造header
  111. src = "%s:%s"%(self.sid,batch)
  112. auth = base64.encodestring(src).strip()
  113. headers = {"Accept": "application/json",
  114. "Content-Type": "application/json;charset=utf-8",
  115. "Authorization": auth}
  116. #构造body
  117. content = map(lambda x:x if isinstance(x, unicode) else str(x).decode("utf8"), content)
  118. print "send sms >>>:",phones,template_id,content
  119. body = {"to": ','.join(phones),
  120. "appId": self.appId,
  121. "templateId": template_id,
  122. "datas": content
  123. }
  124. #成功直接返回1,失败记录日志,并返回0
  125. status_code = ""
  126. status_msg = ""
  127. response = requests.post(url, headers=headers, data=json.dumps(body))
  128. response_code = response.status_code
  129. if response_code==200:
  130. data = response.json()
  131. if data["statusCode"]=="000000":
  132. return 1,data
  133. else:
  134. status_code = data["statusCode"]
  135. status_msg = data["statusMsg"]
  136. return 0,data
  137. #内容,模板ID,号码,http错误码,定义的错误码,操作时间,url
  138. now_time = now.strftime("%Y-%m-%d %H:%M:%S")
  139. msg = "<<(%s_%s_%s) %s>>:url=%s;\n"%(response_code,status_code,status_msg,now_time,url)
  140. sc = ",".join(content)
  141. sp = ",".join(phones)
  142. msg += "content=[%s];template_id=%s;phones=[%s]\n"%(sc,template_id,sp)
  143. return 0,u"未知错误(短信接口报错)"
  144. cloopensms = CloopenSMS()
  145. if __name__ == '__main__':
  146. #测试
  147. phones = ['15982456282']
  148. template_id = '396732'
  149. content = ["2018-12-11 14:00:00","496498291@qq.com","496498291@qq.com"]
  150. cloopensms.send(phones,template_id,content)