本帖最后由 houliangdong 于 2020-3-21 16:47 编辑
先调出access_token,然后通过 手机号获取userid,再通过userid来发工作通知。最后一个手机号变量设置不成功。现在是只能给189的发钉钉工作通知,不能给136的发钉钉工作通知 。
print(send_user(‘测试一个通知 ’,‘13606510177’)).不管用,还是给189的发钉钉工作通知, 要如何设置手机的变量呢?
感谢"天黑我隐身"大神的帮助,拯救了一只小白。哈哈
[Python] 纯文本查看 复制代码 import requests
import json
corpid = 'dingg0jfko1pukchfyva'
secrect = 'eal4Vh7EdJaqJpBqUR_R1xbNj78SYRHH3imHTdVjETdsz8fE4BuB0eznxWkS2B_1'
agent_id = "332949530"
mobile= '18958070102'
def getToken():
url = 'https://oapi.dingtalk.com/gettoken?corpid=%s&corpsecret=%s' % (corpid, secrect)
response = requests.get(url=url)
result = response.json()
errmsg = result['access_token']
print('access_token:',errmsg)
try:
access_token = result['access_token']
except Exception as e:
print(e)
access_token = ''
return access_token
access_token = getToken()
def getuserid():
url = "https://oapi.dingtalk.com/user/get_by_mobile?access_token=%s&mobile=%s"% (access_token,mobile)
response = requests.get(url=url)
result = response.json()
errmsg = result['errmsg']
print('userid:',errmsg)
try:
userid = result['userid']
except Exception as e:
print(e)
userid = ''
return userid
userid = getuserid()
def send_user(content,mobile):
url = 'https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token=%s'% (access_token)
_data = {
'agent_id':agent_id,
'userid_list':userid,
'msg':{
'msgtype':'text',
'text':{'content':content}}
}
data = json.dumps(_data)
response = requests.post(url=url,data=data)
result = response.json()
print(result)
if __name__ == '__main__':
mobile = '13606510177'
content = '444'
print(send_user(content,mobile))
|