吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2240|回复: 7
收起左侧

[已解决] python调用钉钉接口发工作通知的问题

[复制链接]
houliangdong 发表于 2020-3-21 02:14
本帖最后由 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))


发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

从零学习的歪经小 发表于 2020-3-21 09:14
还以为教程,然后一脸懵逼的进来,一脸懵逼的出去
天黑我隐身 发表于 2020-3-21 14:59
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
    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
    agent_id = "332949530"

    没问题

  • userid
    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

    mobile= '18958070102'

    分析到这里很显然就是作者对函数作用域理解存在问题,想要修改也很简单,给getuserid传入一个mobile参数即可

 楼主| houliangdong 发表于 2020-3-21 16:14
本帖最后由 houliangdong 于 2020-3-21 16:20 编辑
天黑我隐身 发表于 2020-3-21 14:59
[md]```python
def send_user(content,mobile):
     url = 'https://oapi.din ...

大神,你能帮我给getuserid传入一个mobile参数吗?帮我改一下。我这边试了几个形式也都是失败。
在运行print(send_user('测试一下,看能不能收到','13606510177'))时,发现钉钉工作通知 还是发给了,189的人。谢谢啦。
[Python] 纯文本查看 复制代码
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()
 楼主| houliangdong 发表于 2020-3-21 16:17
天黑我隐身 发表于 2020-3-21 14:59
[md]```python
def send_user(content,mobile):
     url = 'https://oapi.din ...

大神,你能帮我给getuserid传入一个mobile参数吗?帮我改一下。我这边试了几个形式也都是失败。
在运行print(send_user('测试一下,看能不能收到','13606510177'))时,发现钉钉工作通知 还是发给了,189的人。谢谢啦。.小白一个,真的是很多都不懂。麻烦 啦
天黑我隐身 发表于 2020-3-21 16:31
houliangdong 发表于 2020-3-21 16:17
大神,你能帮我给getuserid传入一个mobile参数吗?帮我改一下。我这边试了几个形式也都是失败。
在运行p ...

你这也太白了吧...
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))
 楼主| houliangdong 发表于 2020-3-21 16:46
哈哈哈,谢谢大神,确实是白的透明的那种 。
zzu007 发表于 2020-4-4 17:15
都是大神啊!
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 19:49

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表