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)
})
相关推荐
阿里云大数据AI技术6 小时前
莫刻机器人LJM登榜WorldArena第二!阿里云PAI提供全流程训练支撑
人工智能·机器人
guanchabao9 小时前
教育培训行业招生外呼机器人怎么设置话术?
机器人
科技圈快迅11 小时前
在机场与高铁站:智能服务机器人正在深度对接业务系统,实现从信息孤岛到移动服务窗口的转变
大数据·人工智能·机器人
renhongxia111 小时前
WAIC 2026闭幕:机器人从跳舞变成拧螺丝
机器人
武子康11 小时前
VLA 已经能输出动作,为什么机器人仍需要多时间尺度闭环
人工智能·机器人·agent
workflower12 小时前
情境感知系统
人工智能·机器学习·设计模式·自然语言处理·机器人
CoreTK_EMC13 小时前
机器人 EMC 器件选型指南:适配多关节伺服与灵巧手的量产级选型方法论
机器人·芯通康·机器人 emc 器件选型·伺服电机 emc 器件·灵巧手 esd 选型·工业级共模电感·机器人关节防护器件
噗噗1213 小时前
企业微信 API 异常监控、全局错误码与限流处理最佳实践
企业微信
wechatbot88814 小时前
解决企业微信官方 API 短板:iPad 协议全功能对接方案
java·汇编·windows·http·微信·企业微信
q***o6914 小时前
承接“高频碎片化问询”:服务机器人如何协同枢纽运营,优化旅客体验
机器人