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

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

获取企业 ID

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

自建企业微信机器人

配置机器人应用详情

功能配置

接收消息服务器配置

配置消息服务器配置

配置环境变量

获取 agentId 和 Secret 信息

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

csharp 复制代码
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 配置

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 定时任务模块

csharp 复制代码
npm install node-schedule

配置定时发送任务

csharp 复制代码
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 库

csharp 复制代码
npm install axios

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

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

具体实现

js 复制代码
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)
})
相关推荐
资源开发与学习2 天前
机器人运动规划源码解析
机器人
hi0_63 天前
机器学习实战(一): 什么是机器学习
人工智能·机器学习·机器人·机器学习实战
大视码垛机3 天前
速度与安全双突破:大视码垛机重构工业自动化新范式
大数据·数据库·人工智能·机器人·自动化·制造
WWZZ20253 天前
视觉SLAM第10讲:后端2(滑动窗口与位子图优化)
c++·人工智能·后端·算法·ubuntu·机器人·自动驾驶
deephub3 天前
机器人逆运动学进阶:李代数、矩阵指数与旋转流形计算
人工智能·机器学习·矩阵·机器人·李群李代数
武子康4 天前
AI-调查研究-76-具身智能 当机器人走进生活:具身智能对就业与社会结构的深远影响
人工智能·程序人生·ai·职场和发展·机器人·生活·具身智能
小鹿清扫日记4 天前
从蛮力清扫到 “会看路”:室外清洁机器人的文明进阶
人工智能·ai·机器人·扫地机器人·具身智能·连合直租·有鹿巡扫机器人
shuidaoyuxing4 天前
机器人防爆的详细讲解
机器人
物联网软硬件开发-轨物科技4 天前
【轨物方案】赋能绿色能源新纪元:轨物科技发布光伏清洁机器人智能控制与运维解决方案
科技·机器人·能源
尤齐4 天前
《机器人抓取:从经典到现代的综述》内容的提取和凝练:
机器人