main.py
# -*- coding:utf-8 -*-
import wx # 导入wxPython
import chatApi
import win32api
import sys, os
#要用绝对路径!
APP_ICON = 'D:\\PycharmProjects\\Rebot\\res\\Robot.ico'
class MyFrame(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, '在线聊天窗口', size=(400,300))
# 以下代码处理图标
if hasattr(sys, "frozen") and getattr(sys, "frozen") == "windows_exe":
exeName = win32api.GetModuleFileName(win32api.GetModuleHandle(None))
icon = wx.Icon(exeName, wx.BITMAP_TYPE_ICO)
else:
icon = wx.Icon(APP_ICON, wx.BITMAP_TYPE_ICO)
self.SetIcon(icon)
# 以下可以添加各类控件
pass
# 创建面板
panel = wx.Panel(self)
# 创建“确定”和“取消”按钮, 并绑定事件
self.bt_confirm = wx.Button(panel, label='发送')
self.bt_confirm.Bind(wx.EVT_BUTTON, self.OnclickSubmit)
self.bt_cancel = wx.Button(panel, label='清空')
self.bt_cancel.Bind(wx.EVT_BUTTON, self.OnclickCancel)
#创建关于按钮,并绑定事件
self.bt_about = wx.Button(panel, label='关于')
self.bt_about.Bind(wx.EVT_BUTTON, self.OnclickAbout)
# 创建文本,左对齐
self.title = wx.StaticText(panel, label="在线聊天")
self.label_user = wx.StaticText(panel, label="You")
self.text_user = wx.TextCtrl(panel, style=wx.TE_LEFT)
self.label_rebot = wx.StaticText(panel, label="机器人")
self.text_rb = wx.TextCtrl(panel,pos = (5,39),size = (200,100),style = wx.TE_MULTILINE)
self.text_rb.write('Hi,我是小i机器人,我可以查天气,讲笑话~ 除此之外还有几十项实用好玩的功能哦~ 快来试试吧!')
# 添加容器,容器中控件按横向并排排列
hsizer_user = wx.BoxSizer(wx.HORIZONTAL)
hsizer_user.Add(self.label_user, proportion=0, flag=wx.ALL, border=5)
hsizer_user.Add(self.text_user, proportion=1, flag=wx.ALL, border=5)
hsizer_rb = wx.BoxSizer(wx.HORIZONTAL)
hsizer_rb.Add(self.label_rebot, proportion=0, flag=wx.ALL, border=5)
hsizer_rb.Add(self.text_rb, proportion=1, flag=wx.ALL, border=5)
hsizer_button = wx.BoxSizer(wx.HORIZONTAL)
hsizer_button.Add(self.bt_confirm, proportion=0, flag=wx.ALIGN_CENTER, border=2)
hsizer_button.Add(self.bt_cancel, proportion=0, flag=wx.ALIGN_CENTER, border=2)
# hsizer_button.Add(self.bt_about, property=0, flag=wx.ALIGN_CENTER,border=2)
# 添加容器,容器中控件按纵向并排排列
vsizer_all = wx.BoxSizer(wx.VERTICAL)
vsizer_all.Add(self.title, proportion=0, flag=wx.BOTTOM | wx.TOP | wx.ALIGN_CENTER,
border=15)
vsizer_all.Add(hsizer_user, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=45)
vsizer_all.Add(hsizer_rb, proportion=0, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=45)
vsizer_all.Add(hsizer_button, proportion=0, flag=wx.ALIGN_CENTER | wx.TOP, border=10)
panel.SetSizer(vsizer_all)
def OnclickSubmit(self, event):
""" 点击确定按钮,执行方法 """
message = ""
MyNews = self.text_user.GetValue()
rbNews=self.text_rb.GetValue()
if MyNews == "" :
message = '请输入你要说的话!'
wx.MessageBox(message,'Warning',wx.OK| wx.ICON_WARNING)
else:
if rbNews !="":
self.text_rb.SetValue("")
#接口一
self.text_rb.write(chatApi.get_reply(MyNews))
#接口二
#self.text_rb.write(chatApi.getContent(MyNews))
# print("1---",chatApi.get_reply(MyNews))
def OnclickCancel(self, event): # 没有event点击取消会报错
""" 点击清空按钮,执行方法 """
self.text_user.SetValue("")
self.text_rb.SetValue("")
def OnclickAbout(self,event):
wx.MessageBox('QQ----1820500218','关于作者',wx.OK)
if __name__ == '__main__':
app = wx.App() # 初始化应用
frame = MyFrame(parent=None, id=1) # 实例MyFrame类,并传递参数
frame.Show() # 显示窗口
app.MainLoop() # 调用MainLoop()主循环方法
chatApi.py
# -*- coding:utf-8 -*-
import requests,re
from urllib.parse import quote,unquote
import json
'''
quote() ---> 中文转utf编码
unquote() --->utf编码转中文
'''
def get_reply(data):
ini = "{'sessionId':'09e2aca4d0a541f88eecc77c03a8b393','robotId':'webbot','userId':'462d49d3742745bb98f7538c42f9f874','body':{'content':'" + data + "'},'type':'txt'}&ts=1529917589648"
url = "http://i.xiaoi.com/robot/webrobot?&callback=__webrobot_processMsg&data=" + quote(ini)
cookie = {"cnonce": "808116", "sig": "0c3021aa5552fe597bb55448b40ad2a90d2dead5",
"XISESSIONID": "hlbnd1oiwar01dfje825gavcn", "nonce": "273765", "hibext_instdsigdip2": "1"}
r = requests.get(url, cookies=cookie)
# print(r.text)
pattern = re.compile(r'\"fontColor\":0,\"content\":\"(.*?)\"')
result = pattern.findall(r.text)
rs=str(result[1]).replace("\\r",'').replace("\\n",'')
return rs