本文整理匯總了TypeScript中file-box.FileBox.fromStream方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript FileBox.fromStream方法的具體用法?TypeScript FileBox.fromStream怎麽用?TypeScript FileBox.fromStream使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類file-box.FileBox
的用法示例。
在下文中一共展示了FileBox.fromStream方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: contactAvatar
public async contactAvatar(contactId: string, file?: FileBox): Promise<void | FileBox> {
log.verbose('PuppetWechat4u', 'contactAvatar(%s)', contactId)
if (file) {
throw new Error('not supported')
}
const rawPayload = await this.contactRawPayload(contactId)
const res = await this.wechat4u.getHeadImg(rawPayload.HeadImgUrl)
/**
* 如何獲取聯係人頭像
*/
return FileBox.fromStream(
res.data,
`${contactId}.jpg`, // FIXME
)
}
示例2: catch
.on('message', async msg => {
try {
console.log(msg.toString())
if (/^(ding|ping|bing|code)$/i.test(msg.text()) /*&& !msg.self()*/) {
/**
* 1. reply 'dong'
*/
log.info('Bot', 'REPLY: dong')
await msg.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 msg.say(joinWechaty)
/**
* 2. reply qrcode image
*/
// const fileBox = FileBox.packLocal(BOT_QR_CODE_IMAGE_FILE)
const fileBox = FileBox.fromStream(
fs.createReadStream(BOT_QR_CODE_IMAGE_FILE),
BOT_QR_CODE_IMAGE_FILE,
)
log.info('Bot', 'REPLY: %s', fileBox.toString())
await msg.say(fileBox)
/**
* 3. reply 'scan now!'
*/
await msg.say('Scan now, because other Wechaty developers want to talk with you too!\n\n(secret code: wechaty)')
}
} catch (e) {
log.error('Bot', 'on(message) exception: %s' , e)
console.error(e)
}
})
示例3: messageFile
/**
*
* Message
*
*/
public async messageFile(id: string): Promise<FileBox> {
log.verbose('PuppetWechat4u', 'messageFile(%s)', id)
const payload = await this.messagePayload(id)
const rawPayload = await this.messageRawPayload(id)
const filename = payload.filename || 'unknown.txt'
/**
* 判斷消息類型
*/
switch (rawPayload.MsgType) {
case this.wechat4u.CONF.MSGTYPE_TEXT:
/**
* 文本消息
*/
throw new Error('msg type is text')
case this.wechat4u.CONF.MSGTYPE_EMOTICON:
/**
* 表情消息
*/
case this.wechat4u.CONF.MSGTYPE_IMAGE:
/**
* 圖片消息
*/
// console.log('圖片消息,保存到本地')
return FileBox.fromStream(
(await this.wechat4u.getMsgImg(rawPayload.MsgId)).data,
filename,
)
case this.wechat4u.CONF.MSGTYPE_VOICE:
/**
* 語音消息
*/
// console.log('語音消息,保存到本地')
return FileBox.fromStream(
(await this.wechat4u.getVoice(rawPayload.MsgId)).data,
filename,
)
case this.wechat4u.CONF.MSGTYPE_VIDEO:
case this.wechat4u.CONF.MSGTYPE_MICROVIDEO:
/**
* 視頻消息
*/
// console.log('視頻消息,保存到本地')
return FileBox.fromStream(
(await this.wechat4u.getVideo(rawPayload.MsgId)).data,
filename,
)
case this.wechat4u.CONF.MSGTYPE_APP:
if (rawPayload.AppMsgType === 6) {
/**
* 文件消息
*/
// console.log('文件消息,保存到本地')
return FileBox.fromStream(
(await this.wechat4u.getDoc(rawPayload.FromUserName, rawPayload.MediaId, rawPayload.FileName)).data,
filename,
)
}
break
default:
break
}
throw new Error('unsupported message. id: ' + id)
}