Home php教程 php手册 微信公众平台开发(七) 聊天机器人功能开发

微信公众平台开发(七) 聊天机器人功能开发

Jun 13, 2016 am 11:31 AM
superior Function accomplish platform develop WeChat article robot of translate chat

上一篇文章介绍了微信公众平台翻译功能的开发,实现了中、英、日语言互译,在实际生活中也能使用到。在接下来的这一篇文章中,我们将完成一个比较有趣的功能,那就是聊天机器人,可以在你无聊的时候陪你聊天逗你开心。

在这个实验中,我们将调取小黄鸡官方(http://www.simsimi.com/)提供的API,结合抓取小九机器人(http://www.xiaojo.com/)的网页,互相补充。Simsimi 是收费的,但你可以尝试7天测试,每天可以免费使用100条回复;小九机器人可以无限制使用,但前提是官方不屏蔽。

3.1 API & URL

官方API 地址:http://developer.simsimi.com/api 

Request URL:http://sandbox.api.simsimi.com/request.p

这里使用免费版测试,付费版的类似,只是URL地址不同。

3.2 请求示例与参数说明

请求示例:

http://sandbox.api.simsimi.com/request.p?key=your_trial_key<span &lc</span>=en<span &ft</span>=1.0<span &text</span>=hi
Copy after login

参数说明:

Language code,支持的语言,简体中文用ch,繁体中文用zh,英文用en,详细请参考:http://developer.simsimi.com/lclist

  0.0:未过滤(包含诅咒,性内容);

  1.0:过滤不文明字句(暂时只支持韩文)

3.3 返回值分析

result:执行结果返回码

  • 400-Bad Request.
  • 401-Unauthorized.
  • 404-Not found.
  • 500-Server Error.

id:回复的消息id(只有result=100 时才会有此项)

response:回复的消息(只有result=100 时才会有此项)

4.1 注册simsimi 账号

URL: http://developer.simsimi.com/signUp

4.2 激活账号

4.3 获取API Key

5.1 调用小黄鸡API 实现

调用simsim($keyword) 函数处理,将其中的“Your API Key” 换成申请到的API Key。

    <span //</span><span 小黄鸡</span>
    <span public</span> <span function</span> simsim(<span $keyword</span><span ){

        </span><span $key</span>="<span>41250a68-3cb5-43c8-9aa2-d7b3caf519b1</span>"<span ;
        </span><span $url_simsimi</span>="http://sandbox.api.simsimi.com/request.p?key=".<span $key</span>."&lc=ch&ft=0.0&text=".<span $keyword</span><span ;
        
        </span><span $json</span>=<span file_get_contents</span>(<span $url_simsimi</span><span );  <span>// </span><span>把整个文件读入一个字符串中</span>

        </span><span $result</span>=json_decode(<span $json</span>,<span true</span><span );  <span>// </span><span>对JSON 格式的字符串进行编码</span>

        </span><span //</span><span $errorCode=$result['result'];  // 调试用</span>

        <span $response</span>=<span $result</span>['response'<span ];  // 回复的消息

        </span><span if</span>(!<span empty</span>(<span $response</span><span )){
            </span><span return</span> <span $response</span><span ;
        }</span><span else</span><span {
            </span><span $ran</span>=<span rand</span>(1,5<span );
            </span><span switch</span>(<span $ran</span><span ){
                </span><span case</span> 1:
                    <span return</span> "小鸡鸡今天累了,明天再陪你聊天吧。"<span ;
                    </span><span break</span><span ;
                </span><span case</span> 2:
                    <span return</span> "小鸡鸡睡觉喽~~"<span ;
                    </span><span break</span><span ;
                </span><span case</span> 3:
                    <span return</span> "呼呼~~呼呼~~"<span ;
                    </span><span break</span><span ;
                </span><span case</span> 4:
                    <span return</span> "你话好多啊,不跟你聊了"<span ;
                    </span><span break</span><span ;
                </span><span case</span> 5:
                    <span return</span> "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,万代不朽"<span ;
                    </span><span break</span><span ;
                </span><span default</span>:
                    <span return</span> "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,万代不朽"<span ;
                    </span><span break</span><span ;
            }
        }
    }</span>
Copy after login

说明:

因为有时候小黄鸡不回复,所以在simsim() 函数中加入了一个判断,如果$response 不为空,则返回$response;如果$response 为空,则加了一个小小的代码,让其随机回复自定义的消息,这样就可以做到有求必应了。

5.2 调用小九机器人实现

小九机器人不提供API,故只能通过网页抓取,代码如下:

    <span //</span><span 小九机器人</span>
    <span public</span> <span function</span> xiaojo(<span $keyword</span><span ){

        </span><span $curlPost</span>=<span array</span>("chat"=><span $keyword</span><span );
        </span><span $ch</span> = curl_init();<span //</span><span 初始化curl</span>
        curl_setopt(<span $ch</span>, CURLOPT_URL,'http://www.xiaojo.com/bot/chata.php');<span //</span><span 抓取指定网页</span>
        curl_setopt(<span $ch</span>, CURLOPT_HTTPHEADER, <span $header</span><span );
        curl_setopt(</span><span $ch</span>, CURLOPT_HEADER, 0);<span //</span><span 设置header</span>
        curl_setopt(<span $ch</span>, CURLOPT_RETURNTRANSFER, 1);<span //</span><span 要求结果为字符串且输出到屏幕上</span>
        curl_setopt(<span $ch</span>, CURLOPT_POST, 1);<span //</span><span post提交方式</span>
        curl_setopt(<span $ch</span>, CURLOPT_POSTFIELDS, <span $curlPost</span><span );
        </span><span $data</span> = curl_exec(<span $ch</span>);<span //</span><span 运行curl</span>
        curl_close(<span $ch</span><span );
        </span><span if</span>(!<span empty</span>(<span $data</span><span )){
            </span><span return</span> <span $data</span><span ;
        }</span><span else</span><span {
            </span><span $ran</span>=<span rand</span>(1,5<span );
            </span><span switch</span>(<span $ran</span><span ){
                </span><span case</span> 1:
                    <span return</span> "小鸡鸡今天累了,明天再陪你聊天吧。"<span ;
                    </span><span break</span><span ;
                </span><span case</span> 2:
                    <span return</span> "小鸡鸡睡觉喽~~"<span ;
                    </span><span break</span><span ;
                </span><span case</span> 3:
                    <span return</span> "呼呼~~呼呼~~"<span ;
                    </span><span break</span><span ;
                </span><span case</span> 4:
                    <span return</span> "你话好多啊,不跟你聊了"<span ;
                    </span><span break</span><span ;
                </span><span case</span> 5:
                    <span return</span> "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,万代不朽"<span ;
                    </span><span break</span><span ;
                </span><span default</span>:
                    <span return</span> "感谢您关注【卓锦苏州】"."\n"."微信号:zhuojinsz"."\n"."卓越锦绣,万代不朽"<span ;
                    </span><span break</span><span ;
            }
        }
    }</span>
Copy after login

5.3 双龙戏凤

我们还可以将上面的小黄鸡和小九机器人进行整合,具体代码如下:

    <span //</span><span 双龙戏凤</span>
    <span public</span> <span function</span> chatter(<span $keyword</span><span ){

        </span><span $curlPost</span>=<span array</span>("chat"=><span $keyword</span><span );
        </span><span $ch</span> = curl_init();    <span //</span><span 初始化curl</span>
        curl_setopt(<span $ch</span>, CURLOPT_URL,'http://www.xiaojo.com/bot/chata.php');    <span //</span><span 抓取指定网页</span>
        curl_setopt(<span $ch</span>, CURLOPT_HTTPHEADER, <span $header</span><span );
        curl_setopt(</span><span $ch</span>, CURLOPT_HEADER, 0);    <span //</span><span 设置header</span>
        curl_setopt(<span $ch</span>, CURLOPT_RETURNTRANSFER, 1);    <span //</span><span 要求结果为字符串且输出到屏幕上</span>
        curl_setopt(<span $ch</span>, CURLOPT_POST, 1);    <span //</span><span post提交方式</span>
        curl_setopt(<span $ch</span>, CURLOPT_POSTFIELDS, <span $curlPost</span><span );
        </span><span $data</span> = curl_exec(<span $ch</span>);    <span //</span><span 运行curl</span>
        curl_close(<span $ch</span><span );

        </span><span if</span>(!<span empty</span>(<span $data</span><span )){
            </span><span return</span> <span $data</span>." [/::)小九]"<span ;
        }</span><span else</span><span {
            </span><span return</span> <span $this</span>->simsim(<span $keyword</span>)." [simsim/::D]"<span ;
        }
    }</span>
Copy after login

 

请到QQ群213260412共享里下载使用。

请关注 卓锦苏州 微信公众帐号,卓锦苏州 基于SAE 平台开发,针对于主流的微信功能进行开发测试。

您可以关注 卓锦苏州 公众帐号进行功能测试,以及获取新的应用开发。

1. 登录微信客户端,朋友们 -> 添加朋友 -> 搜号码 -> zhuojinsz,查找并关注。

2. 扫描二维码:

卓锦苏州 菜单功能:

目前菜单具体功能还在开发之中,会陆续更新,敬请期待。。。

 


We Believe, Great People Share Knowledge...

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

There are rumors that 'iPhone 16 may not support WeChat', and Apple's technical consultant in China said that it is communicating with Tencent about app store commissions There are rumors that 'iPhone 16 may not support WeChat', and Apple's technical consultant in China said that it is communicating with Tencent about app store commissions Sep 02, 2024 pm 10:45 PM

Thanks to netizens Qing Qiechensi, HH_KK, Satomi Ishihara and Wu Yanzu of South China for submitting clues! According to news on September 2, there are recent rumors that "iPhone 16 may not support WeChat." In response to this, a reporter from Shell Finance called Apple's official hotline. Apple's technical consultant in China responded that whether iOS systems or Apple devices can continue to use WeChat, and WeChat The issue of whether it can continue to be listed and downloaded on the Apple App Store requires communication and discussion between Apple and Tencent to determine the future situation. Software App Store and WeChat Problem Description Software App Store technical consultant pointed out that developers may need to pay fees to put software on the Apple Store. After reaching a certain number of downloads, Apple will need to pay corresponding fees for subsequent downloads. Apple is actively communicating with Tencent,

deepseek image generation tutorial deepseek image generation tutorial Feb 19, 2025 pm 04:15 PM

DeepSeek: A powerful AI image generation tool! DeepSeek itself is not an image generation tool, but its powerful core technology provides underlying support for many AI painting tools. Want to know how to use DeepSeek to generate images indirectly? Please continue reading! Generate images with DeepSeek-based AI tools: The following steps will guide you to use these tools: Launch the AI ​​Painting Tool: Search and open a DeepSeek-based AI Painting Tool (for example, search "Simple AI"). Select the drawing mode: select "AI Drawing" or similar function, and select the image type according to your needs, such as "Anime Avatar", "Landscape"

People familiar with the matter responded that 'WeChat may not support Apple iPhone 16': Rumors are rumors People familiar with the matter responded that 'WeChat may not support Apple iPhone 16': Rumors are rumors Sep 02, 2024 pm 10:43 PM

Rumors of WeChat supporting iPhone 16 were debunked. Thanks to netizens Xi Chuang Jiu Shi and HH_KK for submitting clues! According to news on September 2, there are rumors today that WeChat may not support iPhone 16. Once the iPhone is upgraded to the iOS 18.2 system, it will not be able to use WeChat. According to "Daily Economic News", it was learned from people familiar with the matter that this rumor is a rumor. Apple's response: According to Shell Finance, Apple's technical consultant in China responded that the issue of whether WeChat can continue to be used on iOS systems or Apple devices, and whether WeChat can continue to be listed and downloaded in the Apple App Store, needs to be resolved between Apple and Tencent. Only through communication and discussion can we determine the future situation. Currently, Apple is actively communicating with Tencent to confirm whether Tencent will continue to

Magically modified 'Black Myth: Wukong ' to defeat Midjourney. This AI drawing tool is amazing. Magically modified 'Black Myth: Wukong ' to defeat Midjourney. This AI drawing tool is amazing. Aug 23, 2024 pm 09:42 PM

When AI Ideograms compete for realism and artistic sense, Ideogram has opened up a tricky track: it can accurately generate text on pictures, and the fonts and layouts are beautiful. This demand is not niche. Generate posters and illustrations with one click without using P-pictures. It can save a lot of trouble and is very suitable for ordinary people who know nothing about design. We previously wrote about version 1.0 of Ideogram. On August 21st, version 2.0 came. The realism is better, the posters are more designed, and the special skill of text is also stronger. You may have never heard of it. This is an AI product developed by former Google employees. It has many shortcomings, but the longboard can "overtake" Midjourney in corners. Directions https://ideogram.ai/A

gateio Chinese official website gate.io trading platform website gateio Chinese official website gate.io trading platform website Feb 21, 2025 pm 03:06 PM

Gate.io, a leading cryptocurrency trading platform founded in 2013, provides Chinese users with a complete official Chinese website. The website provides a wide range of services, including spot trading, futures trading and lending, and provides special features such as Chinese interface, rich resources and community support.

gateio exchange app old version gateio exchange app old version download channel gateio exchange app old version gateio exchange app old version download channel Mar 04, 2025 pm 11:36 PM

Gateio Exchange app download channels for old versions, covering official, third-party application markets, forum communities and other channels. It also provides download precautions to help you easily obtain old versions and solve the problems of discomfort in using new versions or device compatibility.

Ouyi Exchange app domestic download tutorial Ouyi Exchange app domestic download tutorial Mar 21, 2025 pm 05:42 PM

This article provides a detailed guide to safe download of Ouyi OKX App in China. Due to restrictions on domestic app stores, users are advised to download the App through the official website of Ouyi OKX, or use the QR code provided by the official website to scan and download. During the download process, be sure to verify the official website address, check the application permissions, perform a security scan after installation, and enable two-factor verification. During use, please abide by local laws and regulations, use a safe network environment, protect account security, be vigilant against fraud, and invest rationally. This article is for reference only and does not constitute investment advice. Digital asset transactions are at your own risk.

Sesame Open Door Login Registration Entrance gate.io Exchange Registration Official Website Entrance Sesame Open Door Login Registration Entrance gate.io Exchange Registration Official Website Entrance Mar 04, 2025 pm 04:51 PM

Gate.io (Sesame Open Door) is the world's leading cryptocurrency trading platform. This article provides a complete tutorial on spot trading of Gate.io. The tutorial covers steps such as account registration and login, KYC certification, fiat currency and digital currency recharge, trading pair selection, limit/market transaction orders, and orders and transaction records viewing, helping you quickly get started on the Gate.io platform for cryptocurrency trading. Whether a beginner or a veteran, you can benefit from this tutorial and easily master the Gate.io trading skills.

See all articles