python企业微信群聊_企业微信、钉钉群消息python机器人自动推送

本文介绍了如何使用Python脚本通过钉钉和企业微信的接口实现自动发送消息。脚本中包含了获取签名、发送消息的函数,并展示了发送依赖关系的例子。在钉钉中使用加签方式确保安全,而企业微信则使用电话号码@相关用户。
摘要由CSDN通过智能技术生成

钉钉具体的接口配置文档:https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq

企业微信的接口配置文档,目前在应用内部添加机器人时能够查看,还没有提供公网参考地址。python

一、python脚本

/Users/nisj/Documents/wptDataGit-nisj/wptData/pyScript/love/workGroupAlarm.pymysql

#!/usr/bin/python2.7.5

# -*- coding: utf-8 -*-

import datetime

import os

import time

import hmac

import hashlib

import base64

import urllib

today = datetime.date.today()

yesterday = today - datetime.timedelta(days=1)

def getDingsign():

timestamp = long(round(time.time() * 1000))

secret = 'SEC8da036adfb92d0f3d4f7ee6bb714af5e236ca12bafb231aa4e0824a7a710fd46'

secret_enc = bytes(secret).encode('utf-8')

string_to_sign = '{}\n{}'.format(timestamp, secret)

string_to_sign_enc = bytes(string_to_sign).encode('utf-8')

hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()

sign = urllib.quote_plus(base64.b64encode(hmac_code))

return (timestamp, sign)

def DingDing_alarm(mess):

signs = getDingsign()

timestamp = signs[0]

sign = signs[1]

alarm_mess = '''

curl 'https://oapi.dingtalk.com/robot/send?access_token=b25de77a1ee7988c6dfab9a57c0a31164dc836c350674d69aa2f2954698062fa&timestamp=%s&sign=%s' \

-H 'Content-Type: application/json' \

-d '{

"msgtype": "text",

"text": {

"content": "%s"

},

"at":{

"isAtAll": true

}

}'

''' % (timestamp, sign, mess)

os.system(alarm_mess)

def WeChat_alarm(mess):

alarm_mess = '''

curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=de569076-cf2c-4315-9333-a2b9563fd021' \

-H 'Content-Type: application/json' \

-d '{

"msgtype": "text",

"text": {

"content": "%s",

"mentioned_mobile_list":["15855134153","@all"]

},

}'

''' % (mess)

os.system(alarm_mess)

def runData():

selectResult = os.popen(

"""/usr/local/mysql/bin/mysql -hlocalhost -P3306 -uroot -p123qwe -Dwpt -e " \

select targetTab,depOds \

from move2_dependent \

where targetTab like 'dim%' \

group by targetTab,depOds;" """.format(

dt=yesterday)).readlines();

selectResultList = []

for selectCol in selectResult:

selectResultList.append(selectCol.replace('\n', ''))

return selectResultList

if __name__ == "__main__":

contentStr = '依赖关系:\n'

for content in runData():

contentList = content.split('\t')

contentStr = contentStr + contentList[0] + '&' + contentList[1] + '\n'

DingDing_alarm(mess=contentStr)

WeChat_alarm(mess=contentStr)

二、返回结果及群消息

返回结果:web

/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/nisj/Documents/wptDataGit-nisj/wptData/pyScript/love/workGroupAlarm.py

mysql: [Warning] Using a password on the command line interface can be insecure.

% Total % Received % Xferd Average Speed Time Time Time Current

Dload Upload Total Spent Left Speed

100 1060 100 27 100 1033 113 4329 --:--:-- --:--:-- --:--:-- 4340

{"errcode":0,"errmsg":"ok"} % Total % Received % Xferd Average Speed Time Time Time Current

Dload Upload Total Spent Left Speed

100 1040 100 27 100 1013 77 2907 --:--:-- --:--:-- --:--:-- 2910

Process finished with exit code 0

企业微信群消息:sql

依赖关系:

targetTab&depOds

dim_product_category_full_1d&ods_apicategory_category_full_1d

dim_product_category_full_1d_today&ods_apicategory_category_full_1d

dim_regist_sc_1d&ods_general_shop_exclusive_qrcode_full_1d

dim_regist_sc_1d&ods_report_rpt_roi_scene_full_1d

dim_seller_base_snap_1d&ods_userinfo_userinfo_full_1d

dim_seller_cfc_organization_full_1d&ods_wpt_certchain_cfc_organization_full_1d

dim_share_documents_full_chain_1d&ods_wpt_management_share_documents_full_1d

dim_userinfo_address_full_1d&ods_userinfo_userinfo_address_full_1d

dim_userinfo_basic_info_full_1d&ods_userinfo_userinfo_center_full_1d

dim_userinfo_snap_full_1d&ods_userinfo_userinfo_center_full_1d

dim_userinfo_snap_full_1d&ods_userinfo_userinfo_full_1d

dim_user_yj_artisan_full_1d&ods_wanwan_artisan_full_1d

@倪士甲 @全部人

钉钉群消息:json

依赖关系:

targetTab&depOds

dim_product_category_full_1d&ods_apicategory_category_full_1d

dim_product_category_full_1d_today&ods_apicategory_category_full_1d

dim_regist_sc_1d&ods_general_shop_exclusive_qrcode_full_1d

dim_regist_sc_1d&ods_report_rpt_roi_scene_full_1d

dim_seller_base_snap_1d&ods_userinfo_userinfo_full_1d

dim_seller_cfc_organization_full_1d&ods_wpt_certchain_cfc_organization_full_1d

dim_share_documents_full_chain_1d&ods_wpt_management_share_documents_full_1d

dim_userinfo_address_full_1d&ods_userinfo_userinfo_address_full_1d

dim_userinfo_basic_info_full_1d&ods_userinfo_userinfo_center_full_1d

dim_userinfo_snap_full_1d&ods_userinfo_userinfo_center_full_1d

dim_userinfo_snap_full_1d&ods_userinfo_userinfo_full_1d

dim_user_yj_artisan_full_1d&ods_wanwan_artisan_full_1d

@全部人

三、说明

钉钉的安全设置能够3选1,这里采用了第2种加签的方式;企业微信目前尚未安全设置这方面的配置。

企业微信的@关注人,能够{"mentioned_list":["wangqing","@all"]},但更多的采用{"mentioned_mobile_list":["13800001111","@all"]},由于企业微信的user_id不太好找到,用电话号比较方便。

钉钉的@关注人:c#

"at": {

"atMobiles": [

"156xxxx8827",

"189xxxx8325"

],

"isAtAll": false

}

定时推送能够经过crontab或调度工具Azkaban、airflow配置。api

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值