Skip to content

Commit

Permalink
新增一些基础功能
Browse files Browse the repository at this point in the history
  • Loading branch information
fangmuke committed Nov 14, 2020
1 parent dee87f6 commit 2f51d7d
Show file tree
Hide file tree
Showing 24 changed files with 7,261 additions and 2,487 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.idea
node_modules
/friday.memory-card.json
dist/
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,39 @@
# WeChat Bot

## Status:Dev.
### Status:Dev.
微信机器人

## How To Use

### 1. check node version, need to be $ge than v12.0.0
```
node --version // >= v12.0.0
```

### 2. install node modules
After cloning the repo, change current directory to repo's root folder:

RUN: `yarn install` or `npm install`

### 3. add local config file
RUN `cp ./config/default.json ./config/local.json`.

### 4. override local config file
**Contact [haoda](mailto:oxddoxdd@gmail.com) to request following information.**

* server host
* server port
* padlocal token
* server ca certification

Then fill them into `local.json`.

*`local.json`has been ruled by .gitignore, credentials are safe to store.*

### 5. try the run
RUN: `yarn run dev` or `npm run dev`

### 6. build
RUN: `yarn run build` or `npm run build`

### 7. run
RUN: `yarn run start` or `npm run start`
3 changes: 3 additions & 0 deletions config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!default.json
!.gitignore
11 changes: 11 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"padLocal": {
"host": "127.0.0.1",
"port": 0,
"token": "",
"serverCAFilePath": ""
},
"bot": {
"name": "fangmuke_bot"
}
}
9,339 changes: 6,970 additions & 2,369 deletions package-lock.json

Large diffs are not rendered by default.

21 changes: 17 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
"name": "wechat-bot",
"version": "1.0.0",
"description": "微信机器人",
"main": "src/main.ts",
"main": "index.js",
"engines": {
"node": "12"
},
"scripts": {
"start": "ts-node src/main.ts",
"dev": "ts-node src/main.ts",
"build": "tsc",
"start": "node dist/main.js",
"test": "echo Hello WeChat Bot!"
},
"keywords": [
Expand All @@ -17,8 +19,19 @@
"author": "fangmuke",
"license": "MIT",
"dependencies": {
"wechaty": "^0.47.24",
"@types/config": "^0.0.36",
"@types/lodash": "^4.14.165",
"config": "^3.3.2",
"lodash": "^4.17.20",
"wechaty": "^0.48.10",
"wechaty-plugin-contrib": "^0.14.15",
"wechaty-puppet-padplus": "^0.7.40"
"wechaty-puppet": "^0.32.3",
"wechaty-puppet-padlocal": "^0.1.3"
},
"devDependencies": {
"@chatie/tsconfig": "^0.13.0",
"ts-loader": "^8.0.11",
"ts-node": "^9.0.0",
"typescript": "^4.0.3"
}
}
9 changes: 9 additions & 0 deletions src/bot/handlers/on-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { log, Wechaty } from 'wechaty';

async function onError(this: Wechaty, e: Error): Promise<void> {
log.error('on-error', 'onError(%s).', e);
console.error(e);
console.error(e.stack);
}

export default onError;
7 changes: 7 additions & 0 deletions src/bot/handlers/on-friendship.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { log, Friendship, Wechaty } from 'wechaty';

async function onFriendship(this: Wechaty, friendship: Friendship): Promise<void> {
log.info('on-friendship', 'onFriendship(%s).', friendship);
}

export default onFriendship;
9 changes: 9 additions & 0 deletions src/bot/handlers/on-login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Contact, log, VERSION, Wechaty } from 'wechaty';

async function onLogin(this: Wechaty, user: Contact): Promise<void> {
const msg = `${user.name()} Wechaty@${VERSION} logged in`;
log.info('on-login', 'onLogin(%s) %s.', user, msg);
await user.say(msg);
}

export default onLogin;
7 changes: 7 additions & 0 deletions src/bot/handlers/on-logout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Contact, log, Wechaty } from 'wechaty';

async function onLogout(this: Wechaty, user: Contact): Promise<void> {
log.info('on-logout', 'onLogout(%s).', user);
}

