自从公众号列表页改版以来,不少人都说会弱化公众号菜单的做用。php
并且,对于我的号来讲,开发模式下是不能操做菜单开发的。python
因此咱们索性「放弃菜单」,制做「自动回复」来替代菜单功能。laravel
开发「自动回复」功能,本文特推荐两个工具:git
- EasyWeChat
微信开发,从未如此简单
每个功能设计,都通过精心打磨,只为了提供更好的开发体验程序员
在个人「Laravel 学习图谱」https://github.com/fanly/laravel-awesome中,把这个 EasyWeChat 做为首推,值得你们一试。flask
- ChatterBot
ChatterBot is a Python library that makes it easy to generate automated responses to a user’s input. api
注: 上图来自 ChatterBot 网站微信
下面简述对这两个工具的使用,来构建咱们的「自动回复」功能。
正如其官网所述的那样,只要简单引入,几步就能够开发公众号管理系统了。
1. 安装 EasyWeChat 插件 composer require "overtrue/laravel-wechat:~4.0" 2. 添加配置文件 php artisan vendor:publish --provider="Overtrue\LaravelWeChat\ServiceProvider" 3. 在 config/wechat.php 配置文件加入公众号参数 4. 添加路由 Route::any('wechat', 'WeChatController@serve'); 5. 增长 WeChatController public function serve() { $app = app('wechat.official_account'); $app->server->push(function ($message) { switch ($message['MsgType']) { case 'text': return $this->getChatBotMessage($message['Content']); break; default: $data = $this->article->random(); if ($data) { return $data->title ."\n" ."https://www.coding01.cn/" .$data->slug; } return '收到其它消息'; break; } }); return $app->server->serve(); }
好了,咱们根据获取的消息的类型,作对应的处理,如,发送的文本消息,则经过 ChatterBot 自动聊天回复;若是是其余的消息,则随机回复一篇咱们的文章。
能够看看效果:
对于「EasyWeChat」其它功能,能够参考官网说明。目前暂时够用,就再也不深刻分析了。
不管国内网,有不少作「自动机器人」的
但对于程序员来讲,使用平台来达到目标,好像显得有点 low,不够装逼。
因此咱们仍是折腾折腾,找一些比较简单又易于扩展的开源代码来用用,并且还能学习扩展,一举多得。
在我读书的时候,知道要实现 AI,主要步骤包含:
而寻找了一圈,发现 ChatterBot 比较合适咱们使用和学习。
固然今天的目标是看如何使用:
使用 pip
安装,仍是很方便:
pip install chatterbot
简单加入几条语句用于训练。
from chatterbot import ChatBot from chatterbot.trainers import ListTrainer chatbot = ChatBot("yemeishuBot") conversation = [ "Hello", "Hi there!", "How are you doing?", "I'm doing great", "That is good to hear", "Thank you.", "You're welcome." ] chatbot.set_trainer(ListTrainer) chatbot.train(conversation) response = chatbot.get_response("How are you doing?") print(response)
看看运行结果:
使用终端输入输出。
from chatterbot import ChatBot from chatterbot.trainers import ListTrainer chatbot = ChatBot( "yemeishuBot", input_adapter="chatterbot.input.TerminalAdapter", output_adapter="chatterbot.output.TerminalAdapter", ) conversation = [ "Hello", "Hi there!", "How are you doing?", "I'm doing great", "That is good to hear", "Thank you.", "You're welcome." ] chatbot.set_trainer(ListTrainer) chatbot.train(conversation) print("Type something to begin...") # The following loop will execute each time the user enters input while True: try: # We pass None to this method because the parameter # is not used by the TerminalAdapter bot_input = chatbot.get_response(None) # Press ctrl-c or ctrl-d on the keyboard to exit except (KeyboardInterrupt, EOFError, SystemExit): break
能够在终端输入,得结果了:
个人公众号,主要针对国内用户,固然要使用中文语料来作智能回复。
from chatterbot import ChatBot chatbot = ChatBot( "yemeishuBot", input_adapter="chatterbot.input.TerminalAdapter", output_adapter="chatterbot.output.TerminalAdapter", trainer='chatterbot.trainers.ChatterBotCorpusTrainer' ) chatbot.train('chatterbot.corpus.chinese') print("Type something to begin...") # The following loop will execute each time the user enters input while True: try: # We pass None to this method because the parameter # is not used by the TerminalAdapter bot_input = chatbot.get_response(None) # Press ctrl-c or ctrl-d on the keyboard to exit except (KeyboardInterrupt, EOFError, SystemExit): break
固然最后,咱们须要作成接口,供多地方使用。
本文推荐使用这个:https://github.com/chamkank/flask-chatterbot
Simple boilerplate for ChatterBot using Flask
安装插件:
pip install -r requirements.txt
在后台运行:
nohup python -u flush.py > flush.log 2>&1 &
这就很简单了,只要在咱们的 PHP
代码中直接调用这个接口便可:
public function getChatBotMessage($content) { $client = new Client(['base_uri' => 'http://localhost:5000']); $response = $client->request('GET', 'get', [ 'query' => ['msg' => $content] ]); return $response->getBody()->getContents(); }
显示效果:略
今天利用 EasyWeChat 和 ChatterBot 简单搭建一个公众号「自动回复机器人」,利用 EasyWeChat 桥接好公众号和机器人。
以后咱们就能够不断完善 ChatterBot 功能,结合系统项目中的文章内容,做为咱们本身的语料作训练,提升机器人的自动回复能力。
固然能够参考微软推出 AI 开发免费电子书,手把手教你构建智能聊天机器人《A Developer’s Guide to Building AI Applications》中的架构来设计:
最后,你也能够试试其余,如基于 tensorflow 的机器人。