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.
- TypeScript
- JavaScript
- Python
- Go
- Java
- PHP
- Scala
- C#
- Rust
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)
const { FileBox } = require('wechaty')
async function onMessage (message) {
const fileBox = FileBox.fromUrl('https://wechaty.js.org/img/icon.png')
await bot.say(fileBox)
}
bot.on('message', onMessage)
from wechaty import FileBox
fileBox = FileBox.from_url('https://wechaty.js.org/img/icon.png')
await bot.say(fileBox)
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
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.
- TypeScript
- JavaScript
- Python
- Go
- Java
- PHP
- Scala
- C#
- Rust
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)
const { Message } = require('wechaty')
async function onMessage (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)
from wechaty_puppet import FileBox
from wechaty import Wechaty, Contact, Message
class MyBot(Wechaty):
async def on_message(self, msg: Message):
if msg.type() == MessageType.MESSAGE_TYPE_IMAGE:
image_file_box = await msg.to_file_box()
print(f'saving file<{image_file_box.name}>')
await image_file_box.to_file('/path/to/local/file')
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!
// TODO: Pull Request is welcome!