export default onLogout;
7 changes: 7 additions & 0 deletions src/bot/handlers/on-message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { log, Message, Wechaty } from 'wechaty';

async function onMessage(this: Wechaty, message: Message): Promise<void> {
log.info('on-message', 'onMessage(%s).', message);
}

export default onMessage;
7 changes: 7 additions & 0 deletions src/bot/handlers/on-room-invite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { log, RoomInvitation, Wechaty } from 'wechaty';

async function onRoomInvite(this: Wechaty, roomInvitation: RoomInvitation): Promise<void> {
log.info('on-room-invite', 'onRoomInvite(%s).', roomInvitation);
}

export default onRoomInvite;
7 changes: 7 additions & 0 deletions src/bot/handlers/on-room-join.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Contact, log, Room, Wechaty } from 'wechaty';

async function onRoomJoin(this: Wechaty, room: Room, inviteeList: Contact[], inviter: Contact): Promise<void> {
log.info('on-room-join', 'onRoomJoin(%s, %s, %s).', room, inviteeList.join(','), inviter);
}

export default onRoomJoin;
7 changes: 7 additions & 0 deletions src/bot/handlers/on-room-leave.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Contact, log, Room, Wechaty } from 'wechaty';

async function onRoomLeave(this: Wechaty, room: Room, leaverList: Contact[], remover?: Contact): Promise<void> {
log.info('on-room-leave', 'onRoomLeave(%s, %s, %s).', room, leaverList.join(','), remover);
}

export default onRoomLeave;
7 changes: 7 additions & 0 deletions src/bot/handlers/on-room-topic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Contact, log, Room, Wechaty } from 'wechaty';

async function onRoomTopic(this: Wechaty, room: Room, newTopic: string, oldTopic: string, changer: Contact): Promise<void> {
log.info('on-room-topic', 'onRoomTopic(%s, %s, %s, %s).', room, newTopic, oldTopic, changer);
}

export default onRoomTopic;
7 changes: 7 additions & 0 deletions src/bot/handlers/on-scan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { log, Wechaty } from 'wechaty';

async function onScan(this: Wechaty, qrcode: string, status: number): Promise<void> {
log.info('on-scan', 'onScan() [%s] %s\nScan QR Code above to log in.', status, qrcode);
}

export default onScan;
32 changes: 11 additions & 21 deletions src/bot/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
import {Wechaty, log,} from 'wechaty'
import { Wechaty, WechatyOptions } from 'wechaty';
import { pluginList } from './plugins';

import {pluginList} from './plugins'
let wechaty: undefined | Wechaty;

let index: undefined | Wechaty
function getBot(options?: WechatyOptions): Wechaty {
if (wechaty == undefined) {
wechaty = new Wechaty(options);

function getBot(name: string): Wechaty {
log.verbose('getWechaty', 'getFriday(%s)', name)

const wechaty = new Wechaty({
name,
})

void pluginList

/**
* Initialize Plugins
*/
wechaty.use(
...pluginList,
)

index = wechaty
...pluginList,
);
}

return wechaty
return wechaty;
}

export {getBot}
export { getBot };
24 changes: 24 additions & 0 deletions src/bot/plugins/contact-me.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { FileBox, Message, Wechaty, WechatyPlugin } from 'wechaty';
import { includes } from 'lodash';

export function ContactPlugin(): WechatyPlugin {
return (wechaty: Wechaty) => {
wechaty.on('message', async (message: Message) => {
const type = message.type();
const text = message.text();

if (type === Message.Type.Text && message.room() == null) {
if (includes(text, '房木可')) {
const contactCard = await wechaty.Contact.find({id: 'wxid_vdha0qorca4e22'});
if (contactCard) {
await message.say(contactCard);
}
}
if (includes(text, 'Bot')) {
const fileBox = FileBox.fromQRCode('https://u.wechat.com/EAXXlC8l77ZK-2uCbLdYoKk');
await message.say(fileBox);
}
}
});
};
}
18 changes: 18 additions & 0 deletions src/bot/plugins/event-hot-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { EventHotHandler, EventHotHandlerConfig } from 'wechaty-plugin-contrib';

