wechat용 node.js 로봇입니다.
WeChat 공개 플랫폼에서 제공하는 개방형 정보 인터페이스의 자동 응답 시스템입니다.
weixin-robot
webot 및 wechat-mp를 위한 고급 래퍼입니다. webot
응답 규칙 정의를 담당 wechat-mp
WeChat 서버와의 통신을 담당합니다.
특징:
WeChat 계정을 추가하고 효과를 시험해 보세요.
이 프로젝트를 사용하는 WeChat 로봇의 추가 목록
var express = require ( 'express' ) ;
var webot = require ( 'weixin-robot' ) ;
var app = express ( ) ;
// 指定回复消息
webot . set ( 'hi' , '你好' ) ;
webot . set ( 'subscribe' , {
pattern : function ( info ) {
return info . is ( 'event' ) && info . param . event === 'subscribe' ;
} ,
handler : function ( info ) {
return '欢迎订阅微信机器人' ;
}
} ) ;
webot . set ( 'test' , {
pattern : / ^test / i ,
handler : function ( info , next ) {
next ( null , 'roger that!' )
}
} )
// 你可以获取已定义的 rule
//
// webot.get('subscribe') ->
//
// {
// name: 'subscribe',
// pattern: function(info) {
// return info.is('event') && info.param.event === 'subscribe';
// },
// handler: function(info) {
// return '欢迎订阅微信机器人';
// }
// }
//
// 接管消息请求
webot . watch ( app , { token : 'your1weixin2token' , path : '/wechat' } ) ;
// 如果需要多个实例(即为多个微信账号提供不同回复):
var webot2 = new webot . Webot ( ) ;
webot2 . set ( {
'/hi/i' : 'Hello' ,
'/who (are|r) (you|u)/i' : 'I'm a robot.'
} ) ;
webot2 . watch ( app , {
token : 'token2' ,
path : '/wechat_en' , // 这个path不能为之前已经监听过的path的子目录
} ) ;
// 启动 Web 服务
// 微信后台只允许 80 端口
app . listen ( 80 ) ;
// 如果你不想让 node 应用直接监听 80 端口
// 可以尝试用 nginx 或 apache 自己做一层 proxy
// app.listen(process.env.PORT);
// app.enable('trust proxy');
그런 다음 WeChat 공개 플랫폼 백그라운드에서 인터페이스 주소와 토큰을 입력하거나 webot-cli를 사용하여 메시지를 디버깅할 수 있습니다.
모든 것이 순조롭게 진행되어 자신만의 로봇을 만들었다면 이 프로젝트의 위키 페이지에 오신 것을 환영합니다. 계정을 추가하세요.
테스트 메시지 전송을 위한 실행 파일 webot
제공합니다. npm
사용하여 webot-cli를 설치합니다.
npm install webot-cli -g
webot-cli는 WeChat 사용자 정의 메뉴를 처리하는 기능을 제공합니다. 설치 후 다음을 실행합니다.
webot help menu
0.5.0 - 더욱 간소화된 wechat-mp 모듈로 전환
참고: 이제 세션 지원을 활성화하려면 webot.watch
app.use(connect.session())
앞에 있어야 합니다.
> 구체적인 규칙 정의는 웹봇 문서를 참고하세요.
주요 API:
webot 규칙의 핸들러가 수신한 정보 객체에는 요청 메시지 내용과 세션 지원이 포함되어 있습니다.
wexin-robot
의 info
WeChat의 요청 콘텐츠를 js 명명 규칙에 더 부합하는 값으로 패키지하고 MsgType
에 따라 info.param
객체에 추가 매개변수를 저장합니다. 이를 통해 info
개체의 표준화가 보장되고 다양한 플랫폼에서 동일한 로봇을 더 쉽게 사용할 수 있습니다.
info.raw
를 통해 WeChat 공식 문서와 일치하는 매개변수 개체를 얻을 수 있습니다.
원래 요청 매개변수와 정보 속성의 비교표:
官方参数名 定义 info对象属性 备注
-------------------------------------------------------------------------------------------------------
ToUserName 开发者微信号 info.sp sp means "service provider"
FromUserName 发送方帐号(一个OpenID) info.uid
CreateTime 消息创建时间 (整型)
MsgId 消息id info.id
MsgType 消息类型 info.type
-------------------------------------------------------------------------------------------------------
Content 文本消息内容 info.text MsgType == text
-------------------------------------------------------------------------------------------------------
PicUrl 图片链接 info.param.picUrl MsgType == image
-------------------------------------------------------------------------------------------------------
Location_X 地理位置纬度(lat) info.param.lat MsgType == location
Location_Y 地理位置经度(lng) info.param.lng
Scale 地图缩放大小 info.param.scale
Label 地点名 info.param.label 可能为空
-------------------------------------------------------------------------------------------------------
Title 消息标题 info.param.title MsgType == link
Description 消息描述 info.param.description
Url 消息链接 info.param.url
-------------------------------------------------------------------------------------------------------
Event 事件类型 info.param.event MsgType == event
subscribe(订阅)、
unsubscribe(取消订阅)、
CLICK(自定义菜单点击事件)
LOCATION(上报地理位置事件)
EventKey 事件KEY值,与自定义菜单接 info.param.eventKey
口中KEY值对应
--------------------------------------------------------------------------------------------------------
MediaId 媒体文件的 id info.param.mediaId MsgType == voice / video
Recognition 语音识别的文本 info.param.recognition MsgType == voice
ThumbMediaId 视频消息缩略图的媒体id info.param.thumbMediaId MsgType == video
Format 音频文件的格式 info.param.format
알아채다:
Location_X
, Location_Y
는 제외됩니다.info.text
포함됩니다. info.text
이지만 info.type
은 다릅니다.예를 들어 위치 메시지(MsgType === 'location')는 다음으로 변환됩니다.
{
uid : 'the_FromUserName' ,
sp : 'the_ToUserName' ,
id : 'the_MsgId' ,
type : 'location' ,
param : {
lat : 'the_Location_X' ,
lng : 'the_Location_Y' ,
scale : 'the_Scale' ,
label : 'the_Label'
}
}
대부분의 경우 info.reply에 직접 값을 할당할 필요는 없습니다 .
rule.handler
또는 callbak의 반환 값에 응답 메시지의 내용만 제공하면 됩니다. webot.watch
와 함께 제공되는 Express 미들웨어는 자동으로 info.reply
에 값을 할당하고 이를 XML로 패키징하여 전송합니다. 위챗 서버.
info.reply
가 지원하는 데이터 유형:
info . reply = '收到你的消息了,谢谢'
title 消息标题
url 消息网址
description 消息描述
picUrl 消息图片网址
info . reply = {
title : '消息标题' ,
url : 'http://example.com/...' ,
picUrl : 'http://example.com/....a.jpg' ,
description : '对消息的描述出现在这里' ,
}
// or
info . reply = [ {
title : '消息1' ,
url : 'http://example.com/...' ,
picUrl : 'http://example.com/....a.jpg' ,
description : '对消息的描述出现在这里' ,
} , {
title : '消息2' ,
url : 'http://example.com/...' ,
picUrl : 'http://example.com/....a.jpg' ,
description : '对消息的描述出现在这里' ,
} ]
title 标题
description 描述
musicUrl 音乐链接
hqMusicUrl 高质量音乐链接,wifi 环境下会优先使用该链接播放音乐
reply.type
'music'
으로 지정해야 합니다.
info . reply = {
type : 'music' ,
title : 'Music 101' ,
musicUrl : 'http://....x.mp3' ,
hqMusicUrl : 'http://....x.m4a'
}
위챗을 즐기며 로봇이 되어보세요!
메시지에 답장하고 싶지 않은 경우 info.noReply = true
로 설정하면 됩니다.
// 比如对于语音类型的消息不回复
webot . set ( 'ignore' , {
pattern : function ( info ) {
return info . is ( 'voice' ) ;
} ,
handler : function ( info ) {
info . noReply = true ;
return ;
}
} ) ;
(MIT 라이센스)
본 소프트웨어 및 관련 문서 파일('소프트웨어')의 사본을 취득한 모든 사람에게 사용, 복사, 수정, 병합에 대한 권리를 포함하되 이에 국한되지 않고 제한 없이 소프트웨어를 취급할 수 있는 권한이 무료로 부여됩니다. , 소프트웨어 사본을 게시, 배포, 재라이센스 부여 및/또는 판매하고, 소프트웨어를 제공받은 사람에게 다음 조건에 따라 그렇게 하도록 허용합니다.
위의 저작권 고지와 본 허가 고지는 소프트웨어의 모든 사본 또는 상당 부분에 포함됩니다.
소프트웨어는 상품성, 특정 목적에의 적합성 및 비침해에 대한 보증을 포함하되 이에 국한되지 않고 명시적이든 묵시적이든 어떠한 종류의 보증 없이 '있는 그대로' 제공됩니다. 청구, 손해 또는 기타 소프트웨어나 소프트웨어의 사용 또는 기타 거래로 인해 발생하거나 이와 관련하여 발생하는 계약, 불법 행위 또는 기타 행위에 대한 책임.