2023 年最新企业微信官方会话机器人开发详细教程(更新中)

news/2024/10/22 9:30:03/

目标是开发一个简易机器人,能接收消息并作出回复。

获取企业 ID

企业信息页面链接地址:https://work.weixin.qq.com/wework_admin/frame#profile

在这里插入图片描述

自建企业微信机器人

在这里插入图片描述

配置机器人应用详情

在这里插入图片描述

功能配置

在这里插入图片描述

接收消息服务器配置

配置消息服务器配置

在这里插入图片描述

配置环境变量

获取 agentId 和 Secret 信息
在这里插入图片描述

创建 .env 配置文件配置企业微信机器人应用的配置信息

corpId=
corpSecret=
agentId=

发送应用消息配置

配置应用消息:https://developer.work.weixin.qq.com/document/path/90236

请求方式POST(HTTPS)
请求地址https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN

机器人应用支持推送文本、图片、视频、文件、图文等类型。

配置 POST 请求参数
在这里插入图片描述
config.js 配置

const request = require('request')
const { config } = require('dotenv')
config()const corpId = process.env.corpId
const corpSecret = process.env.corpSecret
const agentId = parseInt(process.env.agentId)
const toUser = '@all'const tokenUrl = `https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${corpId}&corpsecret=${corpSecret}`
const sendMsgUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";function getToken(success, error) {request(tokenUrl, function(error, response, body) {if (!error && response.statusCode == 200) {var json = JSON.parse(body);console.log(json)success(json.access_token)} else {error('Token in error.')}})
}function sendMessage(token, content) {const requestData = {touser: toUser,msgtype: "text",agentid: agentId,safe: 0,text: {content: content}}request({url: `${sendMsgUrl}${token}`,method: "POST",json: true,headers: {"content-type": "application/json",},body: requestData}, function(error, response, body) {console.log(body)if (!error && response.statusCode == 200) {}});
}function sendText(content) {getToken((token) => {sendMessage(token, content)}, (error) => {console.log(error)})
}module.exports = {sendText,
}

定时发送任务

npm 下载 node-schedule 定时任务模块

npm install node-schedule

配置定时发送任务

const alarmWechat = require('./config.js')
const schedule = require('node-schedule')const scheduleTask = ()=> {schedule.scheduleJob('30 * * * * *',() =>{console.log('scheduleCronstyle:' + new Date());alarmWechat.sendText('testmessage')}); 
}scheduleTask();

开发者调试工具

在这里插入图片描述

群机器人配置

下载安装 axios 库

npm install axios

创建 .env 环境变量配置文件

HOOKURL=https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=【密钥】

具体实现

const axios = require("axios")
const { config } = require("dotenv")
config()let data = {msgtype: "text",text: {content: "helloworld"}
}axios.post(hookurl, data, {headers: {"Content-Type": "application/json"}
}).then(res => {console.log(res)
}).catch(err => {console.log(err)
})

http://www.ppmy.cn/news/1217788.html

相关文章

2023nacos源码解读第3集——nacos-client核心功能之微服务调用和配置管理测试

文章目录 1、测试项目2、项目注意事项3、 测试核心功能3.1 测试服务调用与负载均衡3.2 测试配置监听 4、参考文档 1、测试项目 项目地址 nacos-service-a nacos-service-b 2、项目注意事项 项目初始化可以使用aliyun spring initializer ,以更方便的使用springcloud alibaba…

vscode使用flake8设置单行最长字符限制设置失败的问题

vscode使用flake8设置单行最长字符限制设置失败的问题 问题描述解决方案 问题描述 如图所示,使用flake8单行字数过长,就会有有红色底的波浪线 一般情况下很多教程都会让你在setting.json里面设置 但是我打开我的setting.json,发现我已经进…

Java怎么对复杂的数据类型排序和比大小

目录 一.对复杂的数据类型比大小 Comparable接口 compareTo方法 二.对复杂数据类型排序 三.总结 一.对复杂的数据类型比大小 假如我们现在有个学生类,并且我们实例化出了俩个学生对象,他们各自有各自的名字和年龄属性,我们如何对他们进…

H5游戏源码分享-超级染色体小游戏

H5游戏源码分享-超级染色体小游戏 游戏玩法 不断地扩大发展同颜色的色块 用最少的步数完成游戏 <!DOCTYPE html> <html><head><meta charset"UTF-8"><meta name"viewport"content"widthdevice-width,user-scalableno,init…

Linux 系统概述

Part I: Linux 系统概述 什么是 LinuxLinux 的历史和版本Linux 发行版介绍Linux 的优缺点 Part II: Linux 安装与配置 5. 硬件要求与准备工作 6. 安装 Linux 操作系统 7. Linux 系统初始化设置 8. Linux 系统更新与升级 9. Linux 基础配置 Part III: Linux 命令行 10. Linux…

数据结构:反射

基本概念 反射中的四个类 Class类 Java文件在被编译之后&#xff0c;生成了.class文件&#xff0c;JVM此时解读.class文件&#xff0c;将其解析为java.lang.Class 对象&#xff0c;在程序运行时每个java文件就最终变成了Class类对象的一个实例。通过反射机制应用这个 实例就…

Domino为外出Internet邮件设置DKIM签名

大家好&#xff0c;才是真的好。 如果你看了上篇《Domino中和邮件安全有关的SPF、DKIM介绍》内容&#xff0c;想必就对DKIM概念不陌生&#xff0c;当然&#xff0c;上篇我们讲的是邮件入站的SFP、DKIM签名检查&#xff0c;这篇讲述的是外出邮件的DKIM签名。 是的&#xff0c;…

LoadRunner Error -26377: No match found for the requested parameter 获取参数的方法要前置

web_reg_save_param("access_token","LBaccess_token\":\"", //获取返回参数左右边界值时&#xff1a;有"双引号时,需要使用\来进行转义"RB\",","SearchBody",LAST);web_reg_save_param("token_type&q…