提交 7848022b 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

add `implements Sayable`

上级 d3c163df
......@@ -261,12 +261,13 @@ $ export WECHATY_LOG=verbose
Win32:
```shell
```bat
set WECHATY_LOG=verbose
```
Tips: You may want to have more scroll buffer size in your CMD window in windows.
```shell
```bat
mode con lines=32766
```
> http://stackoverflow.com/a/8775884/1123955
......@@ -274,7 +275,7 @@ mode con lines=32766
### NpmLog with Timestamp ###
Here's a quick and dirty patch, to npmlog/log.js
```javascript
```typescript
m.message.split(/\r?\n/).forEach(function (line) {
var date = new Date();
......@@ -362,7 +363,7 @@ wechaty.on('scan', (this: Sayable, url: string, code: number) => {
### 2. Event: `login`
After the bot login full successful, the event `login` will be emitted, with a [Contact](#class-contact) of current logined user.
```javascript
```typescript
wechaty.on('login', (this: Sayable, user: Contact) => {
console.log(`user ${user} login`)
})
......@@ -372,7 +373,7 @@ wechaty.on('login', (this: Sayable, user: Contact) => {
`logout` will be emitted when bot detected it is logout, with a [Contact](#class-contact) of current logined user.
```javascript
```typescript
wechaty.on('logout', (this: Sayable, user: Contact) => {
console.log(`user ${user} logout`)
})
......@@ -382,7 +383,7 @@ wechaty.on('logout', (this: Sayable, user: Contact) => {
Emit when there's a new message.
```javascript
```typescript
wechaty.on('message', (this: Sayable, message: Message) => {
console.log('message ${message} received')
})
......@@ -392,7 +393,7 @@ wechaty.on('message', (this: Sayable, message: Message) => {
Emit when there's a error occoured.
```javascript
```typescript
wechaty.on('error', (this: Sayable, err: Error) => {
console.log('error ${err.message} received')
})
......@@ -404,7 +405,7 @@ The `message` here is a [Message](#class-message).
Fired when we got new friend request, or confirm a friend ship.
```typescript
```ts
wechaty.on('friend', (this: Sayable, contact: Contact, request: FriendRequest) => {
if (request) { // 1. request to be friend from new contact
request.accept()
......@@ -417,7 +418,7 @@ wechaty.on('friend', (this: Sayable, contact: Contact, request: FriendRequest) =
### 7. Event: `room-join`
```typescript
```ts
wechaty.on('room-join', (this: Sayable, room: Room, invitee: Contact, inviter: Contact) => {
console.log(`Room ${room} got new member ${invitee}, invited by ${inviter}`)
})
......@@ -442,7 +443,7 @@ wechaty.on('room-topic', (this: Sayable, room: Room, topic: string, oldTopic: st
## Wechaty Class
Main bot class.
```javascript
```typescript
const bot = Wechaty.instance({
profile
})
......@@ -455,7 +456,7 @@ const bot = Wechaty.instance({
### Wechaty.init(): Wechaty
Initialize the bot, return Promise.
```javascript
```typescript
wechaty.init()
.then(() => {
// do other staff with bot here
......@@ -467,7 +468,7 @@ Check if message is send by self.
Return `true` for send from self, `false` for send from others.
```javascript
```typescript
if (wechaty.self(message)) {
console.log('this message is sent by myself!')
}
......@@ -476,7 +477,7 @@ if (wechaty.self(message)) {
### Wechaty.send(message: Message): Wechaty
send a `message`
```javascript
```typescript
const msg = new Message()
msg.to('filehelper')
msg.content('hello')
......@@ -484,15 +485,6 @@ msg.content('hello')
wechaty.send(msg)
```
### @deprecated Wechaty.reply(message: Message, reply: String): Wechaty
Reply a `message` with `reply`.
That means: the `to` field of the reply message is the `from` of origin message.
```javascript
wechaty.reply(message, 'roger')
```
## Message Class
All wechat messages will be encaped as a Message.
......@@ -520,43 +512,21 @@ A message may be not fully initialized yet. Call `ready()` to confirm we get all
Return a Promise, will be resolved when all data is ready.
```javascript
```typescript
message.ready()
.then(() => {
// Here we can be sure all the data is ready for use.
})
```
### @deprecated Message.get(prop): String|Contact|Room|Date
Get prop from a message.
Supported prop list:
1. `id` :String
1. `from` :Contact
1. `to` :Contact
1. `content` :String
1. `room` :Room
1. `date` :Date
```javascript
message.get('content')
```
### @deprecated Message.set(prop, value): Message
Set prop to value for a message.
Supported prop list: the same as `get(prop)`
```javascript
message.set('content', 'Hello, World!')
```
## Contact Class
`Contact` is `Sayable`
### Contact.id: string
Uniq id
### Contact.name(): string
### Contact.ready(): Contact
......@@ -564,35 +534,17 @@ A Contact may be not fully initialized yet. Call `ready()` to confirm we get all
Return a Promise, will be resolved when all data is ready.
```javascript
```typescript
contact.ready()
.then(() => {
// Here we can be sure all the data is ready for use.
})
```
### Contact.say(content: string)
### Contact.say(content: string): Promise<void>
say `content` to Contact
### @deprecated Contact.get(prop): String|Number
Get prop from a contact.
Supported prop list:
1. `id` :String
1. `weixin` :String
1. `name` :String
1. `remark` :String
1. `sex` :Number
1. `province` :String
1. `city` :String
1. `signature` :String
```javascript
contact.get('name')
```
## Class Room
`Room` is `Sayable`
......@@ -611,7 +563,7 @@ A room may be not fully initialized yet. Call `ready()` to confirm we get all th
Return a Promise, will be resolved when all data is ready.
```javascript
```typescript
room.ready()
.then(() => {
// Here we can be sure all the data is ready for use.
......@@ -630,7 +582,7 @@ which means there will be a `this.say()` method inside listener call, you can us
#### Event: `join`
```javascript
```typescript
Room.on('join', (invitee, inviter) => void)
```
......@@ -670,7 +622,7 @@ Room.findAll(query : Query) : Room[]
### Room.add(contact: Contact): Promise<any>
```javascript
```typescript
const friend = message.get('from')
const room = Room.find({ name: 'Group Name' })
if (room) {
......@@ -706,7 +658,7 @@ Supported prop list:
1. `contact` :Contact
1. `name` :String
```javascript
```typescript
room.get('members').length
```
......
......@@ -6,47 +6,50 @@
* https://github.com/zixia/wechaty
*
*/
import Config from './config'
import {
Config
, Sayable
} from './config'
import Message from './message'
import UtilLib from './util-lib'
import Wechaty from './wechaty'
import log from './brolog-env'
type ContactObj = {
id: string
uin: string
name: string
remark: string
weixin: string
sex: string
province: string
city: string
signature: string
address: string
stranger: boolean
star: boolean
city: string
id: string
name: string
province: string
remark: string
sex: string
signature: string
star: boolean
stranger: boolean
uin: string
weixin: string
}
type ContactRawObj = {
UserName: string
Uin: string
export type ContactRawObj = {
Alias: string
RemarkName: string
Sex: string
Province: string
City: string
NickName: string
StarFriend: string
stranger: string
Province: string
RemarkName: string
Sex: string
Signature: string
StarFriend: string
Uin: string
UserName: string
stranger: string // assign by injectio.js
}
type ContactQueryFilter = {
export type ContactQueryFilter = {
name: string | RegExp
}
class Contact {
export class Contact implements Sayable {
private static pool = new Map<string, Contact>()
private obj: ContactObj
......@@ -223,6 +226,4 @@ class Contact {
// return []
// }
// module.exports = Contact.default = Contact.Contact = Contact
export default Contact
......@@ -9,6 +9,7 @@
import {
Config
, RecommendInfo
, Sayable
} from './config'
import Contact from './contact'
......@@ -48,7 +49,7 @@ export type MessageType = {
[index: string]: number|string
}
export class Message {
export class Message implements Sayable {
public static counter = 0
private _counter: number
......
......@@ -431,6 +431,7 @@
if (c) {
if (c.isContact) {
// extend rawObj to identify `stranger`
c.stranger = !(c.isContact())
}
......
......@@ -12,6 +12,9 @@
import { EventEmitter } from 'events'
import {
Sayable
} from './config'
import Contact from './contact'
import Message from './message'
import Room from './room'
......@@ -21,7 +24,7 @@ import log from './brolog-env'
// (id: string): Promise<any>
// }
abstract class Puppet extends EventEmitter {
export abstract class Puppet extends EventEmitter implements Sayable {
public userId: string
public user: Contact
public abstract getContact(id: string): Promise<any>
......@@ -81,22 +84,25 @@ abstract class Puppet extends EventEmitter {
public abstract quit(): Promise<any>
public abstract ding(data?: string): Promise<any>
/**
* FriendRequest
*/
public abstract friendRequestSend(contact: Contact, hello?: string): Promise<any>
public abstract friendRequestAccept(contact: Contact, ticket: string): Promise<any>
/**
* Room
*/
public abstract roomAdd(room: Room, contact: Contact): Promise<number>
public abstract roomDel(room: Room, contact: Contact): Promise<number>
public abstract roomTopic(room: Room, topic: string): Promise<string>
public abstract roomCreate(contactList: Contact[], topic?: string): Promise<Room>
public abstract roomFind(filterFunc: string): Promise<Room[]>
/**
* Contact
*/
public abstract contactFind(filterFunc: string): Promise<Contact[]>
}
export default Puppet
export {
Contact
, Message
, Puppet
, Room
}
......@@ -49,7 +49,7 @@ export type RoomQueryFilter = {
topic: string | RegExp
}
export class Room extends EventEmitter {
export class Room extends EventEmitter implements Sayable {
private static pool = new Map<string, Room>()
private dirtyObj: RoomObj // when refresh, use this to save dirty data for query
......
/**
*
* wechaty: Wechat for ChatBots.
* Wechaty: Wechat for ChatBots.
* Connect ChatBots
*
* Class Wechaty
*
......@@ -33,7 +34,6 @@ export type WechatySetting = {
profile?: string
head?: HeadType
type?: PuppetType
// port?: number
}
type WechatyEventName = 'error'
......@@ -48,7 +48,7 @@ type WechatyEventName = 'error'
| 'scan'
| 'EVENT_PARAM_ERROR'
export class Wechaty extends EventEmitter {
export class Wechaty extends EventEmitter implements Sayable {
private static _instance: Wechaty
public puppet: Puppet
......@@ -309,8 +309,4 @@ export class Wechaty extends EventEmitter {
}
}
/**
* Expose `Wechaty`.
*/
// module.exports = Wechaty.default = Wechaty.Wechaty = Wechaty
export default Wechaty
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册