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 #172

Merged
merged 6 commits into from Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -65,3 +65,5 @@ typings/
/*.png
/*.html
/*.memory-card.json

.idea
6 changes: 3 additions & 3 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "wechaty-puppet-padpro",
"version": "0.3.9",
"version": "0.3.10",
"description": "Padpro Puppet for Wechaty",
"directories": {
"example": "examples"
Expand Down Expand Up @@ -65,11 +65,11 @@
"qrcode-terminal": "^0.12.0",
"shx": "^0.3.1",
"tstest": "^0.4.2",
"wechaty-puppet": "^0.15.21"
"wechaty-puppet": "^0.15.25"
},
"peerDependencies": {
"file-box": "^0.8.27",
"wechaty-puppet": "^0.15.1"
"wechaty-puppet": "^0.15.25"
},
"homepage": "https://github.com/botorange/wechaty-puppet-padpro#readme",
"dependencies": {
Expand Down
49 changes: 49 additions & 0 deletions src/puppet-padpro.ts
Expand Up @@ -43,6 +43,7 @@ import {
RoomPayload,

UrlLinkPayload,
MiniProgramPayload,
} from 'wechaty-puppet'

import {
Expand All @@ -59,6 +60,7 @@ import {
friendshipVerifyEventMessageParser,

generateAppXMLMessage,
generateMiniProgramXMLMessage,
generateAttachmentXMLMessageFromRaw,

imagePayloadParser,
Expand Down Expand Up @@ -987,6 +989,27 @@ export class PuppetPadpro extends Puppet {
}
}

public async messageMiniProgram (messageId: string): Promise<MiniProgramPayload> {

const rawPayload = await this.messageRawPayload(messageId)
const payload = await this.messagePayload(messageId)

if (payload.type !== MessageType.MiniProgram) {
throw new Error('Can not get miniProgram from non miniProgram payload')
} else {
const appPayload = await appMessageParser(rawPayload)
if (appPayload) {
return {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason returning empty object here?

Copy link
Contributor Author

@zhaoic zhaoic Jul 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is messageForward work well?
I tested with text , using 'msg.forward(contact)', it's not work!
I confirmed that code was step into messageForward->this.messageSendText
receiver:

{ contactId: 'wxid_gjg27bv08vu612', roomId: undefined }

payload:

{ id: '1097670511',
  timestamp: 1564021102,
  type: 7,
  fromId: 'wxid_jy0q2q63aggm21',
  mentionIdList: undefined,
  roomId: undefined,
  text: 'hello world',
  toId: 'wxid_gjg27bv08vu612' }

image

description : appPayload.des,
title : appPayload.title,
// TODO: add miniProgram support in appMessageParser()
}
} else {
throw new Error('Can not parse miniProgram message payload')
}
}
}

public async messageRawPayload (id: string): Promise<PadproMessagePayload> {
const rawPayload = this.cachePadproMessagePayload.get(id)

Expand Down Expand Up @@ -1177,6 +1200,26 @@ export class PuppetPadpro extends Puppet {
await this.padproManager.GrpcSendApp(id, generateAppXMLMessage(urlLinkPayload))
}

public async messageSendMiniProgram (
receiver: Receiver,
miniProgramPayload: MiniProgramPayload
): Promise<void> {
log.verbose(PRE, `messageSendMiniProgram("${JSON.stringify(receiver)}", ${JSON.stringify(miniProgramPayload)})`)

if (!this.padproManager) {
throw new Error('no padpro manager')
}

// Send to the Room if there's a roomId
const id = receiver.roomId || receiver.contactId

if (!id) {
throw Error('no id')
}

await this.padproManager.GrpcSendApp(id, generateMiniProgramXMLMessage(miniProgramPayload))
}

public async messageForward (
receiver : Receiver,
messageId : string,
Expand Down Expand Up @@ -1229,6 +1272,12 @@ export class PuppetPadpro extends Puppet {
receiver,
await this.messageUrl(messageId)
)
} else if (payload.type === MessageType.MiniProgram) {
// TODO: currently this strips out the app information
await this.messageSendMiniProgram(
receiver,
await this.messageMiniProgram(messageId)
)
} else if (payload.type === MessageType.Video) {
await this.forwardVideo(receiver, messageId)
// commenting this out because currently there is no way to directly send location
Expand Down
61 changes: 60 additions & 1 deletion src/pure-function-helpers/app-message-generator.ts
@@ -1,4 +1,4 @@
import { UrlLinkPayload } from 'wechaty-puppet'
import { UrlLinkPayload, MiniProgramPayload } from 'wechaty-puppet'
import { PadproAppMessagePayload, PadproLocationMessagePayload, WechatAppMessageType } from '../schemas'

export const generateLocationMessage = (payload: PadproLocationMessagePayload): string => {
Expand All @@ -24,6 +24,65 @@ export const generateAppXMLMessage = ({ title, description, url, thumbnailUrl }:
+ `</appmsg>`
}

export const generateMiniProgramXMLMessage = (payload: MiniProgramPayload): string => {
return `
<appmsg appid="" sdkver="0">
<title>${payload.title}</title>
<des>${payload.description}</des>
<action/>
<type>33</type>
<showtype>0</showtype>
<soundtype>0</soundtype>
<mediatagname/>
<messageext/>
<messageaction/>
<content/>
<contentattr>0</contentattr>
<url>https://mp.weixin.qq.com/mp/waerrpage?appid=${payload.appid}&amp;type=upgrade&amp;upgradetype=3#wechat_redirect</url>
<lowurl/>
<dataurl/>
<lowdataurl/>
<appattach>
<totallen>0</totallen>
<attachid/>
<emoticonmd5/>
<fileext/>
<cdnthumburl></cdnthumburl>
<cdnthumbmd5></cdnthumbmd5>
<cdnthumblength></cdnthumblength>
<cdnthumbwidth></cdnthumbwidth>
<cdnthumbheight></cdnthumbheight>
<cdnthumbaeskey></cdnthumbaeskey>
<aeskey></aeskey>
<encryver>0</encryver>
<filekey></filekey>
</appattach>
<extinfo/>
<sourceusername>${payload.username}@app</sourceusername>
<sourcedisplayname>${payload.description}</sourcedisplayname>
<thumburl/>
<md5/>
<statextstr/>
<weappinfo>
<username><![CDATA[${payload.username}@app]]></username>
<appid><![CDATA[${payload.appid}]]></appid>
<type>2</type>
<version></version>
<weappiconurl><![CDATA[]]></weappiconurl>
<pagepath><![CDATA[${payload.pagepath}]]></pagepath>
<shareId><![CDATA[0_${payload.appid}_858901320_1563444358_0]]></shareId>
<appservicetype>0</appservicetype>
</weappinfo>
</appmsg>
<fromusername></fromusername>
<scene>0</scene>
<appinfo>
<version>1</version>
<appname/>
</appinfo>
<commenturl/>`
}

export const generateAttachmentXMLMessageFromRaw = (payload: PadproAppMessagePayload): string => {
return `
<appmsg appid="" sdkver="0">
Expand Down
1 change: 1 addition & 0 deletions src/pure-function-helpers/message-raw-payload-parser.ts
Expand Up @@ -238,6 +238,7 @@ export async function messageRawPayloadParser (
payload.type = MessageType.ChatHistory
break
case WechatAppMessageType.MiniProgram:
case WechatAppMessageType.MiniProgramApp:
payload.type = MessageType.MiniProgram
break
case WechatAppMessageType.RedEnvelopes:
Expand Down
1 change: 1 addition & 0 deletions src/schemas/padpro-enums.ts
Expand Up @@ -97,6 +97,7 @@ export enum WechatAppMessageType {
RealtimeShareLocation = 17,
ChatHistory = 19,
MiniProgram = 33,
MiniProgramApp = 36, // this is forwardable mini program
Transfers = 2000,
RedEnvelopes = 2001,
ReaderType = 100001,
Expand Down