0.2.3 • Published 5 years ago
wechaty-puppet-mock-koa v0.2.3
wechaty-puppet-mock-koa
packaging with koa based on wechaty-puppet-mock, It is convenient to test wechaty with restful interface.
Install
$ npm install wechaty-puppet-mock-koa --save-devUsage
PuppetKoa Class
constructor([options])- new classPuppetKoainstanceoptions.port- http listen port, default to3000.options.prefix- http request prefix, default to/mock.options.loginData- information used to simulate login.options.loginData.id- login bot id, default tobot_id.options.loginData.status- login bot status, default to0.options.loginData.qrcode- login bot qrcode, default tohttps://not-exist.com.
options.initFile- initial importedlogin,contact,roomdata, is a JSON file path, default tonull.
NOTE:
loginDatanot required ifinitFileexists
Example
index.js
const {PuppetPadplus} = require('wechaty-puppet-padplus');
let puppet = null;
if (process.env.NODE_ENV != 'dev') {
// use ipad puppet
puppet = new PuppetPadplus({token: 'xxx'});
} else {
// use mock puppet
const {PuppetKoa} = require('wechaty-puppet-mock-koa');
puppet = new PuppetKoa({port:3001,prefix:'/api',initFile:'./initFile.json'});
}
// create bot
const bot = new Wechaty({name: config.botName, puppet});
...
// receive message
bot.on('message', async (msg) => {
console.log(msg.from().name(),msg.text());
});
...initFile.json
loginconfiguration is required.- the configuration of
loginandcontactis consistent with the parameters ofcreate contact ([PUT] /mock/contact)interface - people in
roomMemberListare better existing in thecontactList - the configuration of
roomis consistent with the parameters ofcreate room ([PUT] /mock/room)interface
// /doc/mock.json
{
"login": {
"id": "bot_id",
"name": "bot_name",
"avatar": "https://avatars0.githubusercontent.com/u/21285357?s=200&v=4",
"type": 1,
"gender": 1,
"city": "shanghai",
"alias": "bot_alias",
"star": false,
"weixin": "weixin",
"friend": true,
"address": "shanghai",
"province": "shanghai"
},
"contactList": [
{
"id": "friend_1",
"name": "friend_1_name",
"avatar": "https://avatars0.githubusercontent.com/u/21285357?s=200&v=4",
"type": 1,
"gender": 1,
"city": "shanghai",
"alias": "friend_1_alias",
"star": false,
"weixin": "weixin",
"friend": true,
"address": "shanghai",
"province": "shanghai"
}
],
"roomMemberList": [
{
"id": "friend_1",
"roomAlias": "friend_1_alias",
"inviterId": "bot_id"
}
],
"roomList": [
{
"id": "room_1",
"topic": "room_topic",
"avatar": "https://avatars0.githubusercontent.com/u/21285357?s=200&v=4",
"ownerId": "bot_id",
"adminIdList": [
"bot_id"
],
"memberIdList": [
"bot_id",
"friend_1"
]
}
]
}API sample
send message
// request curl -X POST \ http://localhost:3000/mock/message/single \ -H 'Cache-Control: no-cache' \ -H 'Content-Type: application/json' \ -H 'Postman-Token: f7f34936-8a75-41f3-b008-7c48992bceca' \ -d '{ "type": 7, "text": "hello", "toId": "bot_id", "fromId": "friend_1", "filename": "/path/test.json" }' // response {"state":200,"message":"Request success!"} // console friend_1_name hello
Reference resources
- API Specification:/doc/api_specification.md
- Postman Script: /doc/wechaty-puppet-mock-koa.postman_collection.json
Note
- Run with wechaty 0.46.x+
0.2.3
5 years ago
0.2.2
5 years ago
0.2.1
5 years ago
0.1.0
6 years ago
0.0.17
6 years ago
0.0.18
6 years ago
0.0.16
6 years ago
0.0.15
6 years ago
0.0.14
6 years ago
0.0.13
6 years ago
0.0.12
6 years ago
0.0.11
6 years ago
0.0.10
6 years ago
0.0.9
6 years ago
0.0.8
6 years ago
0.0.7
6 years ago
0.0.6
6 years ago
0.0.5
6 years ago
0.0.4
6 years ago
0.0.3
6 years ago
0.0.2
6 years ago
0.0.1
6 years ago