跳到主要内容

Puppet Service: DIY

Wechaty Puppet Service DIY

Do It Yourself

You can make a Wechaty Puppet Service easily from any Wechaty Puppet Providers.

You can build a Wechaty Puppet Service by yourself with any Wechaty Puppet Providers.

Steps

We have a easy to use docker image which works out-of-the-box.

1. Pull the latest Wechaty Docker Image

docker pull wechaty/wechaty

2. Config Wechaty Puppet Provider

We need to choice which Wechaty Puppet Provider we want to use by setting the WECHATY_PUPPET environment variable.

For example, you can choose wechaty-puppet-wechat by setting WECHATY_PUPPET=wechaty-puppet-wechat.

Wechaty Puppet Providers

Learn all Wechaty Puppet Providers

You need to set all environment variables which requires from a specific provider.

For example, an additional token will be required by PadLocal: WECHATY_PUPPET_PADLOCAL_TOKEN=puppet_padlocal${TOKEN}

export WECHATY_PUPPET="wechaty-puppet-wechat"

3. Create your own Wechaty Puppet Service TOKEN

In order to provide Wechaty Puppet Service, you need to specify a TOKEN for authorization.

You can Generate a new UUIDv4 online, use this new UUIDv4 as your token.

TOKEN uniqueness

Your new token MUST different to any existing tokens in our system. (or they will conflict!)

export WECHATY_TOKEN=$(curl -s https://www.uuidgenerator.net/api/version4)
echo "WECHATY_TOKEN=$WECHATY_TOKEN"

4. Set Wechaty Puppet Service port

The port for your Wechaty Puppet Service need to be specified. Make sure it's free on your server.

port availablility
  1. The server IP must be public on the internet
  2. the port must be public accessible on the internet
export WECHATY_PUPPET_SERVER_PORT="8788"

5. Set Wechaty Log Level

Enable verbose log message output for easy debugging.

More options are:

  1. silly: all debug messages will be outputed
  2. verbose: recommended debug level
  3. info: disable debug messages
  4. warning: only warning message
  5. silence: no log message
export WECHATY_LOG="verbose"

6. Config TLS(SSL) for Wechaty Puppet Service (optional)

From Wechaty version 0.67, the Puppet Service will enable TLS(SSL) by default. (See wechaty/wechaty-puppet-service#160)

You can enable/disable the TLS by setting environment variables to fullfil your needs.

For example, if you need to provide a Wechaty Puppet Service token without TLS, then you can set WECHATY_PUPPET_SERVICE_NO_TLS_INSECURE_SERVER=true to disable TLS.

# set to "true" to disable TLS (not recommanded)
export WECHATY_PUPPET_SERVICE_NO_TLS_INSECURE_SERVER="false"

7. Start your Wechaty Token Gate Server

After finish config all the above settings, start the token gate server with the following docker command:

docker run -ti \
--name wechaty_puppet_service_token_gateway \
--rm \
--privileged \
--network=host \
-e WECHATY_LOG \
-e WECHATY_PUPPET \
-e WECHATY_PUPPET_SERVER_PORT \
-e WECHATY_PUPPET_SERVICE_NO_TLS_INSECURE_SERVER \
-e WECHATY_TOKEN \
wechaty/wechaty
privileged mode

Privileged mode is for using host networking to simplify the docker arguments.

If you want to remove the --privileged, you need to add:

  • -p $WECHATY_PUPPET_SERVER_PORT:$WECHATY_PUPPET_SERVER_PORT for Linux & Mac
  • -p %WECHATY_PUPPET_SERVER_PORT%:%WECHATY_PUPPET_SERVER_PORT% for Windows

8. Check your TOKEN service

wait for token gateway getting full started

The docker command in the previous step might need some time to getting fully started.

Wait and read the docker container log messages carefully to make sure the server has been started before continue this step.

Check your TOKEN availability by visiting https://api.chatie.io/v0/hosties/${WECHATY_TOKEN}

echo HTTP/$(curl -s -o /dev/null -w '%{http_code}' https://api.chatie.io/v0/hosties/${WECHATY_TOKEN})
  1. Succ: HTTP/200 means you are good, the TOKEN is ready to use.
  2. Fail: HTTP/404 means the TOKEN is not registered successfully.

If you get HTTP/404, then you need to check the previous steps and troubleshoot which part has problems. If you find anything need to be reported, please feel free to submit an issue at here

Using wechaty-token CLI

You can use wechaty-token CLI command to check the TOKEN status.

$ npm install --global wechaty-token
+ wechaty-token@0.4.3
updated 1 package in 2.654s

$ wechaty-token --help
wechaty-token <subcommand>
> Wechaty utility for discovering and generating tokens

where <subcommand> can be one of:

- generate - Generate a new Wechaty Token
- discover - Wechaty TOKEN Service Discovery

For more help, try running `wechaty-token <subcommand> --help`

$ wechaty-token discover puppet_not_exist_token
NotFound

$ wechaty-token discover ${WECHATY_TOKEN}
{ host: '1.2.3.4', port: 5678 }

Learn more about the TOKEN from Wechaty Puppet Service TOKEN Specification.

🎉 Congratulations! You are all set

Your Wechaty Puppet Service will be ready to service for Polyglot Wechaty!

All in One Command

For use Wechaty Token Gateway with ease, you can copy/paste the following code (with modifications for your need) in your bash shell:

# Step 1
docker pull wechaty/wechaty

# Step 2
# here we are using wechaty-puppet-wechat for example
# replace it to fit your needs
export WECHATY_PUPPET=wechaty-puppet-wechat

# Step 3
export WECHATY_TOKEN=$(curl -s https://www.uuidgenerator.net/api/version4)
echo "WECHATY_TOKEN=$WECHATY_TOKEN"

# Step 4
export WECHATY_PUPPET_SERVER_PORT=8788

# Step 5
export WECHATY_LOG="verbose"

# Step 6
docker run \
-d \
-ti \
--name wechaty_puppet_service_token_gateway \
--rm \
--privileged \
--network=host \
-e WECHATY_LOG \
-e WECHATY_PUPPET \
-e WECHATY_PUPPET_SERVER_PORT \
-e WECHATY_TOKEN \
wechaty/wechaty

# Step 7
export HTTP_CODE=$(curl -s -o /dev/null -w '%{http_code}' https://api.chatie.io/v0/hosties/${WECHATY_TOKEN})
if [ "$HTTP_CODE" == 200 ]; then
echo "Wechaty Puppet Service TOKEN ${WECHATY_TOKEN} online status:"
curl https://api.chatie.io/v0/hosties/${WECHATY_TOKEN}
echo
else
>&2 echo "ERROR: Wechaty Puppet Service TOKEN ${WECHATY_TOKEN} out of service"
fi

# Step 8 🎉

I hope you enjoy it!

Blogs

History

  1. Using your Puppet PadLocal token with Python, Java, and Go Wechaty wechaty/wechaty#1985
  2. How to create your own Wechaty Puppet Service Token with the Web Protocol wechaty/wechaty#1986
  3. Using PadLocal Token with Polyglot Wechaty via Token Gateway wechaty/puppet-services#84
  4. Wechaty is All You Need: Python, Go, and Java Translation Project wechaty/wechaty#1927

Support

You can join our Gitter network if you aren’t already a member.