const config: EventHotHandlerConfig = {
scan: '../handlers/on-scan',
friendship: '../handlers/on-friendship',
login: '../handlers/on-login',
logout: '../handlers/on-logout',
message: '../handlers/on-message',
error: '../handlers/on-error',
'room-invite': '../handlers/on-room-invite',
'room-join': '../handlers/on-room-join',
'room-leave': '../handlers/on-room-leave',
'room-topic': '../handlers/on-room-topic',
};

const EventHotHandlerPlugin = EventHotHandler(config);

export { EventHotHandlerPlugin };
18 changes: 18 additions & 0 deletions src/bot/plugins/friendship-accepter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { FriendshipAccepter, FriendshipAccepterConfig } from 'wechaty-plugin-contrib';
import { get } from 'config';

const NEW_FRIEND_GREETING = [
`Hi, 我的名字是${get('bot.name')}, 我是房木可基于Wechaty开发的WeChat-Bot。
GitHub:https://github.com/fangmuke/wechat-bot`,
'如果你对WeChat-Bot感兴趣,只需要向我发送"房木可",我会将他的名片发送给你。',
'如果需要我的二维码名片,只需要向我发送"Bot",我会将我的二维码名片发送给你。',
'祝你玩的开心!',
];

const config: FriendshipAccepterConfig = {
greeting: NEW_FRIEND_GREETING,
};

const FriendshipAccepterPlugin = FriendshipAccepter(config);

export { FriendshipAccepterPlugin };
26 changes: 17 additions & 9 deletions src/bot/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import {
QRCodeTerminal,
EventLogger,
DingDong,
} from 'wechaty-plugin-contrib'
QRCodeTerminal,
EventLogger,
} from 'wechaty-plugin-contrib';

import { EventHotHandlerPlugin } from './event-hot-handler';
import { FriendshipAccepterPlugin } from './friendship-accepter';
import * as roomInviterPlugin from './room-inviter';
import { ContactPlugin } from './contact-me';


const pluginList = [
QRCodeTerminal(),
EventLogger(),
DingDong(),
]
QRCodeTerminal(),
EventLogger(),
ContactPlugin(),
EventHotHandlerPlugin,
FriendshipAccepterPlugin,
...Object.values(roomInviterPlugin),
];

export {pluginList}
export { pluginList };
31 changes: 31 additions & 0 deletions src/bot/plugins/room-inviter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Contact, Room } from 'wechaty';
import { RoomInviter, RoomInviterConfig, talkers } from 'wechaty-plugin-contrib';


const repeat: talkers.ContactTalkerOptions = async (contact: Contact, room?: Room) => {
await contact.say(`你已经加入[${await room?.topic()}]。`);
};

const listRoomInviterConfig: RoomInviterConfig[] = [
{
password: [
/^bot-test$/i,
],
room: /^Bot Test$/i,
rule: [
'Hi,我想邀请你加入Bot Test群组!',
],
repeat,
welcome: [
'欢迎加入Bot Test群组!',
],
},
];

let roomInviter = [];

for (const roomInviterConfig of listRoomInviterConfig) {
roomInviter.push(RoomInviter(roomInviterConfig));
}

export { roomInviter };
34 changes: 24 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
import {log} from "wechaty";
import {getBot} from "./bot";
import { get } from 'config';
import { getBot } from './bot';
import PuppetPadlocal from 'wechaty-puppet-padlocal';
import { WechatyOptions } from 'wechaty';

async function main() {
log.verbose('main', 'main()')

const bot = getBot('fangmuke_bot')
const name: string = get('bot.name');
const host: string = get('padLocal.host');
const port: number = get('padLocal.port');
const token: string = get('padLocal.token');
const serverCAFilePath: string = get('padLocal.serverCAFilePath');

await bot.start()
const puppet = new PuppetPadlocal({
endpoint: `${host}:${port}`,
token,
serverCAFilePath,
});

const options: WechatyOptions = {name, puppet};

const bot = getBot(options);

await bot.start();
}

main()
.catch((e) => {
log.error('Main', 'main() rejection: %s', e)
console.error(e)
process.exit(1)
})
.catch((e) => {
console.error(e);
process.exit(1);
});
Loading

0 comments on commit 2f51d7d

Please sign in to comment.