Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to generate Turing smart buddies and realize the functions of work assistant and chat

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to use Python to generate Turing smart buddies and achieve work assistant and chat functions, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

/ 1 Preface /

Idle at home, do a small project, based on Python, achieve a chat robot, share with you. The overall project is relatively simple, the official documents are very detailed, and you can get started quickly.

/ 2 Target /

Put the Turing robot on the desktop to achieve the function of work assistant / chat.

/ 3 libraries involved /

V1.0: requests, json

Version V2.0: requests, json, selenium (function: automatically open the web page if Turing returns the result as url)

/ 4 specific implementation /

1. Create a Turing robot

There are many ways to create a Turing robot online, because creating a Turing robot is very simple and has nothing to do with Python, so I won't go into it here.

Note: after creating a robot, you need to be certified to enjoy 100 free responses a day.

two。 There are many API access tutorials on the Internet. If you want to play casually, you can use Xiaobian's code. If you want to achieve more, you can take a look at the API documentation.

3. Find the most critical apiKey parameter after successful authentication.

4. Start writing code-version V1.0

First introduce the required libraries

Import requestsimport jsonfrom selenium import webdriver

Url specifies url in the official document

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

Add request header

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'}

The following is the input, refer to the official document format

While True: aa = input ('I:') data = {"perception": {"inputText": {"text": aa}, "selfInfo": {"location": {"city": "Jinan"} "userInfo": {"apiKey": "write your own APIKEY", "userId": "just write a few numbers and letters"}}

Whiletrue makes chat endless loop

The variable aa (random) enters information through input (), which is passed into the variable "text"

"city" fill in your city, can be changed to your city, can be detailed to "street"

UserInfo is personal information, apiKey fill in the apiKey,userId measurement of the robot you set up, you can write freely, such as' 123456'.

Res = requests.post (url,headers=headers,data=json.dumps (data))

The above is parsed data that converts dictionary-formatted data variables into legitimate json strings and passes them to post's data parameters.

Try: print ('robot:' + res.json () ['results'] [0] [' values'] ['text']) if aa =' exit': break except: print ('robot:' + res.json () ['results'] [0] [' values'] ['url']))

The above is the output data.

If the returned value is not 'text', then return' url'

If you reply "exit", exit the loop.

The above is all the code.

The above is the V1.0 effect picture, which can check the weather, train frequency, express delivery, hotel, recipe, tongue twister and other functions.

5.V2.0 version

First install the selenium library and download Google browser.

Put the chromedriver file under the Python installation file, and the chromedriver file can be downloaded at the end of the article.

Introduction of related libraries

Import requestsimport jsonfrom selenium import webdriver

Compared to 1.0, you only need to add the last three lines of code. The three lines of code are to get url, open the browser, and open the url with the browser. It's very simple, isn't it?

Except: print ('robot:' + res.json () ['results'] [0] [' values'] ['url']) url_1 = res.json () [' results'] [0] ['values'] [' url'] driver = webdriver.Chrome () driver.get (url_1)

The above is the effect display, very convenient, isn't it!

Finally, package it into a .exe file (the absolute path where cmd runs the pyinstaller-F code source file) and put it on your desktop, or share it with TA!

The overall project is relatively simple, the official documents are very detailed, and you can get started quickly.

Webdriver is very practical, and many websites that do well in anti-climbing can be cracked with this library.

The official also provides more functions waiting for you to experience.

According to the actual measurement of Wechat's personal number access function, it is more convenient to call the Turing robot through Wechat, but the two official three-party programs charge one and one needs to hang up.

4. Finally, thanks to Turing robot, let us ordinary people have free channels to experience AI, experience the convenience that technology brings to human life!

The above content is how to use Python to generate Turing smart buddies and achieve work assistant and chat functions. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report