Skip to main content

Usage with Docker

DOCKER WECHATY GETTING STARTED

Powered by Wechaty Docker Pulls Docker Stars Docker Layers

dockeri.co

Deploy to Docker for Wechaty Starter Project Repository

FEATURES

  1. Wechaty is fully dockerized. So it will be very easy to be used as a MicroService.
  2. Clone this repository, then you will be able to use Docker to run Wechaty with ZERO configuration.

REQUIREMENTS

  1. Docker
  2. Global Internet Connection

Install Docker

Quick & easy install Docker via:

curl -sSL https://get.docker.com | sh

Or

wget -qO- https://get.docker.com/ | sh

Run

The best practice of using Wechaty Docker is like the following:

$ cat > mybot.ts <<'EOF'
import { Wechaty } from 'wechaty'

Wechaty.instance() // Singleton
.on('scan', (qrcode, status) => console.log(`Scan QrCode to login: ${status}\n${qrcode}`))
.on('login', user => console.log(`User ${user} logined`))
.on('message', message => console.log(`Message: ${message}`))
.start()
EOF

$ function wechaty() {
docker run \
-t -i --rm \
--privileged \
--network=host \
-e WECHATY_LOG="$WECHATY_LOG" \
-e WECHATY_PUPPET="$WECHATY_PUPPET" \
-e WECHATY_TOKEN="$WECHATY_TOKEN" \
--mount type=bind,source="$(pwd)",target=/bot \
wechaty/wechaty:latest \
"$@"
}

$ wechaty mybot.ts

see? death easy to use!

You might want to confirm that you can download wechaty/wechaty image successfully by run docker pull wechaty/wechaty, and this command is also able to help you upgrade the image to the latest version.

Docker options explanation

  1. -t : Allocate a pseudo-TTY
  2. -i : Keep STDIN open even if not attached
  3. --rm : Automatically remove the container when it exits
  4. --privileged : Give extended privileges to this container
  5. --network=host : use the Docker host network stack
  6. -e WECHATY_LOG="$WECHATY_LOG" : Pass the environment variable WECHATY_LOG into the container
  7. --volume="$(pwd)":/bot : Bind current directory("$(pwd)") to '/bot' inside the container, by mounting the volume
  8. --name=wechaty : Assign wechaty as the container name
  9. wechaty/wechaty:latest : Image name on docker hub, here's our wechaty/wechaty with latest version
  10. mybot.js : File contains code wrote by you, should be placed in current directory ./

Run Examples

There's many Wechaty ChatBot Examples in the example directory, and all of them are writen in TypeScript.

Run example ChatBot is as easy as run hello world example above, as long as you are using Docker:

cd example
wechaty media-file-bot.ts

Bravo!

Run Wechaty as a Hostie

export WECHATY_TOKEN="your token here"

docker run -e WECHATY_TOKEN="$WECHATY_TOKEN" wechaty/wechaty

WECHATY_TOKEN is required here, because you need this key to managing wechaty on the chatbot cloud manager: https://www.chatie.io

Onbuild

Put this line(and only this line) to your Dockerfile:

FROM wechaty/onbuild

This image makes building derivative images easier. For most use cases, creating a Dockerfile in the base of your project directory with the line FROM wechaty/onbuild will be enough to create a stand-alone image for your project.

  1. The onbuild variant is really useful for "getting off the ground running" (zero to Dockerized in a short period of time)
  2. This onbuild variant will only install npm packages according to the package.json
  3. The npm installs devDependencies by default, which is undesirable if you're building a production image. To avoid this pass NODE_ENV as a build argument i.e. docker build --build-arg NODE_ENV=production ….

Build

docker build -t wechaty .

SEE ALSO

  1. Wechaty Getting Started: https://github.com/wechaty/wechaty-getting-started
  2. Heroku Wechaty Getting Started: https://github.com/wechaty/heroku-wechaty-getting-started

AUTHOR

Huan LI \zixia@zixia.net\

Profile of Huan LI (李卓桓) on StackOverflow

  • Code & Docs © 2018 Huan LI \zixia@zixia.net\
  • Code released under the Apache-2.0 License
  • Docs released under Creative Commons