Build a Python Chatbot with Turing API: Step‑by‑Step Guide

This tutorial walks you through creating a desktop Python chatbot using the Turing API, covering required libraries, authentication, code implementation for both basic and Selenium‑enhanced versions, and packaging the final program into an executable.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Build a Python Chatbot with Turing API: Step‑by‑Step Guide

Introduction

While at home I built a small project: a voice‑chat robot based on Python and shared it. The overall project is simple, the official documentation is very detailed, and you can get started quickly.

Goal

Deploy a Turing robot on the desktop to serve as a work assistant or companion for casual conversation.

Libraries Used

Version 1.0: requests, json Version 2.0: requests, json, selenium (to automatically open URLs returned by the Turing API).

Implementation

1. Create a Turing robot – the creation process is straightforward and unrelated to Python, so it is omitted here. After creation, you must authenticate to enjoy 100 free replies per day.

2. Write the code (Version 1.0)

import requests</code><code>import json</code><code>from selenium import webdriver

Set the API endpoint:

url = 'http://openapi.tuling123.com/openapi/api/v2'

Define request headers:

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}

Enter a loop to read user input and send it to the API:

while True:</code><code>    aa = input('我:')</code><code>    data = {</code><code>        "perception": {</code><code>            "inputText": {"text": aa},</code><code>            "selfInfo": {"location": {"city": "济南"}}</code><code>        },</code><code>        "userInfo": {"apiKey": "YOUR_API_KEY", "userId": "ANY_ID"}</code><code>    }</code><code>    res = requests.post(url, headers=headers, data=json.dumps(data))</code><code>    try:</code><code>        print('机器人:' + res.json()['results'][0]['values']['text'])</code><code>        if aa == '退出':</code><code>            break</code><code>    except:</code><code>        print('机器人:' + res.json()['results'][0]['values']['url'])

The loop runs indefinitely; typing “退出” exits it. If the API returns a URL instead of text, the program opens the URL.

Version 2.0 Enhancements

Install the selenium library and download ChromeDriver. Place the chromedriver.exe in the Python installation directory.

Add three lines to the previous code to handle URL responses:

except:</code><code>    print('机器人:' + res.json()['results'][0]['values']['url'])</code><code>    url_1 = res.json()['results'][0]['values']['url']</code><code>    driver = webdriver.Chrome()</code><code>    driver.get(url_1)

This automatically opens the returned URL in a browser.

Packaging

Finally, package the script into an executable with: pyinstaller -F path/to/your_script.py The resulting .exe can be placed on the desktop or shared with others.

Conclusion

1. The project is straightforward and the official documentation enables rapid development.

2. webdriver is very useful for bypassing anti‑scraping measures on many sites.

3. The Turing API offers many more features to explore.

4. This chatbot demonstrates how ordinary users can experience AI conveniently.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonAIChatbotSeleniumTuring API
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.