Most beginners start using chatbots with two basic functions: autoreply and keyword group entry. Here I’ll show you how to quickly implement these two functions using Python-wechaty.

I. Installation & configuration

The Python-wechaty chatbot framework requires only the specified version package and protocol token. The installation and configuration process is very simple, just copy and paste.

1.1 Installing dependency packages

Install the python-wechaty package
pip install wechaty
Install the plugin library
pip install wechaty-plugin-contrib
Copy the code

1.2 configuration Token

Tokens can be configured in a number of ways:

Method 1: Use environment variables to configure

*export* WECHATY_PUPPET_HOSTIE_TOKEN='your-token'
Copy the code

Method 2: Use Python code to configure

import os
os.environ['WECHATY_PUPPET_HOSTIE_TOKEN'] = 'your-token'
Copy the code

So how do you get long-term tokens? For details, see: everything-about-wechaty

Two, the keyword into the group

Developing keyword clustering based on python-wechaty is very easy, just use the plug-in library: RoomInviterPlugin.

The usage method is as follows:

import asyncio
from typing import Dict
from wechaty import Wechaty
from wechaty_plugin_contrib.contrib import (
    RoomInviterOptions,
    RoomInviterPlugin
)
from wechaty_plugin_contrib.matchers import (
    MessageMatcher,
    RoomMatcher
)

async def run() :
    """async run method"""
    rules: Dict[MessageMatcher, RoomMatcher] = {
        MessageMatcher('wechaty'): RoomMatcher('Wechaty Developer Group (1) '),
				MessageMatcher('python-wechaty'): RoomMatcher('Python-wechaty Developer Group (2)')
    }
    plugin = RoomInviterPlugin(options=RoomInviterOptions(
        name='Python-wechaty keyword group entry plugin',
        rules=rules,
        welcome='Welcome to the group.'
    ))
    bot = Wechaty().use(plugin)
    await bot.start()

asyncio.run(run())
Copy the code

In the premise of token, the above code can be copied and pasted to develop a keyword into the group chat bot, isn’t it very simple?

Automatic reply

Automatic reply is also used frequently in our daily life and work, and the reply content is not limited to text, but also can be pictures, files, links and small programs.

The sample code looks like this:

import asyncio
from wechaty import Wechaty, MiniProgram  # type: ignore
from wechaty_puppet import (    # type: ignore
    FileBox
)

from wechaty_plugin_contrib import (
    AutoReplyRule,
    AutoReplyPlugin,
    AutoReplyOptions,
)

from wechaty_plugin_contrib.matchers import ContactMatcher

async def run() :
    """async run method"""
    img_url = 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy' \
              '/ it/u = 1257042014316688, 936 & FM = 26 & gp = 0. JPG'
    plugin = AutoReplyPlugin(options=AutoReplyOptions(
        rules=[
            AutoReplyRule(keyword='ding', reply_content='dong'),
            AutoReplyRule(keyword=Dragon Ball, reply_content=Dragon Ball),
            AutoReplyRule(
                keyword=Dragon Ball,
                reply_content=FileBox.from_url(img_url, name='python.png')
            ),
            AutoReplyRule(
                keyword='netease - Li Bai', reply_content=MiniProgram.create_from_json({... }) ) ], matchers=[ ContactMatcher('fall off'),
        ]
    ))
    bot = Wechaty().use(plugin)
    await bot.start()

asyncio.run(run())
Copy the code

The code is very simple (API design is very human), I believe you can understand at a glance, I won’t explain too much here.

Four,

Python-wechaty has a user-friendly API and a built-in library of plugins that allow developers to quickly develop their own small applications.

The goal of Wechaty is to create a universal chatbot framework for all IM platforms, and we welcome you to follow and use the Python-Wechaty framework.