-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
7,261 additions
and
2,487 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
.idea | ||
node_modules | ||
/friday.memory-card.json | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
* | ||
!default.json | ||
!.gitignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
Oops, something went wrong.