python調用釘釘 自定義機器人API發送消息

#!/bin/python
# -*- coding: utf-8 -*-
"""
# 發送消息到釘釘
"""
import json
import time
import sys
import requests

# dingidng notification
class Dingding(object):
    def __init__(self, access_token, content='', at_people=''):
        self.access_token = access_token
        self.content = content
        self.at_people = at_people

    def get_token(self):
        '''
        釘釘管理後臺 : http://open-dev.dingtalk.com
        '''
        access_token = 'https://oapi.dingtalk.com/robot/send?access_token=%s' % self.access_token
        return access_token

    def send_dingding(self):
        '''
        access_token: 網站
        content: 發送的內容
        msgtype : 類型
        '''
        msgtype = 'text'
        if self.at_people == '':
            values = {
                      'msgtype': 'text',
                      msgtype: {
                                'content': self.content
                      },
                      'at': {
                              'atMobiles': self.at_people,
                      },
            }
        else:
            values = {
                      'msgtype': 'text',
                      msgtype: {
                                'content': self.content
                      },
                      'at': {
                              'atMobiles': [self.at_people]
                      },
            }

        headers = {'Content-Type': 'application/json; charset=UTF-8'}
        values = json.dumps(values)
        print(self.get_token(), values)
        res = requests.post(self.get_token(), values, headers=headers)
        errmsg = json.loads(res.text)['errmsg']

        if errmsg == 'ok':
            return 'ok'

        return 'fail: %s' % res.text

content = ''.join(sys.argv[1:])
obj = Dingding('你的機器人Webhook API', content)
obj.send_dingding()

現在新版的機器人都需要自定義詞 發送的內容要包括自定義詞

python dingding10.py 啊test

python調用釘釘 自定義機器人API發送消息

python調用釘釘 自定義機器人API發送消息

點外賣的小夥伴可以能進羣

python調用釘釘 自定義機器人API發送消息

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章