Skip to content

Commit

Permalink
v1.10
Browse files Browse the repository at this point in the history
  • Loading branch information
huan committed Nov 21, 2021
1 parent e088b33 commit 21de69c
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 61 deletions.
12 changes: 6 additions & 6 deletions examples/ding-dong-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ puppet.start()
* `scan`, `login`, `logout`, `error`, and `message`
*
*/
function onScan (payload: PUPPET.payload.EventScan) {
function onScan (payload: PUPPET.payloads.EventScan) {
if (payload.qrcode) {
// Generate a QR Code online via
// http://goqr.me/api/doc/create-qr-code/
Expand All @@ -81,16 +81,16 @@ function onScan (payload: PUPPET.payload.EventScan) {
}
}

function onLogin (payload: PUPPET.payload.EventLogin) {
function onLogin (payload: PUPPET.payloads.EventLogin) {
console.info(`${payload.contactId} login`)
puppet.messageSendText(payload.contactId, 'Wechaty login').catch(console.error)
}

function onLogout (payload: PUPPET.payload.EventLogout) {
function onLogout (payload: PUPPET.payloads.EventLogout) {
console.info(`${payload.contactId} logouted`)
}

function onError (payload: PUPPET.payload.EventError) {
function onError (payload: PUPPET.payloads.EventError) {
console.error('Bot error:', payload.data)
/*
if (bot.isLoggedIn) {
Expand All @@ -105,11 +105,11 @@ function onError (payload: PUPPET.payload.EventError) {
* dealing with Messages.
*
*/
async function onMessage (payload: PUPPET.payload.EventMessage) {
async function onMessage (payload: PUPPET.payloads.EventMessage) {
const messagePayload = await puppet.messagePayload(payload.messageId)
console.info(JSON.stringify(messagePayload))

if (messagePayload.type === PUPPET.type.Message.Text
if (messagePayload.type === PUPPET.types.Message.Text
&& /^ding$/i.test(messagePayload.text || '')
) {
const conversationId = messagePayload.roomId || messagePayload.fromId
Expand Down
2 changes: 1 addition & 1 deletion src/firer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import type { PuppetWeChat } from './puppet-wechat.js'

const CURRENT_USER_ID = 'current-user-id'
const mockPuppetWeChat = {
currentUserId: SELF_ID,
currentUserId: CURRENT_USER_ID,
} as any as PuppetWeChat

test('parseFriendConfirm()', async (t) => {
Expand Down
64 changes: 32 additions & 32 deletions src/puppet-wechat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class PuppetWeChat extends PUPPET.Puppet {

public bridge: Bridge

public scanPayload? : PUPPET.payload.EventScan
public scanPayload? : PUPPET.payloads.EventScan
public scanWatchdog : Watchdog<ScanFoodType>

private fileId: number
Expand Down Expand Up @@ -296,7 +296,7 @@ export class PuppetWeChat extends PUPPET.Puppet {

override async messageRawPayloadParser (
rawPayload: WebMessageRawPayload,
): Promise<PUPPET.payload.Message> {
): Promise<PUPPET.payloads.Message> {
log.verbose('PuppetWeChat', 'messageRawPayloadParser(%s) @ %s', rawPayload, this)

const payload = messageRawPayloadParser(rawPayload)
Expand All @@ -306,7 +306,7 @@ export class PuppetWeChat extends PUPPET.Puppet {
* https://github.com/wechaty/wechaty-puppet-wechat/issues/141
*/
if (payload.roomId && payload.text) {
(payload as PUPPET.payload.MessageRoom).mentionIdList = await parseMentionIdList(this, payload.roomId, payload.text)
(payload as PUPPET.payloads.MessageRoom).mentionIdList = await parseMentionIdList(this, payload.roomId, payload.text)
}

return payload
Expand All @@ -322,11 +322,11 @@ export class PuppetWeChat extends PUPPET.Puppet {
return fileBox
}

override async messageUrl (messageId: string) : Promise<PUPPET.payload.UrlLink> {
override async messageUrl (messageId: string) : Promise<PUPPET.payloads.UrlLink> {
return PUPPET.throwUnsupportedError(messageId)
}

override async messageMiniProgram (messageId: string): Promise<PUPPET.payload.MiniProgram> {
override async messageMiniProgram (messageId: string): Promise<PUPPET.payloads.MiniProgram> {
log.verbose('PuppetWeChat', 'messageMiniProgram(%s)', messageId)
return PUPPET.throwUnsupportedError(messageId)
}
Expand All @@ -337,7 +337,7 @@ export class PuppetWeChat extends PUPPET.Puppet {
let url = await this.messageRawPayloadToUrl(rawPayload)

if (!url) {
throw new Error('no url for type ' + PUPPET.type.Message[rawPayload.MsgType])
throw new Error('no url for type ' + PUPPET.types.Message[rawPayload.MsgType])
}

// use http instead of https, because https will only success on the very first request!
Expand Down Expand Up @@ -388,12 +388,12 @@ export class PuppetWeChat extends PUPPET.Puppet {

override async messageSendUrl (
conversationId : string,
urlLinkPayload : PUPPET.payload.UrlLink,
urlLinkPayload : PUPPET.payloads.UrlLink,
) : Promise<void> {
PUPPET.throwUnsupportedError(conversationId, urlLinkPayload)
}

override async messageSendMiniProgram (conversationId: string, miniProgramPayload: PUPPET.payload.MiniProgram): Promise<void> {
override async messageSendMiniProgram (conversationId: string, miniProgramPayload: PUPPET.payloads.MiniProgram): Promise<void> {
log.verbose('PuppetWeChat', 'messageSendMiniProgram("%s", %s)',
conversationId,
JSON.stringify(miniProgramPayload),
Expand Down Expand Up @@ -550,7 +550,7 @@ export class PuppetWeChat extends PUPPET.Puppet {

override async contactRawPayloadParser (
rawPayload: WebContactRawPayload,
): Promise<PUPPET.payload.Contact> {
): Promise<PUPPET.payloads.Contact> {
log.silly('PuppetWeChat', 'contactParseRawPayload(Object.keys(payload).length=%d)',
Object.keys(rawPayload).length,
)
Expand Down Expand Up @@ -593,8 +593,8 @@ export class PuppetWeChat extends PUPPET.Puppet {
* @ignore
*/
type: (!!rawPayload.UserName && !rawPayload.UserName.startsWith('@@') && !!(rawPayload.VerifyFlag & 8))
? PUPPET.type.Contact.Official
: PUPPET.type.Contact.Individual,
? PUPPET.types.Contact.Official
: PUPPET.types.Contact.Individual,
weixin: rawPayload.Alias, // Wechat ID
}
}
Expand Down Expand Up @@ -746,7 +746,7 @@ export class PuppetWeChat extends PUPPET.Puppet {

override async roomRawPayloadParser (
rawPayload: WebRoomRawPayload,
): Promise<PUPPET.payload.Room> {
): Promise<PUPPET.payloads.Room> {
log.verbose('PuppetWeChat', 'roomRawPayloadParser(%s)', rawPayload)

// const payload = await this.roomPayload(rawPayload.UserName)
Expand Down Expand Up @@ -784,7 +784,7 @@ export class PuppetWeChat extends PUPPET.Puppet {
? rawPayload.MemberList.map(m => m.UserName)
: []

const roomPayload: PUPPET.payload.Room = {
const roomPayload: PUPPET.payloads.Room = {
adminIdList: [],
id,
memberIdList,
Expand Down Expand Up @@ -922,10 +922,10 @@ export class PuppetWeChat extends PUPPET.Puppet {
}
}

override async roomMemberRawPayloadParser (rawPayload: WebRoomRawMember): Promise<PUPPET.payload.RoomMember> {
override async roomMemberRawPayloadParser (rawPayload: WebRoomRawMember): Promise<PUPPET.payloads.RoomMember> {
log.verbose('PuppetWeChat', 'roomMemberRawPayloadParser(%s)', rawPayload)

const payload: PUPPET.payload.RoomMember = {
const payload: PUPPET.payloads.RoomMember = {
avatar : rawPayload.HeadImgUrl,
id : rawPayload.UserName,
name : rawPayload.NickName,
Expand All @@ -947,7 +947,7 @@ export class PuppetWeChat extends PUPPET.Puppet {
return PUPPET.throwUnsupportedError(roomInvitationId)
}

override async roomInvitationRawPayloadParser (rawPayload: any): Promise<PUPPET.payload.RoomInvitation> {
override async roomInvitationRawPayloadParser (rawPayload: any): Promise<PUPPET.payloads.RoomInvitation> {
return PUPPET.throwUnsupportedError(rawPayload)
}

Expand All @@ -963,7 +963,7 @@ export class PuppetWeChat extends PUPPET.Puppet {
return rawPayload
}

override async friendshipRawPayloadParser (rawPayload: WebMessageRawPayload): Promise<PUPPET.payload.Friendship> {
override async friendshipRawPayloadParser (rawPayload: WebMessageRawPayload): Promise<PUPPET.payloads.Friendship> {
log.warn('PuppetWeChat', 'friendshipRawPayloadParser(%s)', rawPayload)

const timestamp = Math.floor(Date.now() / 1000) // in seconds
Expand All @@ -975,22 +975,22 @@ export class PuppetWeChat extends PUPPET.Puppet {
throw new Error('no RecommendInfo')
}

const payloadReceive: PUPPET.payload.FriendshipReceive = {
const payloadReceive: PUPPET.payloads.FriendshipReceive = {
contactId : recommendInfo.UserName,
hello : recommendInfo.Content,
id : rawPayload.MsgId,
ticket : recommendInfo.Ticket,
timestamp,
type : PUPPET.type.Friendship.Receive,
type : PUPPET.types.Friendship.Receive,
}
return payloadReceive
}
case WebMessageType.SYS: {
const payloadConfirm: PUPPET.payload.FriendshipConfirm = {
const payloadConfirm: PUPPET.payloads.FriendshipConfirm = {
contactId : rawPayload.FromUserName,
id : rawPayload.MsgId,
timestamp,
type : PUPPET.type.Friendship.Confirm,
type : PUPPET.types.Friendship.Confirm,
}
return payloadConfirm
}
Expand Down Expand Up @@ -1026,7 +1026,7 @@ export class PuppetWeChat extends PUPPET.Puppet {
override async friendshipAccept (
friendshipId : string,
): Promise<void> {
const payload = await this.friendshipPayload(friendshipId) as any as PUPPET.payload.FriendshipReceive
const payload = await this.friendshipPayload(friendshipId) as any as PUPPET.payloads.FriendshipReceive

try {
await this.bridge.verifyUserOk(payload.contactId, payload.ticket)
Expand Down Expand Up @@ -1136,27 +1136,27 @@ export class PuppetWeChat extends PUPPET.Puppet {
): Promise<null | string> {
log.silly('PuppetWeChat', 'readyMedia()')

// let type = PUPPET.type.Message.Unknown
// let type = PUPPET.types.Message.Unknown
let url: undefined | string

try {

switch (rawPayload.MsgType) {
case WebMessageType.EMOTICON:
// type = PUPPET.type.Message.Emoticon
// type = PUPPET.types.Message.Emoticon
url = await this.bridge.getMsgEmoticon(rawPayload.MsgId)
break
case WebMessageType.IMAGE:
// type = PUPPET.type.Message.Image
// type = PUPPET.types.Message.Image
url = await this.bridge.getMsgImg(rawPayload.MsgId)
break
case WebMessageType.VIDEO:
case WebMessageType.MICROVIDEO:
// type = PUPPET.type.Message.Video
// type = PUPPET.types.Message.Video
url = await this.bridge.getMsgVideo(rawPayload.MsgId)
break
case WebMessageType.VOICE:
// type = PUPPET.type.Message.Audio
// type = PUPPET.types.Message.Audio
url = await this.bridge.getMsgVoice(rawPayload.MsgId)
break

Expand All @@ -1167,7 +1167,7 @@ export class PuppetWeChat extends PUPPET.Puppet {
throw new Error('no MMAppMsgDownloadUrl')
}
// had set in Message
// type = PUPPET.type.Message.Attachment
// type = PUPPET.types.Message.Attachment
url = rawPayload.MMAppMsgDownloadUrl
break

Expand All @@ -1177,7 +1177,7 @@ export class PuppetWeChat extends PUPPET.Puppet {
throw new Error('no Url')
}
// had set in Message
// type = PUPPET.type.Message.Attachment
// type = PUPPET.types.Message.Attachment
url = rawPayload.Url
break

Expand All @@ -1191,7 +1191,7 @@ export class PuppetWeChat extends PUPPET.Puppet {

case WebMessageType.TEXT:
if (rawPayload.SubMsgType === WebMessageType.LOCATION) {
// type = PUPPET.type.Message.Image
// type = PUPPET.types.Message.Image
url = await this.bridge.getMsgPublicLinkImg(rawPayload.MsgId)
}
break
Expand All @@ -1213,7 +1213,7 @@ export class PuppetWeChat extends PUPPET.Puppet {
// }
// url = this.payload.url
// return {
// type: PUPPET.type.Message.Unknown,
// type: PUPPET.types.Message.Unknown,
// }
return null
}
Expand Down Expand Up @@ -1544,7 +1544,7 @@ export class PuppetWeChat extends PUPPET.Puppet {
return PUPPET.throwUnsupportedError()
}

override async messageImage (messageId: string, imageType: PUPPET.type.Image): Promise<FileBoxInterface> {
override async messageImage (messageId: string, imageType: PUPPET.types.Image): Promise<FileBoxInterface> {
log.verbose('PuppetWeChat', 'messageImage(%s, %s)', messageId, imageType)
return this.messageFile(messageId)
}
Expand Down
6 changes: 3 additions & 3 deletions src/pure-function-helpers/message-raw-payload-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {

export function messageRawPayloadParser (
rawPayload: WebMessageRawPayload,
): PUPPET.payload.Message {
): PUPPET.payloads.Message {
const id = rawPayload.MsgId
const fromId = rawPayload.MMActualSender // MMPeerUserName
const text: string = rawPayload.MMActualContent // Content has @id prefix added by wx
Expand Down Expand Up @@ -49,7 +49,7 @@ export function messageRawPayloadParser (
}
}

const type: PUPPET.type.Message = webMessageType(rawPayload)
const type: PUPPET.types.Message = webMessageType(rawPayload)

const payloadBase = {
filename: msgFileName,
Expand All @@ -61,7 +61,7 @@ export function messageRawPayloadParser (
type,
}

let payload: PUPPET.payload.Message
let payload: PUPPET.payloads.Message

if (toId) {
payload = {
Expand Down
22 changes: 11 additions & 11 deletions src/pure-function-helpers/web-message-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,38 @@ import * as PUPPET from 'wechaty-puppet'

export function webMessageType (
rawPayload: WebMessageRawPayload,
): PUPPET.type.Message {
): PUPPET.types.Message {

switch (rawPayload.MsgType) {
case WebMessageType.TEXT:
switch (rawPayload.SubMsgType) {
case WebMessageType.LOCATION:
return PUPPET.type.Message.Attachment
return PUPPET.types.Message.Attachment

default:
return PUPPET.type.Message.Text
return PUPPET.types.Message.Text
}

case WebMessageType.EMOTICON:
case WebMessageType.IMAGE:
return PUPPET.type.Message.Image
return PUPPET.types.Message.Image

case WebMessageType.VOICE:
return PUPPET.type.Message.Audio
return PUPPET.types.Message.Audio

case WebMessageType.MICROVIDEO:
case WebMessageType.VIDEO:
return PUPPET.type.Message.Video
return PUPPET.types.Message.Video

case WebMessageType.APP:
switch (rawPayload.AppMsgType) {
case WebAppMsgType.ATTACH:
case WebAppMsgType.URL:
case WebAppMsgType.READER_TYPE:
return PUPPET.type.Message.Attachment
return PUPPET.types.Message.Attachment

default:
return PUPPET.type.Message.Text
return PUPPET.types.Message.Text
}

/**
Expand All @@ -49,10 +49,10 @@ export function webMessageType (
* FIXME: should we use better message type at here???
*/
case WebMessageType.SYS:
return PUPPET.type.Message.Text
return PUPPET.types.Message.Text
// add recall type
case WebMessageType.RECALLED:
return PUPPET.type.Message.Recalled
return PUPPET.types.Message.Recalled
// VERIFYMSG = 37,
// POSSIBLEFRIEND_MSG = 40,
// SHARECARD = 42,
Expand All @@ -64,6 +64,6 @@ export function webMessageType (
// SYSNOTICE = 9999,
// RECALLED = 10002,
default:
return PUPPET.type.Message.Text
return PUPPET.types.Message.Text
}
}
Loading

0 comments on commit 21de69c

Please sign in to comment.