19 lines of Python code let you have your own intelligent chatbot

Table of contents

I. Introduction

2. Understand the API

3. Function realization

1. Detailed intelligent robot API interface

2. Third-party library preparation

3. Code implementation

4. Effect display

5. All resources

4. Conclusion


I. Introduction

Nowadays, technology is developing rapidly, and chatbots have been gradually used in all walks of life. Recently, the editor has seen many articles about artificial intelligence on the Internet, and I silently sighed in my heart, how great it would be if one day I could realize an artificial intelligence robot of my own! So I had a sudden idea to call the chatbot API interface to implement the chat function. Some people may ask, why does the editor use API to make a chatbot? Because I am lazy . This article is mainly aimed at novices who have just enrolled in Python, so that they can fully feel the charm of the Python language.

2. Understand the API

Before the official development, let’s popularize what is an API interface. Interested friends can take a look.

API (Application Programming Interface) is some predefined interfaces (such as functions, HTTP interface), or refers to the agreement for connecting different components of the software system. A set of routines used to provide applications and developers access to a piece of software or hardware without having to access the source code or understand the details of the inner workings.

3. Function realization

1. Detailed intelligent robot API interface

Here I choose Qingyunke's chat robot api interface, because I think this is a very conscientious website.

详细见:https://api.qingyunke.com/

青云客智能机器人API接口说明:

支持功能:天气、翻译、藏头诗、笑话、歌词、计算、成语查询、拼音/五笔、人工智能聊天
接口地址:http://api.qingyunke.com/api.php?key=free&appid=0&msg=请求信息
     key 固定参数free
     appid 设置为0,表示智能识别,可忽略此参数
     msg 关键词,该值请经过 urlencode 处理后再提交
返回结果:{"result":0,"content":"内容"}
     result 状态,0表示正常,其它数字表示错误
     content 返回信息内容 

2. Third-party library preparation

requests is the simplest and easy-to-use HTTP library implemented by python. It is recommended that crawlers use the requests library, so we choose to install the requests third-party library.

Normal installation:

pip install requests

If the download is too slow or the timeout error is reported, you can try the following installation methods: 

pip install requests -i https://pypi.douban.com/simple/

3. Code implementation

First import the libraries we need

import requests  # 需要提前下载好才能使用
from urllib import parse  # url的解析,合并,编码,解码模块

Call the Qingyunke chat robot interface to crawl the reply content to realize the chat function

def chat_robot(msg):
    msg = parse.quote(msg)  # 编码
    url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg={}".format(msg)
    html = requests.get(url)  # GET请求

    return html.json()["content"].replace("{br}", "\n\t\t")

main function 

def main():
    print("输入'exit'退出此程序\n")

    while True:
        massage = input("我>>>")

        if massage == "exit":
            print("机器人:", "下再聊吧,拜拜~")
            break
        elif not massage.replace(" ", ""):  # 如果什么也没输入,则输出下面消息
            print("机器人:", "没有输入内容!")
            continue

        res = chat_robot(massage)
        print("机器人:", res)

4. Effect display

This robot is really unscrupulous. . .

"High Quality Robot"

5. All resources

Baidu Netdisk:

下载地址:https://pan.baidu.com/s/1TGi7itW-ZuEEE61WvSzoXw
密码:hv4w

Lanzuo cloud network disk:

下载地址:https://www.lanzouw.com/iFdYfxqadzc 
密码:4kqf

4. Conclusion

The above python method of calling Qingyunke intelligent robot is all the content shared by the editor today. I hope it can give you a reference. Thank you for your support and viewing. If you see friends here and like it, please click on it and follow it. ^_^, your support is my biggest motivation for updating!

Guess you like

Origin blog.csdn.net/python_sy/article/details/121943204