Working Principle of Open Source |ns4_chatbot Communication Component

Introduction: Yixin will officially open the nextsystem4 (hereinafter referred to as “NS4”) series module on March 29, 2019. This open source NS4 series module is a distributed business system solution generated around the current payment system, which is cumbersome, highly code coupled and costly to maintain. NS4 series framework allows the creation of complex processes/business flows. The implementation of business service nodes can be connected in series and distributed. Its simplicity and light weight make it possible to “decapsulate” (independent of tomcat, jetty and other containers). The design concept of NS4 series framework is to separate business and logic. Developers can realize a business system with complex logic, high performance and stable function by simple configuration and business implementation.Click to view the overall introduction of the framework.

NS4 series includes 4 open source modules, namely: ns4_frame Distributed Service Framework(For details, click to view: Open Source |ns4_frame Distributed Service Framework Development Guide), NS4_gear_idgen ID generator component (ns4 framework Demo example)(click for details: open source | why use ns4_gear_idgen ID generator? ), ns4_gear_watchdog monitoring system components (service guard, application performance monitoring, data acquisition, automatic alarm system) and ns4_chatbot communication components.This article will focus on the working principle of ns4_chatbot communication module.

Project open source address:https://github.com/newsettle/ …

I introduction of the project

Ns4_chatbot is a chat framework for a chat robot that integrates qqbot, wxchat, rasa and web services. Provide WeChat and QQ chat interface, which can send system monitoring messages to a certain group. Neither QQ nor WeChat chat robots can send messages directly to non-friend users in the group. They can only remind them through the @ method.

WeChat chat robot

It is open source.wxpyRobot frame. The python wapper mechanism of @register is used to bind the message response function. WeChat robots need to register groups to send messages.

QQ chat robot

Use cool q scheme. QQ is a program under windows that can simulate QQ. Wine technology is adopted in linux system. It needs to be deployed separately as a docker, and then exposed through an http interface. Various http interfaces are used to let us send messages. Provides http message callbacks.

Second, the global directory structure

Three, realize the function

  • Accept system calls from internal systems (such as monitoring systems) to push messages to QQ or WeChat users. When the internal system calls the service, the following information shall be provided

    • To which group
    • Send to that user in this group
    • Messages sent
  • Can accept QQ, WeChat users’ dialogue, understand their intentions, and respond to users.

IV. Project Deployment & Installation

This project is mainly aimed at linux system and uses python2.7+ environment for deployment description.

4.1 environment-dependent installation

  • Installation of required dependency package: take ns4_chatbot/requirements.txt file in the project and run pipinstall–r requirements.txt command
  • Install redis
  • Install mysql database, import ns4 _ chatbot/import _ data/production/DDL. SQL file

4.2 installation of qq chat robot

Kuq can now run in Wine. see kuqair/pro on wine, so there will naturally be a corresponding Docker mirror coolq/wine-coolq.
To use this plug-in in Docker, you can use Kuq’s official Docker image and install the plug-in in it (download cpk, edit configuration file, enable plug-in), or you can use the maintained image richardchien/cqhttp (based on Kuq’s official image modification) that has installed and enabled the plug-in.

The following describes the use of this mirror.

  • Docker installation

sudo apt-get install -y docker.io

  • Qq installation
$ docker pull richardchien/cqhttp:latest $ mkdir coolq
# 用于存储酷 Q 的程序文件
$ docker run -ti --rm --name cqhttp-test \
# 将宿主目录挂载到容器内用于持久化酷 Q 的程序文件
-v $(pwd)/coolq:/home/user/coolq \
-p 9000:9000 \ # noVNC 端口,用于从浏览器控制酷 Q
-p 5700:5700 \ # HTTP API 插件开放的端口
-e COOLQ_ACCOUNT=123456 \ # 要登录的 QQ 账号,可选但建议填
-e CQHTTP_POST_URL=http://example.com:8080 \ # 事件上报地址
# 允许通过 HTTP 接口访问酷 Q 数据文件 -e CQHTTP_SERVE_DATA_FILES=yes \ richardchien/cqhttp:latest
  • Configure cool q callback address:
在/coolq/app/io.github.richardchien.coolqhttpapi/config/<QQ_ID>.ini 中添加 post_url=http://[IP]:8080/coolq_callback 
  • Qq startup:

Enter in the browserhttp://[ip]:5700, enter the following page:


Click the link in the figure to enter the following page. The password is the password corresponding to COOLQ_ACCOUNT.

Sign in to QQ, enter qq password, etc.

Login successful interface.

Check the log.

V. Project Configuration & Startup

5.1 configure bot.conf files

bot=wechat,qq #启动微信和QQ功能
bot_chat_base_inteval=1 #设置消息的最少发送间隔,防止微信和QQ被封 bot_chat_interval=3 #在bot_chat_base_inteval的基础上随机停顿的时间 retry_max_num=10 #微信或者QQ掉线后尝试重新登录的次数
retry_interval = 60 #尝试重新登录的时间间隔
retry_sleep=600 #尝试10次后,没有登录成功,就彻底休息10分钟
debug = True #是否打开调试日志
[admin] email #微信二维码和错误日志发送的邮箱地址,支持多个邮箱,多个邮箱
地址以逗号隔开
[email]模块 #配置发送邮箱的用户名密码等,注意,此邮箱需要开通 smtp 协议 [http]模块
port=8080 #配置http服务监听地址端口
[db] 模块 #配置数据库的用户名、密码等相关信息 [redis]模块 #配置redis的ip、端口和密码等信息 [wxbot]模块
console_qr = True #是否在日志窗口打印微信登录二维码
cache_path = .cache #缓存地址,存放系统产生的图片,二维码等信息 cache_file = wxpy.pkl #存放微信记住密码等文件
qr_path = .cache/qr.png #存放微信二维码
[coolq]模块 #配置酷Q相关的信息 url=http://127.0.0.1:5700/ #配置登录酷 Q 的地址
qq=12345678 #配置酷Q的登录账号
cache_path = [workspace]/coolqdata/image #配置酷 Q 的图片位置,必须配 置到 coolq 的运行目录的 data/image,这样 CQ 码发送的时候图片的时候才能生效
[voice2txt] # #科大讯飞|百度的声音转文本配置 [rasa]
#NLU 自然语言意图识别模型
nlu_model = rasa/model/default/latest #对话模型
dialog_model = rasa/model/dialogue

5.2 Database Configuration

Define all possible QQ group ID and WeChat group names in the database biz_system_tree. If not, there may be an error that the corresponding group cannot be found.

5.3 startup

Under ns4_chatbot directory, run nohup python Main.py to start the project. Then go to the configured mailbox for WeChat code scanning.

VI. Description of Interface Call

Send message interface address [post]:https://&lt; Ip>:<port>/chat request parameters:

Source of content:Yixin Institute of Technology