python调用钉钉接口发工作通知的问题
本帖最后由 houliangdong 于 2020-3-21 16:47 编辑先调出access_token,然后通过 手机号获取userid,再通过userid来发工作通知。最后一个手机号变量设置不成功。现在是只能给189的发钉钉工作通知,不能给136的发钉钉工作通知 。
print(send_user(‘测试一个通知 ’,‘13606510177’)).不管用,还是给189的发钉钉工作通知, 要如何设置手机的变量呢?
感谢"天黑我隐身"大神的帮助,拯救了一只小白。哈哈
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))
还以为教程,然后一脸懵逼的进来,一脸懵逼的出去{:1_909:} ```python
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)
```
这个函数看上去是向指定手机号发送消息,那就从这里入手
##### 先看看传入的参数
* content
在_data里面暂且不管
* mobile
从头到尾没有使用过,显然有问题
##### 其他全局变量
* access_token
```python
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()
```
getToken函数不需要使用mobile暂且不管
* agent_id
```python
agent_id = "332949530"
```
没问题
* userid
```python
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()
```
getuserid函数没有传参但内部出现了一个变量mobile,于是解释器找到全局变量mobile
```python
mobile= '18958070102'
```
分析到这里很显然就是作者对函数作用域理解存在问题,想要修改也很简单,给getuserid传入一个mobile参数即可 本帖最后由 houliangdong 于 2020-3-21 16:20 编辑
天黑我隐身 发表于 2020-3-21 14:59
```python
def send_user(content,mobile):
url = 'https://oapi.din ...
大神,你能帮我给getuserid传入一个mobile参数吗?帮我改一下。我这边试了几个形式也都是失败。
在运行print(send_user('测试一下,看能不能收到','13606510177'))时,发现钉钉工作通知 还是发给了,189的人。谢谢啦。
def getuserid():
mobile= '18958070102'
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() 天黑我隐身 发表于 2020-3-21 14:59
```python
def send_user(content,mobile):
url = 'https://oapi.din ...
大神,你能帮我给getuserid传入一个mobile参数吗?帮我改一下。我这边试了几个形式也都是失败。
在运行print(send_user('测试一下,看能不能收到','13606510177'))时,发现钉钉工作通知 还是发给了,189的人。谢谢啦。.小白一个,真的是很多都不懂。麻烦 啦 houliangdong 发表于 2020-3-21 16:17
大神,你能帮我给getuserid传入一个mobile参数吗?帮我改一下。我这边试了几个形式也都是失败。
在运行p ...
你这也太白了吧...
```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(mobile):
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
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': getuserid(mobile),
'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))
``` 哈哈哈,谢谢大神,确实是白的透明的那种 。 {:1_921:}都是大神啊!
页:
[1]