import http.client from urllib import parse import json def tpl_send_sms(apikey, tpl_id, tpl_value, mobile): """ 模板接口发短信 """ params = parse.urlencode({ 'apikey': apikey, 'tpl_id': tpl_id, 'tpl_value': parse.urlencode(tpl_value), 'mobile': mobile }) headers = { "Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain" } conn = http.client.HTTPSConnection(sms_host, port=port, timeout=30) conn.request("POST", sms_tpl_send_uri, params, headers) response = conn.getresponse() response_str = response.read() conn.close() return response_str def send_sms(apikey, text, mobile): """ 通用接口发短信 """ params = parse.urlencode({'apikey': apikey, 'text': text, 'mobile':mobile}) headers = { "Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain" } conn = http.client.HTTPSConnection(sms_host, port=port, timeout=30) conn.request("POST", sms_send_uri, params, headers) response = conn.getresponse() response_str = response.read() conn.close() return response_str def sendMessage(apikey = "304eb08353f7ebf00596737acfc31f53"): # 模板 tpl_id = 4621614 # 【甘肃大未来科技】为提高政务新媒体监测服务效果,提升预警时效性和精准性, # 从7月1日起我公司将预警周期由10日调整为7日。感谢您对甘肃大未来的信任和支持。 # 电话号码 dictGS = { 'szq': '13359446622', 'zyb': '13609346975' } dictCities = { '天水市': {'王慧': '18706936366', '王肖肖': '17793816150'}, '白银市': {'高雅丽': '15393391905', '范小强': '13639306533', '张静静': '13830021006'}, '定西市': {'党辉': '18893219695', '高刚': '18993265998'}, '酒泉市': {'白苍松白秘书长': '13909371177', '吴建平': '13389370534'}, '临夏州': {'周世泽': '13830103221', '马清明': '13993012391', '马静': '13993096392'}, '平凉市': {'雷勇': '13809330195', '梁文芬': '13993366938'}, '嘉峪关市': {'彭松涛':'18893605128'}, '庆阳市': {'孙德勋': '13909342931', '闫红': '18993490882'}, '华池县': {'李银粉': '13884192323'}, '宁县': {'张虎帅': '13993434900'}, '镇原县': {'刘主任': '13994327967', '苟罗文': '15268989815'}, } # 批量发送 # (将通知文本向所有号码逐一发送) dictCities['大未来'] = dictGS for dGSk in dictCities.keys(): print('----', dGSk) # dictCities[dGSk].update(dictGS) for dk in dictCities[dGSk].keys(): sss = tpl_send_sms(apikey, tpl_id, '', dictCities[dGSk][dk]) print(' ', dk, dictCities[dGSk][dk], sss.decode('utf-8')) def sendReportMonthly(apikey, sYear, sMon): tpl_id = 4272748 # 【甘肃大未来科技】#city#政务新媒体#year#年#month#月份监测报告电子版已发送到业务联系人,请关注。 # 电话号码 dictGS = { 'szq': '13359446622', 'zyb': '13609346975' } dictCities = { '天水市': {'王慧': '18706936366', '王肖肖': '17793816150'}, '白银市': {'高雅丽': '15393391905', '张静静': '13830021006'}, '定西市': {'张勇':'13993200605', '高刚': '18993265998'},#'党辉': '18893219695', '酒泉市': {'吴建平': '13389370534'}, #'白苍松白秘书长': '13909371177', '临夏州': {'周世泽': '13830103221', '马清明': '13993012391', '任琴霞': '13909300361'}, '平凉市': {'雷勇': '13809330195', '万朵': '15193383961'}, '庆阳市': {'孙德勋': '13909342931', '闫红': '18993490882'}, #'华池县': {},#'李银粉': '13884192323'}, #'宁县': {},#'张虎帅': '13993434900'}, #'镇原县': {}, #{'刘主任': '13994327967'}, #, '苟罗文': '15268989815'}, '嘉峪关市': {'彭松涛': '18893605128'}, '武威市': {'马巨龙': '15379291530'}, '兰州新区': {'高天晓副主任':'13993685885', '刘玉明科长':'17726983336', '闫鹏':'15117091122', }, '陇南市': {'王军主任':'18093988558', '杨帅兵':'13830941310'}, '张掖市': {'李伟璟副主任':'13909365376', '张炜':'18993628432'} } # 批量发送 # (逐市州发送) for dGSk in dictCities.keys(): print('----', dGSk) dictCities[dGSk].update(dictGS) # 向该市号码列表中添加大未来的号码 for dk in dictCities[dGSk].keys(): tpl_value = {'#city#': dGSk, '#year#': sYear, '#month#': sMon} sss = tpl_send_sms(apikey, tpl_id, tpl_value, dictCities[dGSk][dk]) print(' ', dk, dictCities[dGSk][dk], sss.decode('utf-8')) def sendForewarning(apikey): tpl_id = 4058906 # 【甘肃大未来科技】政务新媒体监测预警:#dateStart#至#dateEnd#,监测#city#政务新媒体账号#count#个, # 有#uq#个账号#problem#,具体名单发至相关工作人员,请予以关注。 tpl_id1 = 4348890 # 【甘肃大未来科技】 # dateStart#至#dateEnd#,监测#city#政务新媒体账号#amount#个,更新频次和发布内容正常。 # 电话号码 dDwlNamePhone = { 'szq': '13359446622', 'zyb': '13609346975' } dCityNamePhone = { '天水市': {'王慧': '18706936366', '王肖肖': '17793816150'}, '白银市': {'高雅丽': '15393391905', '范小强': '13639306533', '张静静': '13830021006'}, '定西市': {'张勇':'13993200605', '高刚': '18993265998'}, '酒泉市': {'新领导': '13909371177', '吴建平': '13389370534'}, '临夏州': {'周世泽': '13830103221', '马清明': '13993012391', '马静': '13993096392'}, '平凉市': {'雷勇': '13809330195', '梁文芬': '13993366938'}, '嘉峪关市': {'彭松涛':'18893605128'}, '庆阳市': {'孙德勋': '13909342931', '闫红': '18993490882'}, '华池县': {},#'李银粉': '13884192323'}, '宁县': {'张虎帅': '13993434900'}, '镇原县': {'刘主任': '13994327967', '苟罗文': '15268989815'}, } ############################################################################## ############################################################################## # 检测时间和结果 dDate = { 'dateStart': '6月24日', 'dateEnd': '30日' } dResults = { '白银市': {'账号数量': '361', '预警原因': '无更新', '预警数量': '49'}, '定西市': {'账号数量': '406', '预警原因': '无更新', '预警数量': '23'}, '酒泉市': {'账号数量': '376', '预警原因': '无更新', '预警数量': '22'}, '临夏州': {'账号数量': '295', '预警原因': '无更新', '预警数量': '13'}, '平凉市': {'账号数量': '312', '预警原因': '无更新', '预警数量': '16'}, '庆阳市': {'账号数量': '303', '预警原因': '无更新', '预警数量': '10'}, '天水市': {'账号数量': '225', '预警原因': '无更新', '预警数量': '19'}, # '嘉峪关市': {'账号数量': '97', '预警原因': '无更新', '预警数量': '7'}, '华池县': {'账号数量': '38', '预警原因': '无更新', '预警数量': '2'}, '宁县': {'账号数量': '36', '预警原因': '无更新', '预警数量': '1'}, '镇原县': {'账号数量': '33', '预警原因': '', '预警数量': '0'}, } ############################################################################## ############################################################################## # 批量发送 # (逐市州发送) for sCity in dResults.keys(): print('----', sCity) if sCity in dCityNamePhone: dCityNamePhone[sCity].update(dDwlNamePhone) # 向该市号码列表中添加大未来的号码 for sName in dCityNamePhone[sCity].keys(): uq = dResults[sCity]['预警数量'] if int(uq) > 0: tpl_value = {'#dateStart#': dDate['dateStart'], '#dateEnd#': dDate['dateEnd'], '#city#': sCity, '#count#': dResults[sCity]['账号数量'], '#uq#': dResults[sCity]['预警数量'], '#problem#': dResults[sCity]['预警原因'], } ##sss = tpl_send_sms(apikey, tpl_id, tpl_value, dCityNamePhone[sCity][sName]) sss = '【甘肃大未来科技】政务新媒体监测预警:' + dDate['dateStart'] + '至' + dDate['dateEnd']\ + ',' + sCity + '被监测的' + dResults[sCity]['账号数量'] + '个政务新媒体账号中,有'\ + dResults[sCity]['预警数量'] + '个账号' + dResults[sCity]['预警原因'] + ',具体名单将发至相关工作人员,请予以关注提醒。' print(' ', sName, dCityNamePhone[sCity][sName], sss)#.decode('utf-8')) elif int(uq) == 0: tpl_value = {'#dateStart#': dDate['dateStart'], '#dateEnd#': dDate['dateEnd'], '#city#': sCity, '#amount#': dResults[sCity]['账号数量'], } #sss = tpl_send_sms(apikey, tpl_id1, tpl_value, dCityNamePhone[sCity][sName]) # 【甘肃大未来科技】 # dateStart#至#dateEnd#,监测#city#政务新媒体账号#amount#个,更新频次和发布内容正常。 sss = '【甘肃大未来科技】' + dDate['dateStart'] + '至'+ dDate['dateEnd'] + ',监测'\ + sCity + '政务新媒体账号' + dResults[sCity]['账号数量'] + '个,更新频次和发布内容正常。' print(' ', sName, dCityNamePhone[sCity][sName], sss)#.decode('utf-8')) else: print('!!!!! ERROR !!!!!') if __name__ == "__main__": ########################## # 短信平台地址和端口 ########################## # 服务地址 sms_host = "sms.yunpian.com" voice_host = "voice.yunpian.com" # 端口号 port = 443 # 版本号 version = "v2" # 查账户信息的URI user_get_uri = "/" + version + "/user/get.json" # 智能匹配模板短信接口的URI sms_send_uri = "/" + version + "/sms/single_send.json" # 模板短信接口的URI sms_tpl_send_uri = "/" + version + "/sms/tpl_single_send.json" # 语音短信接口的URI sms_voice_send_uri = "/" + version + "/voice/send.json" # 语音验证码 voiceCode = 1234 ########################## # 短信平台地址和端口 ########################## apikey = "304eb08353f7ebf00596737acfc31f53" # 向所有成员发送通知 #sendMessage(apikey) # 逐市州发送月报告 sendReportMonthly(apikey, '2023', '5') # 逐市州发送预警信息 #sendForewarning(apikey)