Ducks Proposal Style
The Ducks Proposal Style is using the Wechaty Redux plugin with the Ducks package. It helps to use the Redux pattern in a simple and concise way.
In this tutorial, you will learn how to use Redux with Wechaty using the Ducks Proposal Style.
Requirements
- Node.js v16+
- Wechaty v0.40+
- Wechaty Redux
- Ducks
Getting started
Before getting started, make sure you have Node.js
installed on your system. If you do not have Node.js
installed (or have a version below 12), then you need to install the latest version of Node.js
by following the links below:
Installation guide for
Node.js
on other platforms can be found here.
You can head over to the Building the bot section to learn how to build the bot on your own.
Otherwise, if you just want to try out the bot on your local system, follow the steps below:
1. Clone the repository
Use the following commands to clone the GitHub repository and navigate to the directory:
git clone https://github.com/wechaty/wechaty-redux.git
cd wechaty-redux
2. Install dependencies
You can install the npm
packages required for running the bot, using this command:
npm install
3. Run the bot
First, you have to export/set
the environment variables:
- Linux
- macOS
- Windows
export WECHATY_LOG=verbose
export WECHATY_PUPPET=wechaty-puppet-mock
export WECHATY_LOG=verbose
export WECHATY_PUPPET=wechaty-puppet-mock
set WECHATY_LOG=verbose
set WECHATY_PUPPET=wechaty-puppet-mock
Run the bot using the following command:
npx ts-node examples/ducks-proposal.ts
There are various Wechaty puppets available, you can know more about them here.
Building the bot
Let's get started with building the Wechaty Redux bot using the Ducks package.
1. Initialize project
Create a new folder called redux-bot
and move into that directory:
mkdir redux-bot
cd redux-bot
Use the following command to initialize an npm project:
npm init -y
This will generate the package.json
file containing these:
{
"name": "redux-bot",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
2. Install dependencies
For building the bot, you will require the following dependencies:
- wechaty: Official Wechaty package
- wechaty-redux: Wechaty wrapper with Redux Actions & Reducers
- ducks: For using Ducks API
- ts-node: For running TypeScript file
You can install these dependencies by running the following command:
npm install wechaty
npm install wechaty-redux ducks ts-node
You will also need to add dependencies for using any Wechaty Puppet which helps to integrate Wechaty with various instant messaging (IM) systems (such as WeChat, Whatsapp, and Gitter):
If you want to use WhatsApp, install
wechaty-puppet-whatsapp
:npm install wechaty-puppet-whatsapp
If you want to use WeChat, you can try the following puppets:
Web Protocol: Install
wechaty-puppet-wechat
:npm install wechaty-puppet-wechat
iPad Protocol:
padlocal: Install
wechaty-puppet-padlocal
:npm install wechaty-puppet-padlocal
Then get a token like
puppet_padlocal_XXX
, know more about puppet service padlocal here.paimon: Install
wechaty-puppet-service
:npm install wechaty-puppet-service
Then get a token like
puppet_paimon_XXX
, know more about puppet service paimon here.
If you want to use WeCom, install
wechaty-puppet-service
:npm install wechaty-puppet-service
Then get a token like
puppet_wxwork_XXXXX
, more about puppet service wxwork here.
You can find more information about the puppets here.
3. Write code for bot
Start by creating a new file redux-bot.ts
. We will be writing the code here.
Follow the steps below to build the bot:
Import the required packages in the TypeScript file:
import { Wechaty } from 'wechaty'
import { Ducks } from 'ducks'
import {
WechatyRedux,
Duck,
} from 'wechaty-redux'Define an asynchronous
main()
function, and call it. We will be adding code for the bot in this function.async function main() {
// Add code here
}
main()
.catch(console.error)Initialize the Ducks API:
const ducks = new Ducks({ wechaty: Duck })
const store = ducks.configureStore()Instantiate Wechaty with Redux Plugin:
const bot = Wechaty.instance()
bot.use(WechatyRedux({ store }))Start the bot:
await bot.start()
console.info('Wechaty has started with Redux enabled.')Using Redux Store with Wechaty Ducks API:
const wechatyDuck = ducks.ducksify('wechaty')
store.subscribe(() => console.info(store.getState()))
wechatyDuck.operations.ding(bot.id, 'Ducksify Style ding!')
Running the bot
In order to run the bot, first you have to export/set an environment variable with the type of puppet to use:
- Linux
- macOS
- Windows
export WECHATY_LOG=verbose
export WECHATY_PUPPET=wechaty-puppet-wechat
# For using WhatsApp:
# export WECHATY_PUPPET=wechaty-puppet-whatsapp
# For using WeCom:
# export WECHATY_PUPPET=wechaty-puppet-service
# export WECHATY_PUPPET_SERVICE_TOKEN="puppet_wxwork_XXXXX"
export WECHATY_LOG=verbose
export WECHATY_PUPPET=wechaty-puppet-wechat
# For using WhatsApp:
# export WECHATY_PUPPET=wechaty-puppet-whatsapp
# For using WeCom:
# export WECHATY_PUPPET=wechaty-puppet-service
# export WECHATY_PUPPET_SERVICE_TOKEN="puppet_wxwork_XXXXX"
set WECHATY_LOG=verbose
set WECHATY_PUPPET=wechaty-puppet-wechat
# For using WhatsApp:
# set WECHATY_PUPPET=wechaty-puppet-whatsapp
# For using WeCom:
# set WECHATY_PUPPET=wechaty-puppet-service
# set WECHATY_PUPPET_SERVICE_TOKEN="puppet_wxwork_XXXXX"
If you are using WeCom, you can get token from puppet service wxwork.
Run the bot using the following command:
ts-node redux-bot.ts