How to Synchronise a Slack Channel with a WeChat Group

You want to keep messages in sync between WeChat and Slack? Here is a bot for you!

Xiangyu John Wu
MadGinger Asia
6 min readSep 16, 2018

--

Sync WeChat and Slack

The Result of Following This Guide

Except for the deprecated feature of Google Translate, you should be able to get the following result.

https://www.youtube.com/watch?v=cpermWDXwcE

Prerequisites

  1. Slack workspace. If you do not have one go to Slack and create a new workspace.
  2. A Slack channel to synchronise with a WeChat group.
  3. A WeChat account. This WeChat account is invited into a single WeChat group. Make sure that it does not belong to any other groups. The group, where it is a member, is the one with which you wish to sync messages on the Slack channel.
  4. You have Python 2 installed on your computer.
  5. You have virtualenv installed.

Create Slack Bot

Go to https://api.slack.com/apps and sign in to your workspace.

This is the Slack App Interface. You can create and modify your own apps here.

Click on the button “Create New App”.

Interface to create your own custom apps in Slack

Give it a name, e.g. test1.

For the purpose of this tutorial, I will use “test1” as the name for this bot. You can choose any name you wish.

Click on the button “Create App”.

You see here different categories of apps in Slack

Under “Settings > Basic Information > Add features and functionality” choose “Bots”. As you can see here, a bot is just a special kind of app in Slack.

Page to add a bot user in Slack.

Click on the button “Add a Bot User”.

Enter the “Display name” to be the same as your bot’s name. Here, I entered “test1” for both the “Display name” and the “Default username”. Toggle on the “Always Show My Bot as Online” option.

Hit the button “Add Bot User”.

The Basic Information page for easy installation of your bot.

Go back to “Settings > Basic Information” and click on “Install your app to your workspace”. Then click on “Authorize”.

In this tutorial, we do not go through how to distribute your bot to the public. Our bot will remain private for now.

Your bot’s name will appear under the lowest section “App”.

Once you have installed your bot, you should be able to see on the left panel the name of your bot with a green dot to its left. In our case here, it is “test1”. Check if that is true. If so, please continue.

Bot’s token page

Go to “Settings > Install App > OAuth Tokens for Your Team” and copy the “Bot User OAuth Access Token”. Make a note of it for later use. From now on, I will refer to it as the “token”.

For security reasons, make sure that you do not share this token to anyone. If anyone gets hold of your token, they can control your bot and make all sorts of disasters for you. If you are not sure if you shared it accidentally, you can reinstall your bot and get a new token.

Now, let us go to the part in your command line.

Installation

In your command line type:

git clone https://github.com/byteknacker/unichat.git
cd <project_directory>
virtualenv --no-site-packages venv
source venv/bin/activate
git submodule init
git submodule update
pip install -r requirements.txt
pip install ItChat/
pip install python-slackclient/

Run Bot

Now it is time to run the bot.

In your command line, type:

python main.py <token> <slack_channel_name>

The <token> is the very same token I mentioned before that you got from the bot in Slack. The <slack_channel_name> is the exact name of your Slack channel you wish to sync with a WeChat group. Please do not add any quotes or the actual brackets. There is a space between the token and the name of your Slack channel.

After you ran this command, you will be shown in a few seconds a QR code from WeChat. Now go to your WeChat app on your mobile phone, sign in to the WeChat account you created, click on the top right corner the “+” sign and choose the option “Scan”. This will give you a QR code scanner. Use that scanner to scan the QR code that will appear on your computer. Confirm login on your mobile app and wait to see the following message in your command line.

Login successfully as <your_wechat_username>
(many lines of code)...

This confirms that your bot is working. Now you need to have someone other than your connected WeChat account write a message within a group in WeChat, where you are also a member. Then, any message, files, videos and photos are synchronised between your Slack channel and your WeChat group.

Caveat

Currently, you have to wait for someone other than you to message first in a WeChat group. If your WeChat account is a member of some other WeChat groups, then any message triggered in another group would relay further synchronisation to that other group. Your Slack channel remains fixed.

To circumvent this, create a new dedicated WeChat account and get invited into the WeChat group you wish to sync with a Slack channel. Make sure that you only are a member of a single group on WeChat.

This bot has to run on an instance/computer. This means that when you shut down your computer, it will not run there. You need to host it somewhere else for 24/7 access. This may incur some costs but you can make use of the free trial of AWS.

When you first log in to WeChat with this bot, you have to scan the QR code with your WeChat mobile app. This is maybe a hassle if your bot stops running for some reason and you need to reinitiate it. Then you have to manually scan the code. You need an interface to be able to scan the QR code or you can build on top of the code with the feature to send the code per email to you.

The possible reason for this cumbersome problem is that WeChat generates dynamically a new ID for both the user and the group. There is currently no way I can see how to specify in advance the user or group within WeChat. When someone else in the group starts messaging, the bot can then pick up the generated WeChat group ID and then it is possible to identify the correct group. If someone else in another group starts messaging, this group ID will be used and that may be the reason why the WeChat groups can keep changing during synchronisation.

If you wish to raise an issue ticket, please go either to https://github.com/byteknacker/unichat or to the original code from which it is forked: https://github.com/GaaraZhu/unichat

There is also a documentation on the above Github page.

Removal of the Google Translate feature is due to the fact that you have to pay now right from the start to use their API. It used to be free.

If there are a few people requesting some updates, I try to make some time to help along.

Use Cases

  1. Community management of WeChat group directly within Slack
  2. Team members who use both WeChat and Slack do not have to message twice anymore.
  3. Automated file sharing between WeChat and Slack.

Acknowledgement

Many thanks to the https://github.com/GaaraZhu who wrote the code for unichat apparently as a result of a hackathon. unichat is the application on which the code here presented is based and it is just a slight modification of the original. Many thanks to the people who worked on the required libraries of this bot. Also, I am grateful for the support of my colleagues at Madginger, who encouraged me to write this bot in the first place.

--

--