First the package the project needs

import urllib.request
import urllib.parse
from tkinter import *
import time


Copy the code

Function description: Call the server interface to achieve non-specific intelligent reply

def get_robot_replay(question): Parameter description: Question: chat content or question Return value: STR, reply content "" If" what is your name "in question: Answer =" HOW old are you "in question: answer="18" elif "Are you GG or MM"in question: answer="MM" else: try: Params =urllib.parse.urlencode({' MSG ':question}).encode() URL encoding parameter interface needed the req = urllib. Request. The request (" http://api.itmojun.com/chat_robot ", params, method = "POST") # create request object Answer =urllib.request.urlopen(req).read().decode()# except Exception as e: answer="AI robot failed! "% e return answerCopy the code

Reply format and interface design

def msgsend(): MSG = 'I + time. Strftime (' % % Y - m - H: % d % % m: % S', and time. The localtime ()) + '\ n' txt_msglist. Insert (END, MSG, Insert (END, txt_msgssend. Get ('0.0', END)) # Strftime ('%Y-%m-%d %H:% m :%S', time.localtime()) + '\n' txt_msglist. Insert (END, msg1, Insert (END,get_robot_replay(txt_msgsend-get ('0.0', END))) txt_msgsend-delete ('0.0', Def msgsendEvent(event) def msgsendEvent(event) def msgsendEvent(event) if event.keysym == 'Up': msgsend()Copy the code

Chat interface design (source code)

The original address: developer.aliyun.com/ask/269932?…