Skip to main content

Send and receive files

Use the guide to help you integrate additional functions to an existing project which is present at Github/Media Bot or check that your existing local system will run on Wechaty. If, you wish to learn on how to build the bot on your own, please visit one of our Building the bot section.

The steps outlined here mainly focus on working with Javascript, but the user is free to switch between any languages. All wechaty contacts are encapsulated as a Contact.

Prerequisites

  • Your system must have Node.js installed (version >= 12).
  • Your system must have Wechaty (version >= 0.40).
  • You need to be familiar with the basics of Wechaty platform. If not, follow our tutorials section.
  • You need to have at least a minimal application ready to work, follow one of our Example/Media file bot.
  • You need to have some basic knowledge of FileBox module.

If you don't know where to start from

See Running our first ding-dong bot.

Send File - defines how to send files

This section helps you send files like Attachment, Audio, Image, Video to the desired user. Here, FileBox.fromUrl helps you send URL as a attachment to the user.

import { FileBox } from 'wechaty'

async function onMessage (message) {
const fileBox = FileBox.fromUrl('https://wechaty.js.org/img/icon.png')
await bot.say(fileBox)
}

bot.on('message', onMessage)

Receive File - defines how to receive files

This section helps you receive files like Attachment, Audio, Image, Video. Here, message.toFileBox helps you save all the files to a specified location.

import { Message } from 'wechaty'

async function onMessage (message: Message) {
const fileTypeList = [
Message.Type.Attachment,
Message.Type.Audio,
Message.Type.Image,
Message.Type.Video,
]
if (fileTypeList.includes(message.type())) {
const fileBox = await message.toFileBox()
console.info(`Saving file {$fileBox.name}...`)
await fileBox.toFile()
}
}

bot.on('message', onMessage)

References