- Contact
All wechat contacts(friend) will be encapsulated as a Contact.
Contact
isSayable
, Example/Contact-Bot- FriendRequest
Send, receive friend request, and friend confirmation events.
- send request
- receive request(in friend event)
- confirmation friendship(friend event)
- Message
All wechat messages will be encapsulated as a Message.
Message
isSayable
, Example/Ding-Dong-Bot- MediaMessage
Meidia Type Message
- Room
All wechat rooms(groups) will be encapsulated as a Room.
Room
isSayable
, Example/Room-Bot- Wechaty
Main bot class.
- FriendRequest
- ContactQueryFilter
The way to search Contact
- RoomEventName
Room Class Event Type
- RoomEventFunction
Room Class Event Function
- MemberQueryFilter
The way to search member by Room.member()
- WechatyEventName
Wechaty Class Event Type
- WechatyEventFunction
Wechaty Class Event Function
All wechat contacts(friend) will be encapsulated as a Contact.
Contact
is Sayable
,
Example/Contact-Bot
Kind: global class
- Contact
- instance
- .say(textOrMedia) ⇒
Promise.<boolean>
- .name() ⇒
string
- .alias(newAlias) ⇒
string
|null
|Promise.<boolean>
- .stranger() ⇒
boolean
|null
- .official() ⇒
boolean
|null
- .special() ⇒
boolean
|null
- .personal() ⇒
boolean
|null
- .star() ⇒
boolean
- .gender() ⇒
Gender.Male(2)
|Gender.Female(1)
|Gender.Unknown(0)
- .province() ⇒
string
|undefined
- .city() ⇒
string
|undefined
- .avatar() ⇒
Promise.<NodeJS.ReadableStream>
- .refresh() ⇒
Promise.<this>
- .self() ⇒
boolean
- .say(textOrMedia) ⇒
- static
- .find(query) ⇒
Promise.<(Contact|null)>
- .findAll([queryArg]) ⇒
Promise.<Array.<Contact>>
- .find(query) ⇒
- instance
Send Text or Media File to Contact.
Kind: instance method of Contact
Param | Type |
---|---|
textOrMedia | string | MediaMessage |
Example
const contact = await Contact.find({name: 'lijiarui'}) // change 'lijiarui' to any of your contact name in wechat
await contact.say('welcome to wechaty!')
await contact.say(new MediaMessage(__dirname + '/wechaty.png') // put the filePath you want to send here
Get the name from a contact
Kind: instance method of Contact
Example
const name = contact.name()
GET / SET / DELETE the alias for a contact
Tests show it will failed if set alias too frequently(60 times in one minute).
Kind: instance method of Contact
Param | Type |
---|---|
newAlias | none | string | null |
Example ( GET the alias for a contact, return {(string | null)})
const alias = contact.alias()
if (alias === null) {
console.log('You have not yet set any alias for contact ' + contact.name())
} else {
console.log('You have already set an alias for contact ' + contact.name() + ':' + alias)
}
Example (SET the alias for a contact)
const ret = await contact.alias('lijiarui')
if (ret) {
console.log(`change ${contact.name()}'s alias successfully!`)
} else {
console.log(`failed to change ${contact.name()} alias!`)
}
Example (DELETE the alias for a contact)
const ret = await contact.alias(null)
if (ret) {
console.log(`delete ${contact.name()}'s alias successfully!`)
} else {
console.log(`failed to delete ${contact.name()}'s alias!`)
}
Check if contact is stranger
Kind: instance method of Contact
Returns: boolean
| null
- - True for not friend of the bot, False for friend of the bot, null for unknown.
Example
const isStranger = contact.stranger()
Check if it's a offical account
Kind: instance method of Contact
Returns: boolean
| null
- - True for official account, Flase for contact is not a official account, null for unknown
See
Example
const isOfficial = contact.official()
Check if it's a special contact
The contact who's id in following list will be identify as a special contact
weibo
, qqmail
, fmessage
, tmessage
, qmessage
, qqsync
, floatbottle
,
lbsapp
, shakeapp
, medianote
, qqfriend
, readerapp
, blogapp
, facebookapp
,
masssendapp
, meishiapp
, feedsapp
, voip
, blogappweixin
, weixin
, brandsessionholder
,
weixinreminder
, wxid_novlwrv3lqwv11
, gh_22b87fa7cb3c
, officialaccounts
, notification_messages
,
Kind: instance method of Contact
Returns: boolean
| null
- True for brand, Flase for contact is not a brand
See
Example
const isSpecial = contact.special()
Check if it's a personal account
Kind: instance method of Contact
Returns: boolean
| null
- - True for personal account, Flase for contact is not a personal account
Example
const isPersonal = contact.personal()
Check if the contact is star contact.
Kind: instance method of Contact
Returns: boolean
- - True for star friend, False for no star friend.
Example
const isStar = contact.star()
Contact gender
Kind: instance method of Contact
Example
const gender = contact.gender()
Get the region 'province' from a contact
Kind: instance method of Contact
Example
const province = contact.province()
Get the region 'city' from a contact
Kind: instance method of Contact
Example
const city = contact.city()
Get avatar picture file stream
Kind: instance method of Contact
Example
const avatarFileName = contact.name() + `.jpg`
const avatarReadStream = await contact.avatar()
const avatarWriteStream = createWriteStream(avatarFileName)
avatarReadStream.pipe(avatarWriteStream)
log.info('Bot', 'Contact: %s: %s with avatar file: %s', contact.weixin(), contact.name(), avatarFileName)
Force reload data for Contact
Kind: instance method of Contact
Example
await contact.refresh()
Check if contact is self
Kind: instance method of Contact
Returns: boolean
- True for contact is self, False for contact is others
Example
const isSelf = contact.self()
Try to find a contact by filter: {name: string | RegExp} / {alias: string | RegExp}
Find contact by name or alias, if the result more than one, return the first one.
Kind: static method of Contact
Returns: Promise.<(Contact|null)>
- If can find the contact, return Contact, or return null
Param | Type |
---|---|
query | ContactQueryFilter |
Example
const contactFindByName = await Contact.find({ name:"ruirui"} )
const contactFindByAlias = await Contact.find({ alias:"lijiarui"} )
Find contact by name
or alias
If use Contact.findAll() get the contact list of the bot.
name
the name-string set by user-self, should be called namealias
the name-string set by bot for others, should be called alias
Kind: static method of Contact
Param | Type |
---|---|
[queryArg] | ContactQueryFilter |
Example
const contactList = await Contact.findAll() // get the contact list of the bot
const contactList = await Contact.findAll({name: 'ruirui'}) // find allof the contacts whose name is 'ruirui'
const contactList = await Contact.findAll({alias: 'lijiarui'}) // find all of the contacts whose alias is 'lijiarui'
Send, receive friend request, and friend confirmation events.
- send request
- receive request(in friend event)
- confirmation friendship(friend event)
Kind: global class
- FriendRequest
- .send(contact, [hello]) ⇒
Promise.<boolean>
- .accept() ⇒
Promise.<boolean>
- .send(contact, [hello]) ⇒
Send a new friend request
Kind: instance method of FriendRequest
Returns: Promise.<boolean>
- Return a Promise, true for accept successful, false for failure.
Param | Type | Default |
---|---|---|
contact | Contact |
|
[hello] | string |
"'Hi'" |
Example
const from = message.from()
const request = new FriendRequest()
request.send(from, 'hello~')
Accept a friend request
Kind: instance method of FriendRequest
Returns: Promise.<boolean>
- Return a Promise, true for accept successful, false for failure.
All wechat messages will be encapsulated as a Message.
Message
is Sayable
,
Example/Ding-Dong-Bot
Kind: global class
- Message
- instance
- .say(textOrMedia, [replyTo]) ⇒
Promise.<any>
- .from() ⇒
Contact
- .room() ⇒
Room
|null
- .content() ⇒
string
- .type() ⇒
MsgType
- .typeSub() ⇒
MsgType
- .typeApp() ⇒
AppMsgType
- .typeEx() ⇒
MsgType
- .self() ⇒
boolean
- .mentioned() ⇒
Array.<Contact>
- .to() ⇒
Contact
|null
- .readyStream() ⇒
Promise.<Readable>
- .say(textOrMedia, [replyTo]) ⇒
- static
- instance
Reply a Text or Media File message to the sender.
Kind: instance method of Message
See: Example/ding-dong-bot
Param | Type |
---|---|
textOrMedia | string | MediaMessage |
[replyTo] | Contact | Array.<Contact> |
Example
const bot = Wechaty.instance()
bot
.on('message', async m => {
if (/^ding$/i.test(m.content())) {
await m.say('hello world')
console.log('Bot REPLY: hello world')
await m.say(new MediaMessage(__dirname + '/wechaty.png'))
console.log('Bot REPLY: Image')
}
})
message.from() ⇒ Contact
Get the sender from a message.
Kind: instance method of Message
message.room() ⇒ Room
| null
Get the room from the message.
If the message is not in a room, then will return null
Kind: instance method of Message
Get the content of the message
Kind: instance method of Message
Get the type from the message.
If type is equal to MsgType.RECALLED
, Message#id is the msgId of the recalled message.
Kind: instance method of Message
See: MsgType
Get the typeSub from the message.
If message is a location message: m.type() === MsgType.TEXT && m.typeSub() === MsgType.LOCATION
Kind: instance method of Message
See: MsgType
Get the typeApp from the message.
Kind: instance method of Message
See: AppMsgType
Get the typeEx from the message.
Kind: instance method of Message
Check if a message is sent by self.
Kind: instance method of Message
Returns: boolean
- - Return true
for send from self, false
for send from others.
Example
if (message.self()) {
console.log('this message is sent by myself!')
}
message.mentioned() ⇒ Array.<Contact>
Get message mentioned contactList.
Message event table as follows
Web | Mac PC Client | iOS Mobile | android Mobile | |
---|---|---|---|---|
[You were mentioned] tip ([有人@我]的提示) | ✘ | √ | √ | √ |
Identify magic code (8197) by copy & paste in mobile | ✘ | √ | √ | ✘ |
Identify magic code (8197) by programming | ✘ | ✘ | ✘ | ✘ |
Identify two contacts with the same roomAlias by [You were mentioned] tip | ✘ | ✘ | √ | √ |
Kind: instance method of Message
Returns: Array.<Contact>
- - Return message mentioned contactList
Example
const contactList = message.mentioned()
console.log(contactList)
message.to() ⇒ Contact
| null
Get the destination of the message Message.to() will return null if a message is in a room, use Message.room() to get the room.
Kind: instance method of Message
Please notice that when we are running Wechaty, if you use the browser that controlled by Wechaty to send attachment files, you will get a zero sized file, because it is not an attachment from the network, but a local data, which is not supported by Wechaty yet.
Kind: instance method of Message
Kind: static method of Message
Todo
- add function
Kind: static method of Message
Todo
- add function
Meidia Type Message
Kind: global class
- MediaMessage
- .ext() ⇒
string
- .filename() ⇒
string
- .readyStream()
- .saveFile(filePath)
- .forward(to) ⇒
Promise.<boolean>
- .ext() ⇒
Get the MediaMessage file extension, etc: jpg
, gif
, pdf
, word
..
Kind: instance method of MediaMessage
Example
bot.on('message', async function (m) {
if (m instanceof MediaMessage) {
console.log('media message file name extention is: ' + m.ext())
}
})
Get the MediaMessage filename, etc: how to build a chatbot.pdf
..
Kind: instance method of MediaMessage
Example
bot.on('message', async function (m) {
if (m instanceof MediaMessage) {
console.log('media message file name is: ' + m.filename())
}
})
Get the read stream for attachment file
Kind: instance method of MediaMessage
save file
Kind: instance method of MediaMessage
Param | Description |
---|---|
filePath | save file |
Forward the received message.
The types of messages that can be forwarded are as follows:
The return value of type matches one of the following types:
MsgType {
TEXT = 1,
IMAGE = 3,
VIDEO = 43,
EMOTICON = 47,
LOCATION = 48,
APP = 49,
MICROVIDEO = 62,
}
When the return value of type is MsgType.APP
, the return value of typeApp matches one of the following types:
AppMsgType {
TEXT = 1,
IMG = 2,
VIDEO = 4,
ATTACH = 6,
EMOJI = 8,
}
It should be noted that when forwarding ATTACH type message, if the file size is greater than 25Mb, the forwarding will fail. The reason is that the server shields the web wx to download more than 25Mb files with a file size of 0.
But if the file is uploaded by you using wechaty, you can forward it. You need to detect the following conditions in the message event, which can be forwarded if it is met.
.on('message', async m => {
if (m.self() && m.rawObj && m.rawObj.Signature) {
// Filter the contacts you have forwarded
const msg = <MediaMessage> m
await msg.forward()
}
})
Kind: instance method of MediaMessage
Param | Type | Description |
---|---|---|
to | Sayable | Array.<Sayable> |
Room or Contact The recipient of the message, the room, or the contact |
All wechat rooms(groups) will be encapsulated as a Room.
Room
is Sayable
,
Example/Room-Bot
Kind: global class
- Room
- instance
- .say(textOrMedia, [replyTo]) ⇒
Promise.<boolean>
- .on(event, listener) ⇒
this
- .add(contact) ⇒
Promise.<number>
- .del(contact) ⇒
Promise.<number>
- .topic([newTopic]) ⇒
string
|void
- .alias(contact) ⇒
string
|null
- .roomAlias(contact) ⇒
string
|null
- .has(contact) ⇒
boolean
- .memberAll(queryArg) ⇒
Array.<Contact>
- .member(queryArg) ⇒
Contact
|null
- .memberList() ⇒
Array.<Contact>
- .refresh() ⇒
Promise.<void>
- .say(textOrMedia, [replyTo]) ⇒
- static
- .create(contactList, [topic]) ⇒
Promise.<Room>
- .findAll([query]) ⇒
Promise.<Array.<Room>>
- .find(query) ⇒
Promise.<(Room|null)>
- .create(contactList, [topic]) ⇒
- instance
Send message inside Room, if set [replyTo], wechaty will mention the contact as well.
Kind: instance method of Room
Returns: Promise.<boolean>
- If bot send message successfully, it will return true. If the bot failed to send for blocking or any other reason, it will return false
Param | Type | Description |
---|---|---|
textOrMedia | string | MediaMessage |
Send text or media file inside Room. |
[replyTo] | Contact | Array.<Contact> |
Optional parameter, send content inside Room, and mention @replyTo contact or contactList. |
Example (Send text inside Room)
const room = await Room.find({name: 'wechaty'}) // change 'wechaty' to any of your room in wechat
await room.say('Hello world!')
Example (Send media file inside Room)
const room = await Room.find({name: 'wechaty'}) // change 'wechaty' to any of your room in wechat
await room.say(new MediaMessage('/test.jpg')) // put the filePath you want to send here
Example (Send text inside Room, and mention @replyTo contact)
const contact = await Contact.find({name: 'lijiarui'}) // change 'lijiarui' to any of the room member
const room = await Room.find({name: 'wechaty'}) // change 'wechaty' to any of your room in wechat
await room.say('Hello world!', contact)
Kind: instance method of Room
Returns: this
- - this for chain
Param | Type | Description |
---|---|---|
event | RoomEventName |
Emit WechatyEvent |
listener | RoomEventFunction |
Depends on the WechatyEvent |
Example (Event:join )
const room = await Room.find({topic: 'event-room'}) // change `event-room` to any room topic in your wechat
if (room) {
room.on('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:leave )
const room = await Room.find({topic: 'event-room'}) // change `event-room` to any room topic in your wechat
if (room) {
room.on('leave', (room: Room, leaverList: Contact[]) => {
const nameList = leaverList.map(c => c.name()).join(',')
console.log(`Room ${room.topic()} lost member ${nameList}`)
})
}
Example (Event:topic )
const room = await Room.find({topic: 'event-room'}) // change `event-room` to any room topic in your wechat
if (room) {
room.on('topic', (room: Room, topic: string, oldTopic: string, changer: Contact) => {
console.log(`Room ${room.topic()} topic changed from ${oldTopic} to ${topic} by ${changer.name()}`)
})
}
Add contact in a room
Kind: instance method of Room
Param | Type |
---|---|
contact | Contact |
Example
const contact = await Contact.find({name: 'lijiarui'}) // change 'lijiarui' to any contact in your wechat
const room = await Room.find({topic: 'wechat'}) // change 'wechat' to any room topic in your wechat
if (room) {
const result = await room.add(contact)
if (result) {
console.log(`add ${contact.name()} to ${room.topic()} successfully! `)
} else{
console.log(`failed to add ${contact.name()} to ${room.topic()}! `)
}
}
Delete a contact from the room It works only when the bot is the owner of the room
Kind: instance method of Room
Param | Type |
---|---|
contact | Contact |
Example
const room = await Room.find({topic: 'wechat'}) // change 'wechat' to any room topic in your wechat
const contact = await Contact.find({name: 'lijiarui'}) // change 'lijiarui' to any room member in the room you just set
if (room) {
const result = await room.del(contact)
if (result) {
console.log(`remove ${contact.name()} from ${room.topic()} successfully! `)
} else{
console.log(`failed to remove ${contact.name()} from ${room.topic()}! `)
}
}
SET/GET topic from the room
Kind: instance method of Room
Param | Type | Description |
---|---|---|
[newTopic] | string |
If set this para, it will change room topic. |
Example (When you say anything in a room, it will get room topic. )
const bot = Wechaty.instance()
bot
.on('message', async m => {
const room = m.room()
if (room) {
const topic = room.topic()
console.log(`room topic is : ${topic}`)
}
})
Example (When you say anything in a room, it will change room topic. )
const bot = Wechaty.instance()
bot
.on('message', async m => {
const room = m.room()
if (room) {
const oldTopic = room.topic()
room.topic('change topic to wechaty!')
console.log(`room topic change from ${oldTopic} to ${room.topic()}`)
}
})
Return contact's roomAlias in the room, the same as roomAlias
Kind: instance method of Room
Returns: string
| null
- - If a contact has an alias in room, return string, otherwise return null
Param | Type |
---|---|
contact | Contact |
Example
const bot = Wechaty.instance()
bot
.on('message', async m => {
const room = m.room()
const contact = m.from()
if (room) {
const alias = room.alias(contact)
console.log(`${contact.name()} alias is ${alias}`)
}
})
Same as function alias
Kind: instance method of Room
Param | Type |
---|---|
contact | Contact |
Check if the room has member contact
.
Kind: instance method of Room
Returns: boolean
- Return true
if has contact, else return false
.
Param | Type |
---|---|
contact | Contact |
Example (Check whether 'lijiarui' is in the room 'wechaty')
const contact = await Contact.find({name: 'lijiarui'}) // change 'lijiarui' to any of contact in your wechat
const room = await Room.find({topic: 'wechaty'}) // change 'wechaty' to any of the room in your wechat
if (contact && room) {
if (room.has(contact)) {
console.log(`${contact.name()} is in the room ${room.topic()}!`)
} else {
console.log(`${contact.name()} is not in the room ${room.topic()} !`)
}
}
room.memberAll(queryArg) ⇒ Array.<Contact>
Find all contacts in a room
name
the name-string set by user-self, should be called name, equal toContact.name()
roomAlias
|alias
the name-string set by user-self in the room, should be called roomAliascontactAlias
the name-string set by bot for others, should be called alias, equal toContact.alias()
Kind: instance method of Room
Param | Type | Description |
---|---|---|
queryArg | MemberQueryFilter | string |
When use memberAll(name:string), return all matched members, including name, roomAlias, contactAlias |
room.member(queryArg) ⇒ Contact
| null
Find all contacts in a room, if get many, return the first one.
Kind: instance method of Room
Param | Type | Description |
---|---|---|
queryArg | MemberQueryFilter | string |
When use member(name:string), return all matched members, including name, roomAlias, contactAlias |
Example (Find member by name)
const room = await Room.find({topic: 'wechaty'}) // change 'wechaty' to any room name in your wechat
if (room) {
const member = room.member('lijiarui') // change 'lijiarui' to any room member in your wechat
if (member) {
console.log(`${room.topic()} got the member: ${member.name()}`)
} else {
console.log(`cannot get member in room: ${room.topic()}`)
}
}
Example (Find member by MemberQueryFilter)
const room = await Room.find({topic: 'wechaty'}) // change 'wechaty' to any room name in your wechat
if (room) {
const member = room.member({name: 'lijiarui'}) // change 'lijiarui' to any room member in your wechat
if (member) {
console.log(`${room.topic()} got the member: ${member.name()}`)
} else {
console.log(`cannot get member in room: ${room.topic()}`)
}
}
room.memberList() ⇒ Array.<Contact>
Get all room member from the room
Kind: instance method of Room
Force reload data for Room
Kind: instance method of Room
Room.create(contactList, [topic]) ⇒ Promise.<Room>
Create a new room.
Kind: static method of Room
Param | Type |
---|---|
contactList | Array.<Contact> |
[topic] | string |
Example (Creat a room with 'lijiarui' and 'juxiaomi', the room topic is 'ding - created')
const helperContactA = await Contact.find({ name: 'lijiarui' }) // change 'lijiarui' to any contact in your wechat
const helperContactB = await Contact.find({ name: 'juxiaomi' }) // change 'juxiaomi' to any contact in your wechat
const contactList = [helperContactA, helperContactB]
console.log('Bot', 'contactList: %s', contactList.join(','))
const room = await Room.create(contactList, 'ding')
console.log('Bot', 'createDingRoom() new ding room created: %s', room)
await room.topic('ding - created')
await room.say('ding - created')
Find room by topic, return all the matched room
Kind: static method of Room
Param | Type |
---|---|
[query] | RoomQueryFilter |
Example
const roomList = await Room.findAll() // get the room list of the bot
const roomList = await Room.findAll({name: 'wechaty'}) // find all of the rooms with name 'wechaty'
Try to find a room by filter: {topic: string | RegExp}. If get many, return the first one.
Kind: static method of Room
Returns: Promise.<(Room|null)>
- If can find the room, return Room, or return null
Param | Type |
---|---|
query | RoomQueryFilter |
Main bot class.
The World's Shortest ChatBot Code: 6 lines of JavaScript
Kind: global class
- Wechaty
- instance
- .init() ⇒
Promise.<void>
- .on(event, listener) ⇒
Wechaty
- .quit() ⇒
Promise.<void>
- .logout() ⇒
Promise.<void>
- .self() ⇒
Contact
- .say(content) ⇒
Promise.<boolean>
- .init() ⇒
- static
- .instance()
- .version([forceNpm]) ⇒
string
- instance
Initialize the bot, return Promise.
Kind: instance method of Wechaty
Example
await bot.init()
// do other stuff with bot here
wechaty.on(event, listener) ⇒ Wechaty
Kind: instance method of Wechaty
Returns: Wechaty
- - this for chain
More Example Gist: Example/Friend-Bot
Param | Type | Description |
---|---|---|
event | WechatyEvent |
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()}`)
})
Quit the bot
Kind: instance method of Wechaty
Example
await bot.quit()
Logout the bot
Kind: instance method of Wechaty
Example
await bot.logout()
wechaty.self() ⇒ Contact
Get current user
Kind: instance method of Wechaty
Example
const contact = bot.self()
console.log(`Bot is ${contact.name()}`)
Send message to filehelper
Kind: instance method of Wechaty
Param | Type |
---|---|
content | 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()
Return version of Wechaty
Kind: static 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'
Kind: global class
- FriendRequest
- .send(contact, [hello]) ⇒
Promise.<boolean>
- .accept() ⇒
Promise.<boolean>
- .send(contact, [hello]) ⇒
Send a new friend request
Kind: instance method of FriendRequest
Returns: Promise.<boolean>
- Return a Promise, true for accept successful, false for failure.
Param | Type | Default |
---|---|---|
contact | Contact |
|
[hello] | string |
"'Hi'" |
Example
const from = message.from()
const request = new FriendRequest()
request.send(from, 'hello~')
Accept a friend request
Kind: instance method of FriendRequest
Returns: Promise.<boolean>
- Return a Promise, true for accept successful, false for failure.
Enum for Gender values.
Kind: global enum
Properties
Name | Type | Description |
---|---|---|
Unknown | number |
0 for Unknown |
Male | number |
1 for Male |
Female | number |
2 for Female |
The way to search Contact
Kind: global typedef
Properties
Name | Type | Description |
---|---|---|
name | string |
The name-string set by user-self, should be called name |
alias | string |
The name-string set by bot for others, should be called alias More Detail |
Room Class Event Type
Kind: global typedef
Properties
Name | Type | Description |
---|---|---|
join | string |
Emit when anyone join any room. |
topic | string |
Get topic event, emitted when someone change room topic. |
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. |
Room Class Event Function
Kind: global typedef
Properties
Name | Type | Description |
---|---|---|
room-join | function |
(this: Room, inviteeList: Contact[] , inviter: Contact) => void |
room-topic | function |
(this: Room, topic: string, oldTopic: string, changer: Contact) => void |
room-leave | function |
(this: Room, leaver: Contact) => void |
The way to search member by Room.member()
Kind: global typedef
Properties
Name | Type | Description |
---|---|---|
name | string |
Find the contact by wechat name in a room, equal to Contact.name() . |
alias | string |
Find the contact by alias set by the bot for others in a room, equal to roomAlias . |
roomAlias | string |
Find the contact by alias set by the bot for others in a room. |
contactAlias | string |
Find the contact by alias set by the contact out of a room, equal to Contact.alias() . More Detail |
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. |
scan | string |
A scan event will be emitted when the bot needs to show you a QR Code for scanning. |
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. |
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 |