对接企业微信,通过机器人发送群信息

1.自己创建一个群聊

2.通过右键群聊创建机器人

3.获取机器人webhook地址

4.查看企业微信的接口文档 

        由于我使用的是卡片模式,所以以下是基于卡片模式的编写,如选择其他可点击下方按钮进行查询群机器人配置说明 - 文档 - 企业微信开发者中心 (qq.com)

{
    "msgtype":"template_card",
    "template_card":{
        "card_type":"news_notice",
        "source":{
            "icon_url":"https://wework.qpic.cn/wwpic/252813_jOfDHtcISzuodLa_1629280209/0",
            "desc":"企业微信",
            "desc_color":0
        },
        "main_title":{
            "title":"欢迎使用企业微信",
            "desc":"您的好友正在邀请您加入企业微信"
        },
        "card_image":{
            "url":"https://wework.qpic.cn/wwpic/354393_4zpkKXd7SrGMvfg_1629280616/0",
            "aspect_ratio":2.25
        },
        "image_text_area":{
            "type":1,
            "url":"https://work.weixin.qq.com",
            "title":"欢迎使用企业微信",
            "desc":"您的好友正在邀请您加入企业微信",
            "image_url":"https://wework.qpic.cn/wwpic/354393_4zpkKXd7SrGMvfg_1629280616/0"
        },
        "quote_area":{
            "type":1,
            "url":"https://work.weixin.qq.com/?from=openApi",
            "appid":"APPID",
            "pagepath":"PAGEPATH",
            "title":"引用文本标题",
            "quote_text":"Jack:企业微信真的很好用~\nBalian:超级好的一款软件!"
        },
        "vertical_content_list":[
            {
                "title":"惊喜红包等你来拿",
                "desc":"下载企业微信还能抢红包!"
            }
        ],
        "horizontal_content_list":[
            {
                "keyname":"邀请人",
                "value":"张三"
            },
            {
                "keyname":"企微官网",
                "value":"点击访问",
                "type":1,
                "url":"https://work.weixin.qq.com/?from=openApi"
            },
            {
                "keyname":"企微下载",
                "value":"企业微信.apk",
                "type":2,
                "media_id":"MEDIAID"
            }
        ],
        "jump_list":[
            {
                "type":1,
                "url":"https://work.weixin.qq.com/?from=openApi",
                "title":"企业微信官网"
            },
            {
                "type":2,
                "appid":"APPID",
                "pagepath":"PAGEPATH",
                "title":"跳转小程序"
            }
        ],
        "card_action":{
            "type":1,
            "url":"https://work.weixin.qq.com/?from=openApi",
            "appid":"APPID",
            "pagepath":"PAGEPATH"
        }
    }
}

5.根据提供的请求体编写实体类

        有些写死的配置可以直接写死,灵活的配置可以抽取成配置yaml里

import lombok.Data;

import java.util.List;
import java.util.Map;

@Data
public class TemplateCard {

    private String card_type;

    private Source source;

    private MainTitle main_title;

    private List<Map<String, Object>> horizontal_content_list;

    private CardAction card_action;
    @Data
    public static class CardAction{
        private Integer type;

        private String url;
    }

    @Data
    public static class MainTitle{
        private String title;

        private String desc;
    }

    @Data
    public static class Source{
        private String icon_url="https://wework.qpic.cn/wwpic/252813_jOfDHtcISzuodLa_1629280209/0";

        private String desc="企业微信";

        private Integer desc_color=0;
    }
}


import lombok.Data;

@Data
public class MsgE {

    private TemplateCard template_card;

    private String msgtype;

}

6.编写配置类

qw:
  config:

    enabled:     //是否开启通知
      true 
    cardtype:    文本通知模版卡片下默认值
      text_notice
    title:       //标题
      
    url:        //点击卡片跳转地址
      
    msgtype:    //文本通知模版卡片类型
      template_card
    robotWebhook: //机器人的webhook
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "qw.config")
public class QWProperties {

    private Boolean enabled;

    private String cardtype;

    private String title;

    private String url;

    private String msgtype;

