Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send MiniProgram using PadPro #1822

Merged
merged 4 commits into from Jul 20, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -108,7 +108,7 @@ See more:
* [.logout()](#Wechaty+logout) ⇒ <code>Promise.&lt;void&gt;</code>
* [.logonoff()](#Wechaty+logonoff) ⇒ <code>boolean</code>
* [.userSelf()](#Wechaty+userSelf) ⇒ [<code>ContactSelf</code>](#ContactSelf)
* [.say(textOrContactOrFileOrUrl)](#Wechaty+say) ⇒ <code>Promise.&lt;void&gt;</code>
* [.say(textOrContactOrFileOrUrlOrMini)](#Wechaty+say) ⇒ <code>Promise.&lt;void&gt;</code>
* _static_
* [.instance([options])](#Wechaty.instance)

@@ -310,7 +310,7 @@ console.log(`Bot is ${contact.name()}`)
```
<a name="Wechaty+say"></a>
### wechaty.say(textOrContactOrFileOrUrl) ⇒ <code>Promise.&lt;void&gt;</code>
### wechaty.say(textOrContactOrFileOrUrlOrMini) ⇒ <code>Promise.&lt;void&gt;</code>
Send message to userSelf, in other words, bot send message to itself.
> Tips:
This function is depending on the Puppet Implementation, see [puppet-compatible-table](https://github.com/Chatie/wechaty/wiki/Puppet#3-puppet-compatible-table)
@@ -319,7 +319,7 @@ This function is depending on the Puppet Implementation, see [puppet-compatible-
| Param | Type | Description |
| --- | --- | --- |
| textOrContactOrFileOrUrl | <code>string</code> \| <code>Contact</code> \| <code>FileBox</code> \| <code>UrlLink</code> | send text, Contact, or file to bot. </br> You can use [FileBox](https://www.npmjs.com/package/file-box) to send file |
| textOrContactOrFileOrUrlOrMini | <code>string</code> \| <code>Contact</code> \| <code>FileBox</code> \| <code>UrlLink</code> \| <code>MiniProgram</code> | send text, Contact, or file to bot. </br> You can use [FileBox](https://www.npmjs.com/package/file-box) to send file |
**Example**
```js
@@ -352,6 +352,17 @@ const linkPayload = new UrlLink ({
url : 'https://github.com/chatie/wechaty',
})
await bot.say(linkPayload)
// 6. send MiniProgram to bot itself
const miniProgramPayload = new MiniProgram ({
username : 'gh_xxxxxxx', //get from mp.weixin.qq.com
appid : '', //optional, get from mp.weixin.qq.com
title : '', //optional
pagepath : '', //optional
description : '', //optional
thumbnailurl : '', //optional
})
await bot.say(miniProgramPayload)
```
<a name="Wechaty.instance"></a>
@@ -98,7 +98,7 @@
"semver": "^6.0.0",
"state-switch": "^0.6.2",
"watchdog": "^0.8.1",
"wechaty-puppet": "^0.15.15",
"wechaty-puppet": "^0.15.25",
"ws": "^7.0.0"
},
"devDependencies": {
@@ -36,6 +36,7 @@ export {
Room,
RoomInvitation,
UrlLink,
MiniProgram,
} from './user'

export {
@@ -40,6 +40,7 @@ import {
} from '../types'

import { UrlLink } from './url-link'
import { MiniProgram } from './mini-program'

export const POOL = Symbol('pool')

@@ -301,12 +302,13 @@ export class Contact extends Accessory implements Sayable {
public async say (file: FileBox) : Promise<void>
public async say (contact: Contact) : Promise<void>
public async say (url: UrlLink) : Promise<void>
public async say (mini: MiniProgram): Promise<void>

/**
* > Tips:
* This function is depending on the Puppet Implementation, see [puppet-compatible-table](https://github.com/Chatie/wechaty/wiki/Puppet#3-puppet-compatible-table)
*
* @param {(string | Contact | FileBox | UrlLink)} textOrContactOrFileOrUrl
* @param {(string | Contact | FileBox | UrlLink | MiniProgram)} textOrContactOrFileOrUrlOrMini
* send text, Contact, or file to contact. </br>
* You can use {@link https://www.npmjs.com/package/file-box|FileBox} to send file
* @returns {Promise<void>}
@@ -341,40 +343,59 @@ export class Contact extends Accessory implements Sayable {
* url : 'https://github.com/chatie/wechaty',
* })
* await contact.say(urlLink)
*
* // 5. send mini program to contact
*
* const miniProgram = new MiniProgram ({
* username : 'gh_xxxxxxx', //get from mp.weixin.qq.com
* appid : '', //optional, get from mp.weixin.qq.com
* title : '', //optional
* pagepath : '', //optional
* description : '', //optional
* thumbnailurl : '', //optional
* })
* await contact.say(miniProgram)
*/
public async say (textOrContactOrFileOrUrl: string | Contact | FileBox | UrlLink): Promise<void> {
log.verbose('Contact', 'say(%s)', textOrContactOrFileOrUrl)
public async say (textOrContactOrFileOrUrlOrMini: string | Contact | FileBox | UrlLink | MiniProgram): Promise<void> {
log.verbose('Contact', 'say(%s)', textOrContactOrFileOrUrlOrMini)

if (typeof textOrContactOrFileOrUrl === 'string') {
if (typeof textOrContactOrFileOrUrlOrMini === 'string') {
/**
* 1. Text
*/
await this.puppet.messageSendText({
contactId: this.id,
}, textOrContactOrFileOrUrl)
} else if (textOrContactOrFileOrUrl instanceof Contact) {
}, textOrContactOrFileOrUrlOrMini)
} else if (textOrContactOrFileOrUrlOrMini instanceof Contact) {
/**
* 2. Contact
*/
await this.puppet.messageSendContact({
contactId: this.id,
}, textOrContactOrFileOrUrl.id)
} else if (textOrContactOrFileOrUrl instanceof FileBox) {
}, textOrContactOrFileOrUrlOrMini.id)
} else if (textOrContactOrFileOrUrlOrMini instanceof FileBox) {
/**
* 3. File
*/
await this.puppet.messageSendFile({
contactId: this.id,
}, textOrContactOrFileOrUrl)
} else if (textOrContactOrFileOrUrl instanceof UrlLink) {
}, textOrContactOrFileOrUrlOrMini)
} else if (textOrContactOrFileOrUrlOrMini instanceof UrlLink) {
/**
* 4. Link Message
*/
await this.puppet.messageSendUrl({
contactId : this.id,
}, textOrContactOrFileOrUrl.payload)
}, textOrContactOrFileOrUrlOrMini.payload)
} else if (textOrContactOrFileOrUrlOrMini instanceof MiniProgram) {
/**
* 5. Mini Program
*/
await this.puppet.messageSendMiniProgram({
contactId : this.id,
}, textOrContactOrFileOrUrlOrMini.payload)
} else {
throw new Error('unsupported arg: ' + textOrContactOrFileOrUrl)
throw new Error('unsupported arg: ' + textOrContactOrFileOrUrlOrMini)
}
}

@@ -8,3 +8,4 @@ export * from './money'
export * from './room'
export * from './room-invitation'
export * from './url-link'
export * from './mini-program'
@@ -50,6 +50,9 @@ import {
import {
UrlLink,
} from './url-link'
import {
MiniProgram,
} from './mini-program'

export interface MessageUserQueryFilter {
from? : Contact,
@@ -423,6 +426,7 @@ export class Message extends Accessory implements Sayable {
public async say (contact: Contact) : Promise<void>
public async say (file: FileBox) : Promise<void>
public async say (url: UrlLink) : Promise<void>
public async say (mini: MiniProgram) : Promise<void>

public async say (...args: never[]): Promise<never>
/**
@@ -431,7 +435,7 @@ export class Message extends Accessory implements Sayable {
* This function is depending on the Puppet Implementation, see [puppet-compatible-table](https://github.com/Chatie/wechaty/wiki/Puppet#3-puppet-compatible-table)
*
* @see {@link https://github.com/Chatie/wechaty/blob/1523c5e02be46ebe2cc172a744b2fbe53351540e/examples/ding-dong-bot.ts|Examples/ding-dong-bot}
* @param {(string | Contact | FileBox | UrlLink)} textOrContactOrFile
* @param {(string | Contact | FileBox | UrlLink | MiniProgram)} textOrContactOrFile
* send text, Contact, or file to bot. </br>
* You can use {@link https://www.npmjs.com/package/file-box|FileBox} to send file
* @param {(Contact|Contact[])} [mention]
@@ -479,53 +483,76 @@ export class Message extends Accessory implements Sayable {
* })
* await msg.say(linkPayload)
* }
*
* // 5. send MiniProgram
*
* if (/^link$/i.test(m.text())) {
* const miniProgramPayload = new MiniProgram ({
* username : 'gh_xxxxxxx', //get from mp.weixin.qq.com
* appid : '', //optional, get from mp.weixin.qq.com
* title : '', //optional
* pagepath : '', //optional
* description : '', //optional
* thumbnailurl : '', //optional
* })
* await msg.say(miniProgramPayload)
* }
*
* })
* .start()
*/
public async say (
textOrContactOrFileOrUrl : string | Contact | FileBox | UrlLink,
textOrContactOrFileOrUrlOrMini : string | Contact | FileBox | UrlLink | MiniProgram,
): Promise<void> {
log.verbose('Message', 'say(%s)', textOrContactOrFileOrUrl)
log.verbose('Message', 'say(%s)', textOrContactOrFileOrUrlOrMini)

// const user = this.puppet.userSelf()
const from = this.from()
// const to = this.to()
const room = this.room()

if (typeof textOrContactOrFileOrUrl === 'string') {
if (typeof textOrContactOrFileOrUrlOrMini === 'string') {
/**
* Text Message
*/
await this.puppet.messageSendText({
contactId : (from && from.id) || undefined,
roomId : (room && room.id) || undefined,
}, textOrContactOrFileOrUrl)
} else if (textOrContactOrFileOrUrl instanceof Contact) {
}, textOrContactOrFileOrUrlOrMini)
} else if (textOrContactOrFileOrUrlOrMini instanceof Contact) {
/**
* Contact Card
*/
await this.puppet.messageSendContact({
contactId : (from && from.id) || undefined,
roomId : (room && room.id) || undefined,
}, textOrContactOrFileOrUrl.id)
} else if (textOrContactOrFileOrUrl instanceof FileBox) {
}, textOrContactOrFileOrUrlOrMini.id)
} else if (textOrContactOrFileOrUrlOrMini instanceof FileBox) {
/**
* File Message
*/
await this.puppet.messageSendFile({
contactId : (from && from.id) || undefined,
roomId : (room && room.id) || undefined,
}, textOrContactOrFileOrUrl)
} else if (textOrContactOrFileOrUrl instanceof UrlLink) {
}, textOrContactOrFileOrUrlOrMini)
} else if (textOrContactOrFileOrUrlOrMini instanceof UrlLink) {
/**
* Link Message
*/
await this.puppet.messageSendUrl({
contactId : (from && from.id) || undefined,
roomId : (room && room.id) || undefined,
}, textOrContactOrFileOrUrl.payload)
}, textOrContactOrFileOrUrlOrMini.payload)
} else if (textOrContactOrFileOrUrlOrMini instanceof MiniProgram) {
/**
* MiniProgram
*/
await this.puppet.messageSendMiniProgram({
contactId : (from && from.id) || undefined,
roomId : (room && room.id) || undefined,
}, textOrContactOrFileOrUrlOrMini.payload)
} else {
throw new Error('unknown msg: ' + textOrContactOrFileOrUrl)
throw new Error('unknown msg: ' + textOrContactOrFileOrUrlOrMini)
}
}

@@ -904,4 +931,24 @@ export class Message extends Accessory implements Sayable {
return new UrlLink(urlPayload)
}

public async toMiniProgram (): Promise<MiniProgram> {
log.verbose('Message', 'toMiniProgram()')

if (!this.payload) {
throw new Error('no payload')
}

if (this.type() !== Message.Type.MiniProgram) {
throw new Error('message not a MiniProgram')
}

const miniProgramPayload = await this.puppet.messageMiniProgram(this.id)

if (!miniProgramPayload) {
throw new Error(`no miniProgram payload for message ${this.id}`)
}

return new MiniProgram(miniProgramPayload)
}

}
@@ -0,0 +1,62 @@
import {
MiniProgramPayload,
} from 'wechaty-puppet'

import {
log,
} from '../config'

export class MiniProgram {

/**
*
* Create
*
*/
public static async create (): Promise<MiniProgram> {
log.verbose('MiniProgram', 'create()')

// TODO: get appid and username from wechat
const payload: MiniProgramPayload = {
appid : 'todo',
description : 'todo',
pagepath : 'todo',
thumbnailurl : 'todo',
title : 'todo',
username : 'todo',
}

return new MiniProgram(payload)
}

constructor (
public readonly payload: MiniProgramPayload,
) {
log.verbose('MiniProgram', 'constructor()')
}

public appid (): undefined | string {
return this.payload.appid
}

public title (): undefined | string {
return this.payload.title
}

public pagepath (): undefined | string {
return this.payload.pagepath
}

public username (): undefined | string {
return this.payload.username
}

public description (): undefined | string {
return this.payload.description
}

public thumbnailurl (): undefined | string {
return this.payload.thumbnailurl
}

}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.