""" Description: 需要提供以下三个信息,在申请到的微信企业号当中可以找到 agentid corpid corpsecret Author:Nod Date:18-04-10 Record: #---------------------------------v1-----------------------------------# """ import urllib.request import json #-------------------------------- # 获取企业微信token #-------------------------------- def get_token(url, corpid, corpsecret): token_url = '%s/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (url, corpid, corpsecret) token = json.loads(urllib.request.urlopen(token_url).read().decode())['access_token'] return token #-------------------------------- # 构建告警信息json #-------------------------------- def messages(msg): values = { "touser": '@all', "msgtype": 'text', "agentid": 1000002, #修改为对应应用的agentid "text": {'content': msg}, "safe": 0 } msges=(bytes(json.dumps(values), 'utf-8')) return msges #-------------------------------- # 发送告警信息 #-------------------------------- def send_message(url,token, data): send_url = '%s/cgi-bin/message/send?access_token=%s' % (url,token) respone=urllib.request.urlopen(urllib.request.Request(url=send_url, data=data)).read() x = json.loads(respone.decode())['errcode'] # print(x) if x == 0: print ('Succesfully') else: print ('Failed') ##############函数结束######################## corpid = 'ww7dd0074bd8b006f9' corpsecret = '8gPCvguwomL0WMej8fcghxQgOY-y0LlGQsAicaDHvA8' url = 'https://qyapi.weixin.qq.com' msg=input('请输入内容:>>') #函数调用 test_token=get_token(url, corpid, corpsecret) msg_data= messages(msg) send_message(url,test_token, msg_data)
以上为Python调用企业微信号接口进行发送微信消息的测试案例
仅供参考
Python调用微信给对应用户发送消息
"""
Description:
Author:Nod
Date:
Record:调用微信给对应用户发送微信消息
#---------------------------------v1-----------------------------------#
"""
import itchat
import time
#引入时间函数进行测试
time_format='%Y-%d-%m %X'
time_current=time.strftime(time_format)
#itchat.auto_login()#python登录扫描二维码 每次登陆都需要产生二维码
itchat.auto_login(hotReload = True) #第一次扫描 短时间内不要扫描 但是时间长了需要客户端确认
users=itchat.search_friends("王豪文")
userName= users[0]['UserName']
itchat.send('你好王豪文,now is %s'%time_current,toUserName=userName)