企业微信机器人自动消息发送webhook接入代码
主要代码类如下
package com.test.common;
import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map;
import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.json.JSONObject;
/*
- 文件名:com.test.common;.SendRobotMsgUtils.java
- 简述:TODO
- 详述:
- 新建时间:2022年5月2日 下午12:31:41
- 修改内容:[新增]
- 修改时间:2022年5月2日 下午12:31:41
- 版本:1.0
- / public class SendRobotMsgUtils {
@SuppressWarnings({ "rawtypes", "unchecked" })
public boolean wechatRobotTxtMsg(String webhookUrl,String msgContent,List<String> mentioned_mobile_list) {boolean falg=false;//webhook地址 String msgtype="text";HttpResponse response=null;try {if(webhookUrl!=null&&msgContent!=null) {HttpPost httpPost=new HttpPost(webhookUrl);CloseableHttpClient httpclient=HttpClients.createDefault();httpPost.setHeader("Content-Type", "application/json; charset=utf-8");Map<String,Object> param=new HashMap();param.put("msgtype",msgtype);Map<String,Object> param_msg=new HashMap();param_msg.put("content", msgContent);if(mentioned_mobile_list!=null&&mentioned_mobile_list.size()>0) {param_msg.put("mentioned_mobile_list", mentioned_mobile_list);}//封装消息体param.put(msgtype, param_msg);String txtmsg=JSONObject.valueToString(param);System.out.println("*************执行前txtmsg:"+txtmsg);StringEntity entity = new StringEntity(txtmsg, "utf-8");httpPost.setEntity(entity);System.out.println("*************执行前:"+httpPost.toString());response=httpclient.execute(httpPost);if(response!=null&&response.getStatusLine()!=null&&response.getStatusLine().getStatusCode()==200) {String responseStr=response.getEntity().toString();falg=true;System.out.println("*************执行结果:"+responseStr);}}else {System.out.println("*************输入参数为空********************************");}} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return falg;
}@SuppressWarnings({ "rawtypes", "unchecked" })
public boolean wechatRobotMarkdownMsg(String webhookUrl,String msgContent,List<String> mentioned_mobile_list) {boolean falg=false;//webhook地址 String msgtype="markdown";HttpResponse response=null;try {if(webhookUrl!=null&&msgContent!=null) {HttpPost httpPost=new HttpPost(webhookUrl);CloseableHttpClient httpclient=HttpClients.createDefault();httpPost.setHeader("Content-Type", "application/json; charset=utf-8");Map<String,Object> param=new HashMap();param.put("msgtype",msgtype);Map<String,Object> param_msg=new HashMap();param_msg.put("content", msgContent);if(mentioned_mobile_list!=null) {param_msg.put("mentioned_mobile_list", mentioned_mobile_list);}//封装消息体param.put(msgtype, param_msg);String txtmsg=JSONObject.valueToString(param);System.out.println("*************执行前txtmsg:"+txtmsg);StringEntity entity = new StringEntity(txtmsg, "utf-8");httpPost.setEntity(entity);System.out.println("*************执行前:"+httpPost.toString());response=httpclient.execute(httpPost);if(response!=null&&response.getStatusLine()!=null&&response.getStatusLine().getStatusCode()==200) {String responseStr=response.getEntity().toString();falg=true;System.out.println("*************执行结果:"+responseStr);}}else {System.out.println("*************输入参数为空********************************");}} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return falg;
}public static void main(String[] args) {SendRobotMsgUtils srmu=new SendRobotMsgUtils();SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:SSS");String nowstr=sdf.format(new Date());String webhookUrl="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=c17661d0c2e6";String msgContent="监控机器人-普通文本消息\n消息时间:"+nowstr+";\n交易日期+支付渠道+渠道笔数+当日总笔数\n"+ "你们所有人可以关闭此群消息!!!";List<String> mlist=new ArrayList<String>();mlist.add("@all");mlist.add("18688888888");
// srmu.wechatRobotTxtMsg(webhookUrl,msgContent,mlist); String msgmkContent= "
监控名称
\r\n" + " <tr style="border: 3px ;border-color: black;">\r\n" + " <th width="25%">编号\r\n" + " <th width="25%">用户名\r\n" + " <th width="25%">姓名\r\n" + " <th width="25%">联系电话\r\n" + " \r\n" + " \r\n" + " \r\n" + " <td style="border: 3px ;border-color: black;">01\r\n" + " 张三\r\n" + " 姓名\r\n" + " 18688888888\r\n" + " \r\n" + "\r\n"; srmu.wechatRobotMarkdownMsg(webhookUrl, msgmkContent, mlist);
}
} 实际开发完成的功能效果可以访问:https://www.onekbit.com/wechatrobotinfo/selectlist.do 体验使用。
使用微信机器人定时群发消息:
本文由博客一文多发平台 OpenWrite 发布!
企业微信机器人自动消息发送webhook接入代码相关推荐
- springboot实现企业微信机器人自动按时播报天气
springboot实现企业微信机器人自动按时播报天气 第一步搭建项目...这个没有什么好说的 配置: <dependency><groupId>org.apache.http ...
- 企业微信机器人脚本python_python提取数据库数据并实现企业微信机器人定时消息推送...
之前发过一个企业微信群机器人接口调用发提醒的文章,是用最简单的VBS脚本+Windows任务计划来实现的,仅实现了定时发送固定内容的提醒.其实企业微信机器人就是一个webhook接口,用户以post ...
- 获取MAC OS苹果电脑配置信息,发送到企业微信机器人自动提醒
import subprocess import requests import time import jsonclass MacInfo:def __init__(self):self.strSe ...
- Beego搭建企业微信机器人定时消息推送
Beego搭建企业微信Robot定时消息推送 功能说明 PostMan添加job msgType 默认是:text并且支持@all markdown支持颜色和样式但是不支持@all cron quar ...
- python调用企业微信机器人API-自动发送文本、图片与CSV文件3种方式
前言:使用企业微信app端,在群里添加robot并命名:PC端点击robot可编辑相关资料. 正文 1.调用企业微信API-发送文本. 1.1接口说明: 这里文本发送markdown格式.文本具体呈现 ...
- python 企业微信机器人自动推送文字和文件
1.首先需要在企业微信对应群里创建机器人. 2.获取后查看机器人的webhook. 3.发送文件需要先上传文件,所以通过编辑机器人查看api文档,获取上传url: 4.代码 #传入文件 def pos ...
- 常见问题:企业微信机器人账号消息如何对应
有用户经常会问到这个问题,为什么我的企业微信群机器人修改/删除后,在腾讯云HiFlow场景连接器里的其他流程会被影响?或者为什么我的企业微信群机器人修改之后,所有群的消息都发到一个群了? 首先,我们要 ...
- 利用python进行企业微信机器人自动发送消息
def test_004_robot(self):headers = {"Content-Type": "text/plain"}s="卖品,打印码: ...
- 企业微信机器人?用Python,几行代码搞定!
直接上代码 # -*-coding:utf-8-*- # 刘文豪 # 大帅哥 import requests import timedef postmessage():url = "http ...
最新文章
- java创建线程的两种方法区别
- Pandas的DataFrame输出截断和省略问题
- linux sock结构体,struct socket结构体详解
- vs code打开文件显示的中文乱码
- MicroRNA Ranking(Tehran2016)
- sklearn分类器:朴素贝叶斯
- pdf无法启动因计算机丢失,解决打开 PDF 文件时出现的文档损坏错误
- 信息学奥赛C++语言:换钱
- 听云CTO访谈:解读现代应用性能管理(APM)技术
- eclipse中配置spring约束schema
- 为什么旧硬件的驱动越来越难找了?
- 微信小程序获得微信头像和昵称
- 如何利用EasyRecovery恢复c盘已删文档
- 魔方机器人之下位机编程------下位机完整程序
- redhat下载镜像官方地址
- 安装GLPI资产管理
- 微信小程序判断是否开启定位功能引导重新打开定位权限
- Android开发相关介绍--基础篇
- MySQL 查看和终止正在运行的连接线程
- 日志模块、正则表达式、时间模块