云枢业务集成调用钉钉机器人发送Markdown消息

347 阅读4分钟

 背景

在使用低代码平台 奥哲·云枢 的时候,有时希望把一些表单内容发送到钉钉群,图片或者链接。 那怎么才能方便快捷的发送呢? 直接发markdown消息就可以了,因为钉钉支持显示markdown。 本文记录了如何在云枢使用钉钉机器人发送markdown消息到群,并分享了如何使用JavaScript拼接markdown的方法。

一、云枢新建业务集成

1、点检新建服务

 ​编辑

地址URL这一栏,因为是公司的网络有中转,所以这里打码。网络如果默认互通,可以直接填钉钉的地址

oapi.dingtalk.com/

​编辑

2、新建一个方法

​编辑

3、填下以下参数后保存

​编辑

二、表单基础设置

1、表单添加长文本,并设置不可见

​编辑

2、在数据模型下,新建一条业务规则

​编辑

3、设置相应的属性

​编辑

access_token 填上钉钉群自定义机器人的密钥。

获取方法:获取自定义机器人 Webhook 地址 - 钉钉开放平台 (dingtalk.com)

Content-Type 默认 application/json

msgtype 默认 markdown

title 使用 "单据号" 会好一点,设置成 "数据标题"的话,有可能因为格式上带了时间而发送失败。

反正 title 的内容是不会发到群里的,只有 text 的内容才能发到群里。

其他参数按照下图来设置就可以了。

​编辑

4、流程设计中添加业务规则

​编辑

三、云枢添加markdown类

1、复制以下所有代码

    /**  
    * 用于创建Markdown文本的类
    */
    class MakeMarkDown {
        constructor() {
            this._text = '';
            this.headingSymbols = ['#', '##', '###', '####', '#####', '######'];
        }

        /**  
        * 添加一次换行,不结束当前语句状态
        */
        addln() {
            this._text = this._text + '\n';
        }
        /**
         * 添加两次换行,结束当前语句,起新行
         */
        addEmptyLine() {
            this._text += '\n\n';
        }
        /**  
        * 添加文本 
        * @param {string} text 文本  
        */
        addText(text) {
            this._text = this._text + text;
        }
        /**  
        * 添加文本 (换新行)  
        * @param {string} text 文本  
        */
        addTextln(text) {
            this.addText(text);
            this.addEmptyLine();
        }

        /**  
        * 添加标题 (换新行)  
        * @param {number} level 标题等级,1~6级,1级最大,6级最小  
        * @param {string} text 文本  
        */
        addTitleln(level, text) {
            if (level < 1 || level > this.headingSymbols.length) {
                throw new Error('Invalid title level. Must be between 1 and ${this.headingSymbols.length}.');
            }
            this.addTextln(this.headingSymbols[level - 1] + ' ' + text + ' ');
        }
        /**  
         * 添加**加粗**文本 
         * @param {string} text 文本  
         */
        addBold(text) {
            this.addText('**' + text + '** ');
        }
        /**  
         * 添加**加粗**文本 (换新行)   
         * @param {string} text 文本  
         */
        addBoldln(text) {
            this.addBold(text);
            this.addEmptyLine();
        }
        /**  
         * 添加*斜体*文本 
         * @param {string} text 文本  
         */
        addItalic(text) {
            this.addText('*' + text + '* ');
        }
        /**  
         * 添加*斜体*文本  (换新行)  
         * @param {string} text 文本  
         */
        addItalicln(text) {
            this.addItalic(text);
            this.addEmptyLine();
        }
        /**  
         * 添加> 引用文本
         * @param {string} text 文本  
         */
        addReference(text) {
            this.addText('> ' + text);
        }
        /**  
         * 添加> 引用文本 (换新行)   
         * @param {string} text 文本  
         */
        addReferenceln(text) {
            this.addReference(text);
            this.addEmptyLine();
        }
        /**  
         * 添加照片  
         *  
         * @param key {object} 图片控件的KEY对象,例如:this.Attachment1713107713475
         * @param key.value {Array<object>} 包含多个照片信息的数组  
         *         其中每个对象包含以下属性:  
         *         - name {string}: 照片的名称  
         *         - url {string}: 照片的URL地址  
         *  
         * @description  
         * 遍历key.value数组,将每个照片以Markdown格式 ![图片名](图片链接) 添加到文本中。  
         * 在处理URL时,会截取'&T='及其后的部分,只保留'&T='前的URL作为图片链接。  
         */
        addPhoto(key) {
            if (!Array.isArray(key.value)) {
                throw new Error('Invalid photo key. Value must be an array.');
            }
            key.value.forEach((photo) => {
                if (!photo.name || !photo.url) {
                    throw new Error('Invalid photo object. Must have name and url properties.');
                }
                const name = photo.name;
                const url = photo.url.split('&T=')[0]; // 截取 URL  
                const markdownImage = `![${name}](${url})`;
                this.addText(markdownImage + ' ');
            });
        }
        /**  
         * 添加照片并添加空行  
         *  
         * @param key {object} 包含照片信息的对象  
         * @param key.value {Array<object>} 包含多个照片信息的数组  
         *         其中每个对象包含以下属性:  
         *         - name {string}: 照片的名称  
         *         - url {string}: 照片的URL地址  
         *  
         * @description  
         * 调用addPhoto方法添加照片后,再调用addEmptyLine方法添加一个空行。  
         * 这个方法主要用于在添加完照片后,保持文本格式的整洁,通过添加一个空行来分隔内容。  
         */
        addPhotoln(key) {
            this.addPhoto(key);
            this.addEmptyLine();
        }
        /**  
        * 把文本添加到无序列表
        * @param {string} text 数组
        */
        addList(text) {
            this.addText('- ' + text + ' ');
            this.addln();
        }
        /**  
         * 把数组添加到无序列表 (换新行)  
         * @param {array} list 数组
         */
        addListFormArrayln(list) {
            for (let i = 0; i < list.length; i++) {
                this.addText('- ' + list[i] + ' ');
                this.addln();
            }
            this.addEmptyLine();
        }
        /**  
        * 把数组添加到有序列表 (换新行)  
        * @param {array} list 数组
        */
        addIndexListFormArrayln(list) {
            for (let i = 0; i < list.length; i++) {
                this.addText((i + 1).toString() + '. ' + list[i] + ' ');
                this.addln();
            }
            this.addEmptyLine();
        }
        /**  
         * 获取当前生成的Markdown文本内容  
         *  
         * @returns {string} 返回当前类实例中存储的Markdown文本内容  
         */ 
        getText() {
            return this._text;
        }
    }

2、粘贴到云枢表单的以下位置

粘贴到 1 和 2 之间,这里的代码只是折叠了,点击左边的 “ 44 > ” 就能 展开 和 折叠 

​编辑

四、类的使用

这里临时用另一个表单做测试(和上面教程用的不是同一张表,阁下设置的时候,还是按照教程,用同一个表配置和测试就行)

1、在校验块中使用代码

这里蓝色的地方是控件的编码,下面的例程,看看就很容易懂了。

​编辑控件编码的查看方法

​编辑

2、填写表单进行测试

​编辑

3、在钉钉群查看效果