diff --git a/examples/ding-dong-bot.ts b/examples/ding-dong-bot.ts index 4f636ea..266e7c3 100644 --- a/examples/ding-dong-bot.ts +++ b/examples/ding-dong-bot.ts @@ -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/ @@ -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) { @@ -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 diff --git a/src/firer.spec.ts b/src/firer.spec.ts index 41628bc..ccdc83a 100755 --- a/src/firer.spec.ts +++ b/src/firer.spec.ts @@ -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) => { diff --git a/src/puppet-wechat.ts b/src/puppet-wechat.ts index 9b37271..6c300cc 100644 --- a/src/puppet-wechat.ts +++ b/src/puppet-wechat.ts @@ -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 private fileId: number @@ -296,7 +296,7 @@ export class PuppetWeChat extends PUPPET.Puppet { override async messageRawPayloadParser ( rawPayload: WebMessageRawPayload, - ): Promise { + ): Promise { log.verbose('PuppetWeChat', 'messageRawPayloadParser(%s) @ %s', rawPayload, this) const payload = messageRawPayloadParser(rawPayload) @@ -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 @@ -322,11 +322,11 @@ export class PuppetWeChat extends PUPPET.Puppet { return fileBox } - override async messageUrl (messageId: string) : Promise { + override async messageUrl (messageId: string) : Promise { return PUPPET.throwUnsupportedError(messageId) } - override async messageMiniProgram (messageId: string): Promise { + override async messageMiniProgram (messageId: string): Promise { log.verbose('PuppetWeChat', 'messageMiniProgram(%s)', messageId) return PUPPET.throwUnsupportedError(messageId) } @@ -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! @@ -388,12 +388,12 @@ export class PuppetWeChat extends PUPPET.Puppet { override async messageSendUrl ( conversationId : string, - urlLinkPayload : PUPPET.payload.UrlLink, + urlLinkPayload : PUPPET.payloads.UrlLink, ) : Promise { PUPPET.throwUnsupportedError(conversationId, urlLinkPayload) } - override async messageSendMiniProgram (conversationId: string, miniProgramPayload: PUPPET.payload.MiniProgram): Promise { + override async messageSendMiniProgram (conversationId: string, miniProgramPayload: PUPPET.payloads.MiniProgram): Promise { log.verbose('PuppetWeChat', 'messageSendMiniProgram("%s", %s)', conversationId, JSON.stringify(miniProgramPayload), @@ -550,7 +550,7 @@ export class PuppetWeChat extends PUPPET.Puppet { override async contactRawPayloadParser ( rawPayload: WebContactRawPayload, - ): Promise { + ): Promise { log.silly('PuppetWeChat', 'contactParseRawPayload(Object.keys(payload).length=%d)', Object.keys(rawPayload).length, ) @@ -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 } } @@ -746,7 +746,7 @@ export class PuppetWeChat extends PUPPET.Puppet { override async roomRawPayloadParser ( rawPayload: WebRoomRawPayload, - ): Promise { + ): Promise { log.verbose('PuppetWeChat', 'roomRawPayloadParser(%s)', rawPayload) // const payload = await this.roomPayload(rawPayload.UserName) @@ -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, @@ -922,10 +922,10 @@ export class PuppetWeChat extends PUPPET.Puppet { } } - override async roomMemberRawPayloadParser (rawPayload: WebRoomRawMember): Promise { + override async roomMemberRawPayloadParser (rawPayload: WebRoomRawMember): Promise { 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, @@ -947,7 +947,7 @@ export class PuppetWeChat extends PUPPET.Puppet { return PUPPET.throwUnsupportedError(roomInvitationId) } - override async roomInvitationRawPayloadParser (rawPayload: any): Promise { + override async roomInvitationRawPayloadParser (rawPayload: any): Promise { return PUPPET.throwUnsupportedError(rawPayload) } @@ -963,7 +963,7 @@ export class PuppetWeChat extends PUPPET.Puppet { return rawPayload } - override async friendshipRawPayloadParser (rawPayload: WebMessageRawPayload): Promise { + override async friendshipRawPayloadParser (rawPayload: WebMessageRawPayload): Promise { log.warn('PuppetWeChat', 'friendshipRawPayloadParser(%s)', rawPayload) const timestamp = Math.floor(Date.now() / 1000) // in seconds @@ -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 } @@ -1026,7 +1026,7 @@ export class PuppetWeChat extends PUPPET.Puppet { override async friendshipAccept ( friendshipId : string, ): Promise { - 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) @@ -1136,27 +1136,27 @@ export class PuppetWeChat extends PUPPET.Puppet { ): Promise { 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 @@ -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 @@ -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 @@ -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 @@ -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 } @@ -1544,7 +1544,7 @@ export class PuppetWeChat extends PUPPET.Puppet { return PUPPET.throwUnsupportedError() } - override async messageImage (messageId: string, imageType: PUPPET.type.Image): Promise { + override async messageImage (messageId: string, imageType: PUPPET.types.Image): Promise { log.verbose('PuppetWeChat', 'messageImage(%s, %s)', messageId, imageType) return this.messageFile(messageId) } diff --git a/src/pure-function-helpers/message-raw-payload-parser.ts b/src/pure-function-helpers/message-raw-payload-parser.ts index 3c4de1a..7499957 100644 --- a/src/pure-function-helpers/message-raw-payload-parser.ts +++ b/src/pure-function-helpers/message-raw-payload-parser.ts @@ -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 @@ -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, @@ -61,7 +61,7 @@ export function messageRawPayloadParser ( type, } - let payload: PUPPET.payload.Message + let payload: PUPPET.payloads.Message if (toId) { payload = { diff --git a/src/pure-function-helpers/web-message-type.ts b/src/pure-function-helpers/web-message-type.ts index 1bb3553..495fa87 100644 --- a/src/pure-function-helpers/web-message-type.ts +++ b/src/pure-function-helpers/web-message-type.ts @@ -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 } /** @@ -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, @@ -64,6 +64,6 @@ export function webMessageType ( // SYSNOTICE = 9999, // RECALLED = 10002, default: - return PUPPET.type.Message.Text + return PUPPET.types.Message.Text } } diff --git a/tests/puppeteer-friendship.spec.ts b/tests/puppeteer-friendship.spec.ts index 7907be4..d29a553 100755 --- a/tests/puppeteer-friendship.spec.ts +++ b/tests/puppeteer-friendship.spec.ts @@ -68,9 +68,9 @@ test('PuppetWeChatFriendship.receive smoke testing', async (t) => { const hello = info.Content const ticket = info.Ticket const id = 'id' - const type = PUPPET.type.Friendship.Receive + const type = PUPPET.types.Friendship.Receive - const payload: PUPPET.payload.Friendship = { + const payload: PUPPET.payloads.Friendship = { contactId: info.UserName, hello, id, @@ -96,7 +96,7 @@ test('PuppetWeChatFriendship.receive smoke testing', async (t) => { t.equal(friendshipPayload.hello, '我是群聊"Wechaty"的李卓桓.PreAngel', 'should has right request message') t.equal(friendshipPayload.contactId, info.UserName, 'should have a Contact id') - t.equal(friendshipPayload.type, PUPPET.type.Friendship.Receive, 'should be receive type') + t.equal(friendshipPayload.type, PUPPET.types.Friendship.Receive, 'should be receive type') sandbox.restore() }) @@ -113,11 +113,11 @@ test('PuppetWeChatFriendship.confirm smoke testing', async (t) => { {"MsgId":"3382012679535022763","FromUserName":"@04a0fa314d0d8d50dc54e2ec908744ebf46b87404d143fd9a6692182dd90bd49","ToUserName":"@f7321198e0349f1b38c9f2ef158f70eb","MsgType":10000,"Content":"You have added 李卓桓.PreAngel as your WeChat contact. Start chatting!","Status":4,"ImgStatus":1,"CreateTime":1475569920,"VoiceLength":0,"PlayLength":0,"FileName":"","FileSize":"","MediaId":"","Url":"","AppMsgType":0,"StatusNotifyCode":0,"StatusNotifyUserName":"","RecommendInfo":{"UserName":"","NickName":"","QQNum":0,"Province":"","City":"","Content":"","Signature":"","Alias":"","Scene":0,"VerifyFlag":0,"AttrStatus":0,"Sex":0,"Ticket":"","OpCode":0},"ForwardFlag":0,"AppInfo":{"AppID":"","Type":0},"HasProductId":0,"Ticket":"","ImgHeight":0,"ImgWidth":0,"SubMsgType":0,"NewMsgId":3382012679535022600,"MMPeerUserName":"@04a0fa314d0d8d50dc54e2ec908744ebf46b87404d143fd9a6692182dd90bd49","MMDigest":"You have added 李卓桓.PreAngel as your WeChat contact. Start chatting!","MMIsSend":false,"MMIsChatRoom":false,"LocalID":"3382012679535022763","ClientMsgId":"3382012679535022763","MMActualContent":"You have added 李卓桓.PreAngel as your WeChat contact. Start chatting!","MMActualSender":"@04a0fa314d0d8d50dc54e2ec908744ebf46b87404d143fd9a6692182dd90bd49","MMDigestTime":"16:32","MMDisplayTime":1475569920,"MMTime":"16:32"} `) - const friendshipPayload: PUPPET.payload.Friendship = { + const friendshipPayload: PUPPET.payloads.Friendship = { contactId : CONTACT_ID, id : 'id', timestamp : Math.floor(Date.now() / 1000), // in seconds - type : PUPPET.type.Friendship.Confirm, + type : PUPPET.types.Friendship.Confirm, } const sandbox = sinon.createSandbox() @@ -146,7 +146,7 @@ test('PuppetWeChatFriendship.confirm smoke testing', async (t) => { const friendshipPayload2 = await puppet.friendshipPayload('xx') t.equal(friendshipPayload2.contactId, CONTACT_ID, 'should have a Contact id') - t.equal(friendshipPayload2.type, PUPPET.type.Friendship.Confirm, 'should be confirm type') + t.equal(friendshipPayload2.type, PUPPET.types.Friendship.Confirm, 'should be confirm type') sandbox.restore() }) diff --git a/tests/puppeteer-message.spec.ts b/tests/puppeteer-message.spec.ts index 35fdb3c..8f075e2 100755 --- a/tests/puppeteer-message.spec.ts +++ b/tests/puppeteer-message.spec.ts @@ -78,13 +78,13 @@ test('constructor()', async t => { } const sandbox = sinon.createSandbox() const mockMessagePayload = async (/* _: string */) => { - const payload: PUPPET.payload.Message = { + const payload: PUPPET.payloads.Message = { fromId : EXPECTED.from, id : EXPECTED.id, mentionIdList : [], timestamp : Date.now(), toId : 'toId', - type : PUPPET.type.Message.Text, + type : PUPPET.types.Message.Text, } return payload }