This is the 31st day of my participation in the August More Text Challenge

August more text challenge last day, has been based on the output of knowledge points, the last day to point different!

As is known to all, now there are many online is very intelligent (disabled) AI robot interface, use of these interfaces, we can achieve a robot, can chat with the dialogue, in order to increase the fun of the robot chats, today I decided to make two robots, it both to chat, and see what will happen interesting things, start!!!!!!

Robot API interface

After a baidu search, I found several recommended robot interfaces as follows:

  • Day robot: day robot, verify mailbox can have 100 times of free call times every day, the test is enough, do not want to use their own mailbox, you can find some temporary mailbox (baidu search a lot of).

  • Qingyun guest robot: qingyun guest robot, this robot seems to call Fifi, indifferent, mainly can white piao, need not land, can call interface directly.

  • Turing robot: Turing robot, it is said that this is quite intelligent, but it seems not free ah, 19.9 yuan, joking, I can have that money, or maybe I have a problem with the operation, anyway, use the first two.

Call the interface to encapsulate the robot

Requests are sent using the Requests library, which requires PIP Install Requests to be installed.

  • Skyline robot:
class TXRobot:
    def __init__(self) :
        self.api = "http://api.tianapi.com/txapi/robot/index?key={}&question={}"
        self.key = "xxxxxx"  Fill in the application key on the official website

    def chat(self, msg) :
        res = requests.get(tx_url.format(self.key, msg)).json()
        content = res["newslist"] [0] ["reply"]
        return content
Copy the code

Note: I set the false self.key here, so I need to change self.key to the real key applied on the official website. Give it a try and see what it replies:

tx_robot = TXRobot()
print(tx_robot.chat("Hello?"))
{robotName} is {robotName}, what can I do for you?
Copy the code
  • Qingyunke Robot:
class QYKRobot:
    def __init__(self) :
        self.api = "http://api.qingyunke.com/api.php?key=free&appid=0&msg={}"

    def chat(self, msg) :
        res = requests.get(self.api.format(msg)).json()
        content = res["content"]
        return content
Copy the code

Try again:

qyk_robot = QYKRobot()
print(qyk_robot.chat("Hello?"))
I'm fine, how are you, how are you
Copy the code

Okay, the two robots are wrapped up, and at least for now, they’re responding okay, no special retardation. Next, let them talk! Get ready for the show!!

Realize two robot chat

The logic is to type in the first sentence, get one robot to respond, then take its response as a message, get the other robot to respond, and so on.

if __name__ == '__main__':
    msg = input(Enter the first sentence of the chat dialog:)
    while True:
        tx_robot = TXRobot()
        content = tx_robot.chat(msg)
        print("Skywalker Robot :", content)

        qyk_robot = QYKRobot()
        msg = qyk_robot.chat(content)
        print(Qingyunke Robot:, msg)
Copy the code

Qingyunke robot has a sentence deep in my heart, that is the motherland has not yet unified, you unexpectedly have the mood to learn, ha ha ha, said right ah, all touch the fish!!

Chat text to voice

Since the two robots have already realized the chat, that can realize the chat text to voice, so that you can listen to the dialogue between them. Find a third party library: Pyttsx3, which can be installed using PIP Install Pyttsx3. This library can convert text to speech and play it back, and is very simple to use, for example:

import pyttsx3
engine = pyttsx3.init()

engine.say('hello, the nuggets')
engine.runAndWait()
Copy the code

So change the code:

if __name__ == '__main__':
    msg = input(Enter the first sentence of the chat dialog:)
    engine = pyttsx3.init()
    while True:
        tx_robot = TXRobot()
        content = tx_robot.chat(msg)
        print("Skywalker Robot :", content)
        engine.say('Skybot says :{}'.format(content))
        engine.runAndWait()

        qyk_robot = QYKRobot()
        msg = qyk_robot.chat(content)
        print(Qingyunke Robot:, msg)
        engine.say('Qingyunke robot says :{}'.format(msg))
        engine.runAndWait()
Copy the code

So you can listen to the dialogue between the two robots!

conclusion

The result is achieved, although the two robots are not very intelligent, but at least to achieve the basic chat dialogue function, one might say, this is not the adjustment of the third party interface, right, mainly called the third party interface, although the code is very simple, logic is not difficult, but still quite interesting. If you need to call intelligent robots, you can also look at the products introduced above.

This is the last article of the challenge of More articles in August. The challenge of More articles is quite big for me, but I still insist on it. Next, I will summarize the previous articles.

Finally, thank my girlfriend for her tolerance, understanding and support in work and life!