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

message with attachment file caused by Message.ext() BREAKING CHANGE (fix #1175)

上级 2250ce12
......@@ -85,17 +85,30 @@ bot
)
if (/^(ding|ping|bing|code)$/i.test(m.text()) && !m.self()) {
m.say('dong')
/**
* 1. reply 'dong'
*/
log.info('Bot', 'REPLY: dong')
m.say('dong')
const joinWechaty = `Join Wechaty Developers' Community\n\n` +
`Wechaty is used in many ChatBot projects by hundreds of developers.\n\n` +
`If you want to talk with other developers, just scan the following QR Code in WeChat with secret code: wechaty,\n\n` +
`you can join our Wechaty Developers' Home at once`
await m.say(joinWechaty)
await m.say(new bot.Message(BOT_QR_CODE_IMAGE_FILE))
/**
* 2. reply qrcode image
*/
const imageMessage = new bot.Message(BOT_QR_CODE_IMAGE_FILE)
log.info('Bot', 'REPLY: %s', imageMessage)
await m.say(imageMessage)
/**
* 3. reply 'scan now!'
*/
await m.say('Scan now, because other Wechaty developers want to talk with you too!\n\n(secret code: wechaty)')
log.info('Bot', 'REPLY: Image')
}
} catch (e) {
log.error('Bot', 'on(message) exception: %s' , e)
......
......@@ -336,14 +336,14 @@ export class PuppetPuppeteer extends Puppet {
let mediatype: MediaType
switch (ext) {
case 'bmp':
case 'jpeg':
case 'jpg':
case 'png':
case 'gif':
case '.bmp':
case '.jpeg':
case '.jpg':
case '.png':
case '.gif':
mediatype = MediaType.IMAGE
break
case 'mp4':
case '.mp4':
mediatype = MediaType.VIDEO
break
default:
......@@ -507,7 +507,7 @@ export class PuppetPuppeteer extends Puppet {
},
},
}
let mediaId
let mediaId: string
try {
mediaId = <string>await new Promise((resolve, reject) => {
try {
......@@ -537,7 +537,7 @@ export class PuppetPuppeteer extends Puppet {
log.error('PuppetPuppeteer', 'uploadMedia(): upload fail')
throw new Error('PuppetPuppeteer.uploadMedia(): upload fail')
}
return Object.assign(mediaData, { MediaId: mediaId as string })
return Object.assign(mediaData, { MediaId: mediaId })
}
public async sendMedia(message: PuppeteerMessage): Promise<boolean> {
......@@ -556,8 +556,9 @@ export class PuppetPuppeteer extends Puppet {
}
let mediaData: MediaData
const rawObj = message.rawObj as MsgRawObj
if (!rawObj.MediaId) {
const rawObj = message.rawObj || {} as MsgRawObj
if (!rawObj || !rawObj.MediaId) {
try {
mediaData = await this.uploadMedia(message, destinationId)
message.rawObj = Object.assign(rawObj, mediaData)
......@@ -1090,14 +1091,14 @@ export class PuppetPuppeteer extends Puppet {
public extToType(ext: string): MsgType {
switch (ext) {
case 'bmp':
case 'jpeg':
case 'jpg':
case 'png':
case '.bmp':
case '.jpeg':
case '.jpg':
case '.png':
return MsgType.IMAGE
case 'gif':
case '.gif':
return MsgType.EMOTICON
case 'mp4':
case '.mp4':
return MsgType.VIDEO
default:
return MsgType.APP
......
......@@ -80,10 +80,10 @@ export class PuppeteerMessage extends Message {
super()
log.silly('PuppeteerMessage', 'constructor()')
this.obj = {} as MsgObj
this.obj = {} as MsgObj
// this.rawObj = {} as MsgRawObj
if (!fileOrObj) {
this.rawObj = <MsgRawObj>{}
return
}
......@@ -137,39 +137,39 @@ export class PuppeteerMessage extends Message {
* @private
*/
public toString() {
return `PuppeteerMessage<${Misc.plainText(this.obj.content)}>`
return `PuppeteerMessage<${Misc.plainText(this.obj && this.obj.content)}>`
}
/**
* @private
*/
public toStringDigest() {
const text = Misc.digestEmoji(this.obj.digest)
return '{' + this.typeEx() + '}' + text
}
/**
* @private
*/
public getSenderString() {
const from = PuppeteerContact.load(this.obj.from)
from.puppet = this.puppet
const fromName = from.name()
const roomTopic = this.obj.room
? (':' + PuppeteerRoom.load(this.obj.room).topic())
: ''
return `<${fromName}${roomTopic}>`
}
/**
* @private
*/
public getContentString() {
let content = Misc.plainText(this.obj.content)
if (content.length > 20) { content = content.substring(0, 17) + '...' }
return '{' + this.type() + '}' + content
}
// /**
// * @private
// */
// public toStringDigest() {
// const text = Misc.digestEmoji(this.obj.digest)
// return '{' + this.typeEx() + '}' + text
// }
// /**
// * @private
// */
// public getSenderString() {
// const from = PuppeteerContact.load(this.obj.from)
// from.puppet = this.puppet
// const fromName = from.name()
// const roomTopic = this.obj.room
// ? (':' + PuppeteerRoom.load(this.obj.room).topic())
// : ''
// return `<${fromName}${roomTopic}>`
// }
// /**
// * @private
// */
// public getContentString() {
// let content = Misc.plainText(this.obj.content)
// if (content.length > 20) { content = content.substring(0, 17) + '...' }
// return '{' + this.type() + '}' + content
// }
/**
* @private
......@@ -347,7 +347,43 @@ export class PuppeteerMessage extends Message {
*/
public type(): MsgType {
log.silly('PuppeteerMessage', 'type() = %s', MsgType[this.obj.type])
return this.obj.type || MsgType.TEXT
/**
* 1. A message created with rawObj
*/
if (this.obj.type) {
return this.obj.type
}
/**
* 2. A message created with TEXT
*/
const ext = this.extFromFile()
if (!ext) {
return MsgType.TEXT
}
/**
* 3. A message created with local file
*/
switch (ext.toLowerCase()) {
case '.bmp':
case '.jpg':
case '.jpeg':
case '.png':
return MsgType.IMAGE
case '.gif':
return MsgType.EMOTICON
case '.mp4':
return MsgType.VIDEO
case '.mp3':
return MsgType.VOICE
}
throw new Error('unknown type: ' + ext)
}
/**
......@@ -378,12 +414,12 @@ export class PuppeteerMessage extends Message {
return this.rawObj.AppMsgType
}
/**
* Get the typeEx from the message.
*
* @returns {MsgType}
*/
public typeEx() { return MsgType[this.obj.type] }
// /**
// * Get the typeEx from the message.
// *
// * @returns {MsgType}
// */
// public typeEx() { return MsgType[this.obj.type] }
/**
* Check if a message is sent by self.
......@@ -608,38 +644,38 @@ export class PuppeteerMessage extends Message {
return this
}
/**
* @private
*/
public get(prop: string): string {
log.warn('PuppeteerMessage', 'DEPRECATED get() at %s', new Error('stack').stack)
if (!prop || !(prop in this.obj)) {
const s = '[' + Object.keys(this.obj).join(',') + ']'
throw new Error(`Message.get(${prop}) must be in: ${s}`)
}
return this.obj[prop]
}
/**
* @private
*/
public set(prop: string, value: string): this {
log.warn('PuppeteerMessage', 'DEPRECATED set() at %s', new Error('stack').stack)
if (typeof value !== 'string') {
throw new Error('value must be string, we got: ' + typeof value)
}
this.obj[prop] = value
return this
}
// /**
// * @private
// */
// public get(prop: string): string {
// log.warn('PuppeteerMessage', 'DEPRECATED get() at %s', new Error('stack').stack)
// if (!prop || !(prop in this.obj)) {
// const s = '[' + Object.keys(this.obj).join(',') + ']'
// throw new Error(`Message.get(${prop}) must be in: ${s}`)
// }
// return this.obj[prop]
// }
// /**
// * @private
// */
// public set(prop: string, value: string): this {
// log.warn('PuppeteerMessage', 'DEPRECATED set() at %s', new Error('stack').stack)
// if (typeof value !== 'string') {
// throw new Error('value must be string, we got: ' + typeof value)
// }
// this.obj[prop] = value
// return this
// }
/**
* @private
*/
public dump() {
console.error('======= dump message =======')
Object.keys(this.obj).forEach(k => console.error(`${k}: ${this.obj[k]}`))
Object.keys(this.obj!).forEach(k => console.error(`${k}: ${this.obj![k]}`))
}
/**
......@@ -648,7 +684,7 @@ export class PuppeteerMessage extends Message {
public dumpRaw() {
console.error('======= dump raw message =======')
if (!this.rawObj) {
throw new Error('no this.obj')
throw new Error('no this.rawObj')
}
Object.keys(this.rawObj).forEach(k => console.error(`${k}: ${this.rawObj && this.rawObj[k]}`))
}
......@@ -729,13 +765,16 @@ export class PuppeteerMessage extends Message {
* }
* })
*/
public filename(): string {
public filename(): string | null {
log.verbose('PuppeteerMessage', 'filename()')
if (this.parsedPath) {
// https://nodejs.org/api/path.html#path_path_parse_path
const filename = path.join(
this.parsedPath!.dir || '',
this.parsedPath!.base || '',
)
log.silly('PuppeteerMessage', 'filename()=%s, build from parsedPath', filename)
return filename
}
......@@ -747,10 +786,12 @@ export class PuppeteerMessage extends Message {
const ext = this.rawObj.MMAppMsgFileExt || this.ext()
filename += '.' + ext
}
log.silly('PuppeteerMessage', 'filename()=%s, build from rawObj', filename)
return filename
}
throw new Error('no rawObj')
return null
}
......@@ -766,38 +807,75 @@ export class PuppeteerMessage extends Message {
* })
*/
public ext(): string {
if (this.parsedPath && this.parsedPath.ext)
const fileExt = this.extFromFile()
if (fileExt) {
return fileExt
}
const typeExt = this.extFromType()
if (typeExt) {
return typeExt
}
throw new Error('unknown ext()')
}
private extFromFile(): string | null {
if (this.parsedPath && this.parsedPath.ext) {
return this.parsedPath.ext
}
return null
}
private extFromType(): string {
let ext: string
const type = this.type()
switch (this.type()) {
switch (type) {
case MsgType.EMOTICON:
return '.gif'
ext = '.gif'
break
case MsgType.IMAGE:
return '.jpg'
ext = '.jpg'
break
case MsgType.VIDEO:
case MsgType.MICROVIDEO:
return '.mp4'
ext = '.mp4'
break
case MsgType.VOICE:
return '.mp3'
ext = '.mp3'
break
case MsgType.APP:
switch (this.typeApp()) {
case AppMsgType.URL:
return '.url' // XXX
ext = '.url' // XXX
break
default:
ext = '.' + this.type()
break
}
break
case MsgType.TEXT:
if (this.typeSub() === MsgType.LOCATION) {
return '.jpg'
ext = '.jpg'
}
ext = '.' + this.type()
break
default:
log.silly('PuppeteerMessage', `ext() got unknown type: ${this.type()}`)
ext = '.' + this.type()
}
log.silly('PuppeteerMessage', `ext() got unknown type: ${this.type()}`)
return String('.' + this.type())
return ext
}
/**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册