    private String robotWebhook;
}

7.编写工具类

        用的json工具类选择hutool就行,没有特别之处


import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.geelycv.ni.common.config.QWProperties;
import com.geelycv.ni.common.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class CompanyWxUtil {

    @Autowired
    private QWProperties qwProperties;


    public  void sendMsg(String orderId,String phone,String count){
        if (!qwProperties.getEnabled()){
            return;
        }
        HttpRequest request = HttpUtil.createPost(qwProperties.getRobotWebhook());
        TemplateCard textCard = new TemplateCard();

        textCard.setCard_type(qwProperties.getCardtype());
        textCard.setSource(new TemplateCard.Source());

        TemplateCard.MainTitle mainTitle = new TemplateCard.MainTitle();
        mainTitle.setTitle(qwProperties.getTitle());
        textCard.setMain_title(mainTitle);

        ArrayList<Map<String,Object>> maps = new ArrayList<>();
        addHorizontalContentList("单号",orderId,maps);
        addHorizontalContentList("下单人手机号",phone,maps);
        addHorizontalContentList("金额",count,maps);
        textCard.setHorizontal_content_list(maps);

        TemplateCard.CardAction cardAction = new TemplateCard.CardAction();
        cardAction.setType(1);
        cardAction.setUrl(qwProperties.getUrl());
        textCard.setCard_action(cardAction);

        MsgE msgE = new MsgE();
        msgE.setTemplate_card(textCard);
        msgE.setMsgtype(qwProperties.getMsgtype());

        log.info("请求的json为{}",JsonUtils.toJsonString(msgE));
        request.body(JsonUtils.toJsonString(msgE));
        HttpResponse execute = request.execute();
        Integer errcode = (Integer) JsonUtils.parseMap(execute.body()).get("errcode");
        if (errcode==0){
            log.info("企业微信消息发送成功");
        }else {
            log.info("企业微信消息发送失败,返回信息为{}",execute.body());
        }
    }

    private void addHorizontalContentList(String keyname,String value,ArrayList<Map<String,Object>> maps){
        HashMap<String, Object> map = new HashMap<>();
        map.put("keyname",keyname);
        map.put("value",value);
        maps.add(map);
    }
}

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Django中对接企业微信机器人,你需要使用企业微信提供的开发接口和Django的功能来实现。下面是一个简单的步骤: 1. 获取企业微信开发凭证:首先,在企业微信开发者平台上注册一个账号,并创建一个企业应用。在创建应用时,你将得到一个CorpID(企业ID)和一个Secret(密钥),它们将用于后续的认证和访问。 2. 配置企业微信回调URL:在企业微信开发者平台上,配置你的应用的回调URL。这个URL将用于接收企业微信发送的消息和事件。 3. 创建Django应用:使用Django的命令行工具创建一个新的Django应用。你可以运行`python manage.py startapp appname`来创建一个新的应用。"appname"是你给应用起的名称。 4. 编写机器人逻辑:在你创建的Django应用目录中,打开views.py文件,并编写处理企业微信消息和事件的逻辑。你可以使用企业微信提供的API来发送和接收消息,处理事件等。 5. 配置URL路由:打开项目目录下的urls.py文件,配置URL路由以将请求发送到你编写的机器人视图。你可以使用Django的URL路由机制来匹配URL和视图函数。 6. 配置企业微信回调URL验证:在你的Django应用中,编写一个视图函数来处理企业微信的URL验证请求。你需要将企业微信发送的验证参数进行处理,并返回相应的响应。 7. 运行开发服务器:保存你的代码并在命令行中运行`python manage.py runserver`来启动Django的开发服务器。确保你的应用和URL配置正确无误。 8. 部署应用程序:当你完成了测试,你可以将你的Django应用程序部署到生产环境中,以便可以在企业微信中使用你的机器人。 请注意,以上步骤只是一个简单的示例,实际上还有很多其他的细节和功能可以涉及。你需要参考企业微信开发文档和Django文档来了解更多详细信息,并根据你的具体需求进行定制和扩展。希望这对你有所帮助!如果你还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值