- Wechaty
Main bot class.
- WechatyEventName
Wechaty Class Event Type
- WechatyEventFunction
Wechaty Class Event Function
Main bot class.
The World's Shortest ChatBot Code: 6 lines of JavaScript
Kind: global class
- Wechaty
- instance
- .Contact
- .version([forceNpm]) ⇒
string
- .on(event, listener) ⇒
Wechaty
- .start() ⇒
Promise.<void>
- .stop() ⇒
Promise.<void>
- .logout() ⇒
Promise.<void>
- .logonoff() ⇒
boolean
.self()- .userSelf() ⇒
Contact
- .say(text) ⇒
Promise.<boolean>
- static
- instance
Clone Classes for this bot and attach the puppet
to the Class
Fixme: https://stackoverflow.com/questions/36886082/abstract-constructor-type-in-typescript microsoft/TypeScript#5843 (comment) microsoft/TypeScript#19197
Kind: instance property of Wechaty
Return version of Wechaty
Kind: instance method of Wechaty
Returns: string
- - the version number
Param | Type | Default | Description |
---|---|---|---|
[forceNpm] | boolean |
false |
if set to true, will only return the version in package.json. otherwise will return git commit hash if .git exists. |
Example
console.log(Wechaty.instance().version()) // return '#git[af39df]'
console.log(Wechaty.instance().version(true)) // return '0.7.9'
wechaty.on(event, listener) ⇒ Wechaty
Kind: instance method of Wechaty
Returns: Wechaty
- - this for chain
More Example Gist: Examples/Friend-Bot
Param | Type | Description |
---|---|---|
event | WechatyEventName |
Emit WechatyEvent |
listener | WechatyEventFunction |
Depends on the WechatyEvent |
Example (Event:scan )
wechaty.on('scan', (url: string, code: number) => {
console.log(`[${code}] Scan ${url} to login.` )
})
Example (Event:login )
bot.on('login', (user: Contact) => {
console.log(`user ${user} login`)
})
Example (Event:logout )
bot.on('logout', (user: Contact) => {
console.log(`user ${user} logout`)
})
Example (Event:message )
wechaty.on('message', (message: Message) => {
console.log(`message ${message} received`)
})
Example (Event:friend )
bot.on('friend', (contact: Contact, request: FriendRequest) => {
if(request){ // 1. request to be friend from new contact
let result = await request.accept()
if(result){
console.log(`Request from ${contact.name()} is accept succesfully!`)
} else{
console.log(`Request from ${contact.name()} failed to accept!`)
}
} else { // 2. confirm friend ship
console.log(`new friendship confirmed with ${contact.name()}`)
}
})
Example (Event:room-join )
bot.on('room-join', (room: Room, inviteeList: Contact[], inviter: Contact) => {
const nameList = inviteeList.map(c => c.name()).join(',')
console.log(`Room ${room.topic()} got new member ${nameList}, invited by ${inviter}`)
})
Example (Event:room-leave )
bot.on('room-leave', (room: Room, leaverList: Contact[]) => {
const nameList = leaverList.map(c => c.name()).join(',')
console.log(`Room ${room.topic()} lost member ${nameList}`)
})
Example (Event:room-topic )
bot.on('room-topic', (room: Room, topic: string, oldTopic: string, changer: Contact) => {
console.log(`Room ${room.topic()} topic changed from ${oldTopic} to ${topic} by ${changer.name()}`)
})
Start the bot, return Promise.
Kind: instance method of Wechaty
Example
await bot.start()
// do other stuff with bot here
Stop the bot
Kind: instance method of Wechaty
Example
await bot.stop()
Logout the bot
Kind: instance method of Wechaty
Example
await bot.logout()
Get the logon / logoff state
Kind: instance method of Wechaty
Example
if (bot.logonoff()) {
console.log('Bot logined')
} else {
console.log('Bot not logined')
}
Deprecated
Kind: instance method of Wechaty
Get current user
Kind: instance method of Wechaty
Example
const contact = bot.userSelf()
console.log(`Bot is ${contact.name()}`)
Send message to filehelper
Kind: instance method of Wechaty
Param | Type |
---|---|
text | string |
get the singleton instance of Wechaty
Kind: static method of Wechaty
Example (The World's Shortest ChatBot Code: 6 lines of JavaScript)
const { Wechaty } = require('wechaty')
Wechaty.instance() // Singleton
.on('scan', (url, code) => console.log(`Scan QR Code to login: ${code}\n${url}`))
.on('login', user => console.log(`User ${user} logined`))
.on('message', message => console.log(`Message: ${message}`))
.init()
Enum for AppMsgType values.
Kind: global enum
Properties
Name | Type | Description |
---|---|---|
TEXT | number |
AppMsgType.TEXT (1) for TEXT |
IMG | number |
AppMsgType.IMG (2) for IMG |
AUDIO | number |
AppMsgType.AUDIO (3) for AUDIO |
VIDEO | number |
AppMsgType.VIDEO (4) for VIDEO |
URL | number |
AppMsgType.URL (5) for URL |
ATTACH | number |
AppMsgType.ATTACH (6) for ATTACH |
OPEN | number |
AppMsgType.OPEN (7) for OPEN |
EMOJI | number |
AppMsgType.EMOJI (8) for EMOJI |
VOICE_REMIND | number |
AppMsgType.VOICE_REMIND (9) for VOICE_REMIND |
SCAN_GOOD | number |
AppMsgType.SCAN_GOOD (10) for SCAN_GOOD |
GOOD | number |
AppMsgType.GOOD (13) for GOOD |
EMOTION | number |
AppMsgType.EMOTION (15) for EMOTION |
CARD_TICKET | number |
AppMsgType.CARD_TICKET (16) for CARD_TICKET |
REALTIME_SHARE_LOCATION | number |
AppMsgType.REALTIME_SHARE_LOCATION (17) for REALTIME_SHARE_LOCATION |
TRANSFERS | number |
AppMsgType.TRANSFERS (2e3) for TRANSFERS |
RED_ENVELOPES | number |
AppMsgType.RED_ENVELOPES (2001) for RED_ENVELOPES |
READER_TYPE | number |
AppMsgType.READER_TYPE (100001) for READER_TYPE |
Enum for MsgType values.
Kind: global enum
Properties
Name | Type | Description |
---|---|---|
TEXT | number |
MsgType.TEXT (1) for TEXT |
IMAGE | number |
MsgType.IMAGE (3) for IMAGE |
VOICE | number |
MsgType.VOICE (34) for VOICE |
VERIFYMSG | number |
MsgType.VERIFYMSG (37) for VERIFYMSG |
POSSIBLEFRIEND_MSG | number |
MsgType.POSSIBLEFRIEND_MSG (40) for POSSIBLEFRIEND_MSG |
SHARECARD | number |
MsgType.SHARECARD (42) for SHARECARD |
VIDEO | number |
MsgType.VIDEO (43) for VIDEO |
EMOTICON | number |
MsgType.EMOTICON (47) for EMOTICON |
LOCATION | number |
MsgType.LOCATION (48) for LOCATION |
APP | number |
MsgType.APP (49) for APP |
VOIPMSG | number |
MsgType.VOIPMSG (50) for VOIPMSG |
STATUSNOTIFY | number |
MsgType.STATUSNOTIFY (51) for STATUSNOTIFY |
VOIPNOTIFY | number |
MsgType.VOIPNOTIFY (52) for VOIPNOTIFY |
VOIPINVITE | number |
MsgType.VOIPINVITE (53) for VOIPINVITE |
MICROVIDEO | number |
MsgType.MICROVIDEO (62) for MICROVIDEO |
SYSNOTICE | number |
MsgType.SYSNOTICE (9999) for SYSNOTICE |
SYS | number |
MsgType.SYS (10000) for SYS |
RECALLED | number |
MsgType.RECALLED (10002) for RECALLED |
Wechaty Class Event Type
Kind: global typedef
Properties
Name | Type | Description |
---|---|---|
error | string |
When the bot get error, there will be a Wechaty error event fired. |
login | string |
After the bot login full successful, the event login will be emitted, with a Contact of current logined user. |
logout | string |
Logout will be emitted when bot detected log out, with a Contact of the current login user. |
heartbeat | string |
Get bot's heartbeat. |
friend | string |
When someone sends you a friend request, there will be a Wechaty friend event fired. |
message | string |
Emit when there's a new message. |
room-join | string |
Emit when anyone join any room. |
room-topic | string |
Get topic event, emitted when someone change room topic. |
room-leave | string |
Emit when anyone leave the room. If someone leaves the room by themselves, wechat will not notice other people in the room, so the bot will never get the "leave" event. |
scan | string |
A scan event will be emitted when the bot needs to show you a QR Code for scanning. |
Wechaty Class Event Function
Kind: global typedef
Properties
Name | Type | Description |
---|---|---|
error | function |
(this: Wechaty, error: Error) => void callback function |
login | function |
(this: Wechaty, user: Contact)=> void |
logout | function |
(this: Wechaty, user: Contact) => void |
scan | function |
(this: Wechaty, url: string, code: number) => void
|
heartbeat | function |
(this: Wechaty, data: any) => void |
friend | function |
(this: Wechaty, friend: Contact, request?: FriendRequest) => void |
message | function |
(this: Wechaty, message: Message) => void |
room-join | function |
(this: Wechaty, room: Room, inviteeList: Contact[], inviter: Contact) => void |
room-topic | function |
(this: Wechaty, room: Room, topic: string, oldTopic: string, changer: Contact) => void |
room-leave | function |
(this: Wechaty, room: Room, leaverList: Contact[]) => void |