企微,钉钉,飞书群机器人设计(python与Uibot设计)

2022/1/10 17:11:56

本文主要是介绍企微,钉钉,飞书群机器人设计(python与Uibot设计),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、企微,钉钉,飞书群机器人(python版)

想制作群机器人消息通知,必须先知道三款群机器人的创建方法与发送消息的格式:以下是三款办公软件的群机器人创建官方文档
企微:https://work.weixin.qq.com/api/doc/90000/90136/91770
钉钉:https://open.dingtalk.com/document/group/custom-robot-access
飞书:https://open.feishu.cn/document/ukTMukTMukTM/ucTM5YjL3ETO24yNxkjN?lang=zh-CN

建立好群机器人后,需要了解各软件群机器人的发送文档格式是如何发送的

企微text格式:

{
    "msgtype": "text",
    "text": {
        "content": "广州今日天气:29度,大部分多云,降雨概率:60%",
        "mentioned_list":["wangqing","@all"],
        "mentioned_mobile_list":["13800001111","@all"]
    }
}

msgtype:类型
text:文本
content:文本内容
mentioned_list:艾特人的名称,"@all":艾特所有人(可不填)
mentioned_mobile_list:艾特人的电话号码,"@all":艾特所有人(可不填)

钉钉text格式:

{
    "at": {
        "atMobiles":[
            "180xxxxxx"
        ],
        "atUserIds":[
            "user123"
        ],
        "isAtAll": false
    },
    "text": {
        "content":"我就是我, @XXX 是不一样的烟火"
    },
    "msgtype":"text"
}

msgtype:发送文本类型
text:发送文本;content:发送文本内容
atMobiles:艾特人的电话号码
atUserIds:艾特人的IP名称
isAtAll:是否艾特所有人

飞书text格式:

{
    "msg_type": "text",
    "content": {
        "text": "新更新提醒"
    }
}

msg_type:发送文本类型
content:发送文本
text:发送文本内容

了解完格式后,就可以制作基于python的群机器人制作
程序设计:

# -*- coding:utf-8 -*-
import json
import requests
import datetime
def push_det (*AtMobile,message,push_webhook):
    requests.packages.urllib3.disable_warnings()
    Ated = []
    try:
        if not 'dingtalk' in push_webhook and not 'weixin' in push_webhook and not 'feishu' in push_webhook:
            raise Exception('webhook无效')
        nowtime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        headers = {'Content-Type': 'application/json'}
        content = ("通知消息({}):\n{}").format(nowtime, message)
        if len(*AtMobile)>0:
            if str(AtMobile[0]).lower()=='all':
                if "dingtalk" in push_webhook:
                    Ated = {"isAtAll":True}
                elif "weixin" in push_webhook:
                    Ated = ["@all"]
                else:
                    print("无法艾特所有人")
            else:
                Mobile = []
                for x in AtMobile:
                    Mobile.append(str(x))
                if "dingtalk" in push_webhook:
                    Ated = {"atMobiles":Mobile}
                elif "weixin" in push_webhook:
                    Ated = Mobile
                else:
                    print("无法艾特人")
        else:
            Ated = None
        if 'dingtalk' in push_webhook:
            text={"content":content}
            post_data={"msgtype": "text", "text": text,"at":Ated}
        elif 'weixin' in push_webhook:
            mapping={"content": content,"mentioned_mobile_list":Ated}
            post_data={"msgtype": "text", "text": mapping}
        else:
            text = {"text": content}
            post_data = {"msg_type": "text", "content": text}
            print(post_data)
        data = json.dumps(post_data)
        print(data)
        response = requests.post(url=push_webhook, headers=headers, data=data, verify=False)
        print(response.text)
    except Exception as e:
        print(e)

wehbook = "机器人wehbook"
data = "监控测试"
push_det("all",message=data,push_webhook=dindin)

由于飞书艾特人需要开发应用权限并上传应用,在此无飞书艾特人功能

二、企微,钉钉,飞书群机器人(Uibot版)

Uibot是来也公司制作的一个机器人流程自动化软件(RPA),小编最近也一直在学习Uibot知识,Uibot的编程语言是基于python语言开发的,因此与python有些类似。

/*
编写库:可添加多个Function的子程序,每个子程序内编写的流程可作为单独的命令重复使用到流程。
发布库:编写完子程序后,通过"发布库"填写命令配置信息,将命令库打包到指定目录。
使用库:在流程项目内,通过命令中心>自定义命令库目录导入安装后,在命令面板的扩展命令目录下使用。
*/


Function 飞书(message,push_webhook)
	Dim sRet = ""
	Dim nowtime = ""
	Dim content = ""
	Dim feishu = ""
	Dim text1 = ""
	Try
		feishu = InStr(push_webhook,'feishu',0)
		If feishu = 0
			TracePrint ("无效push_webhook")
		Else
			dTime = Time.Now()
			sRet = Time.Format(dTime,"yyyy-mm-dd HH:mm:ss")		
			content = ("通知消息("&sRet&"):\n"&message)
			text1 = {"text":content}
			post_data = {"msg_type":"text","content":text1}
			response = HTTP.PostJson(push_webhook,post_data, 60000)
		End If
	Catch message
		TracePrint message
	End Try
End Function

Function 企微(AtMobile,message,push_webhook)
	Dim sRet = ""
	Dim nowtime = ""
	Dim mapping = ""
	Dim content = ""
	Dim weixin = ""
	Dim arrRet = []
	Dim at = ""
	Try
		weixin = InStr(push_webhook,'weixin',0)
		If weixin = 0
			TracePrint ("无效push_webhook")
		Else
			dTime = Time.Now()
			sRet = Time.Format(dTime,"yyyy-mm-dd HH:mm:ss")		
			content = ("通知消息("&sRet&"):\n"&message)
			If Len(AtMobile)>0
				If AtMobile[0] = "all"
					at = ["@all"]
				Else
					For Each mobile In AtMobile
						Mobilet = CStr(mobile)
						at = push(arrRet,Mobilet)
					Next
				End If
			Else
				at = None
			End If
			mapping = {"content":content,"mentioned_mobile_list":at}
			post_data = {"msgtype":"text","text":mapping}
			response = HTTP.PostJson(push_webhook,post_data, 60000)
		End If
	Catch message
		TracePrint message
	End Try    
End Function

Function 钉钉(AtMobile,message,push_webhook)
	Dim sRet = ""
	Dim nowtime = ""
	Dim mapping = ""
	Dim content = ""
	Dim dindin = ""
	Dim arrRet = []
	Dim at = ""
	Try
		dindin = InStr(push_webhook,'dingtalk',0)
		If weixin = 0
			TracePrint ("无效push_webhook")
		Else
			dTime = Time.Now()
			sRet = Time.Format(dTime,"yyyy-mm-dd HH:mm:ss")		
			content = ("通知消息("&sRet&"):\n"&message)
			If Len(AtMobile)>0
				If AtMobile[0] = "all"
					at = {'isAtAll':True}
				Else
					For Each mobile In AtMobile
						Mobilet = CStr(mobile)
						at = {"atMobiles":push(arrRet,Mobilet)}
					Next
				End If
			Else
				at = None
			End If
			mapping = {"content":content}
			post_data = {"msgtype":"text","text":mapping,"at":at}
			response = HTTP.PostJson(push_webhook,post_data, 60000)
		End If
	Catch message
		TracePrint message
	End Try    
End Function

制作完成后,可以直接发布命令库,从而可以直接使用此项函数了



这篇关于企微,钉钉,飞书群机器人设计(python与Uibot设计)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程