关键词搜索

源码搜索 ×
×

水一篇,VB+python实现智能聊天机器人案例

发布2022-12-18浏览830次

详情内容

1.分工

理论上单python也能写,但是做gui开发,python要用到thinter库/qt库,稍微麻烦一点。这个案例是python做json截取,VB做gui开发+截取json字符。


2.准备工作

编写生成file_controlv2.dll并注册,编写speaker.vbs,准备python环境,并通过pip安装pyinstaller和request 通过pyinstaller -F get_page.py生成新的exe供VB使用,get_page.py、file_controlv2.dll 和 speaker.vbs参考以下代码

get_page.py

  1. import requests
  2. import sys
  3. def get_page(msg):
  4. page=requests.get('http://api.qingyunke.com/api.php?key=free&appid=0&msg='+msg)
  5. print( page.text)
  6. f = open("pageinfo", "a")
  7. f.write(page.text)
  8. f.close()
  9. if __name__ == '__main__':
  10. print (sys.argv[1])
  11. get_page(sys.argv[1])

file_controlv2.dll

speaker.vbs

  1. Set objArgs = WScript. Arguments
  2. CreateObject("SAPI.SpVoice").speak objArgs(0)

file_controlv2.dll

  1. Public Function file_controldll() As Boolean
  2. file_controldll = True
  3. End Function
  4. Public Sub set_text(filepath As String, mode As Integer, txt As String) 'mode是模式,txt是写入内容,filepath是写入的文件路径
  5. Select Case mode
  6. Case 1
  7. Open filepath For Output As #1
  8. Case 3
  9. Open filepath For Append As #1
  10. End Select
  11. Print #1, systemout + txt & vbCrLf;
  12. Close #1
  13. End Sub
  14. Public Function get_text(filepath As String)
  15. Dim lines As String
  16. If filepath <> "" Then
  17. Open filepath For Input As #1
  18. Do While Not EOF(1)
  19. DoEvents
  20. Line Input #1, NextLine
  21. lines = lines & NextLine & vbCrLf 'vbcrif是换行的意
  22. Loop
  23. Close #1
  24. get_text = lines + Chr(13)
  25. End If
  26. End Function
  27. Public Function get_lines(filepath As String)
  28. Dim lines As Integer
  29. Open filepath For Input As #1
  30. Do While Not EOF(1)
  31. Line Input #1, Str1
  32. lines = lines + 1
  33. Loop
  34. Close #1
  35. get_lines = lines
  36. End Function
  37. Public Function get_linetext(filepath As String, lines As Integer)
  38. Open filepath For Binary As #1
  39. a = StrConv(InputB(LOF(1), 1), vbUnicode)
  40. Close #1
  41. b = Split(a, vbCrLf)
  42. get_linetext = b(lines - 1)
  43. End Function

要将Instancing的值设置为6,生成dll文件,并在创建新工程后引用,主程序工程名不能与dll工程名一样,否则会报错


3.摆放布局和编写主程序代码

具体布局如图所示

 具体代码:

  1. ''''''''''''''''
  2. '智能ai系统demo
  3. 'PYTHON+VB混合编程 && 简单api获取实例(实际就是做字符串的切割) && 微软自带语音包
  4. 'API青客云
  5. '用到的库:file_controlv2.dll 源码:GITHUB,另一个仓库
  6. 'power by wh
  7. ''''''''''''''''
  8. Sub speak(str)
  9. DoEvents
  10. 'CreateObject("SAPI.SpVoice").speak str
  11. Shell "cscript speaker.vbs" & Chr(32) & str '''降低卡顿'''
  12. End Sub
  13. Private Sub close_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  14. If Button = 1 Then End
  15. End Sub
  16. Private Sub Command1_Click()
  17. If Text1.Text = "" Or Text1.Text = "请输入信息,并点击发送" Then Exit Sub
  18. inputstr = Text1.Text
  19. Text1.Text = ""
  20. If Dir(App.Path + "\pageinfo") <> "" Then Kill App.Path + "\pageinfo"
  21. dialog.AddItem CStr(Time) + ":" + Chr(32) + "你:" & inputstr
  22. wait_for_run App.Path + "\get_page.exe" & Chr(32) & inputstr
  23. recevieinfo
  24. End Sub
  25. Sub recevieinfo() '''获取信息'''
  26. 'JSON样例 {"result":0,"content":"“111”是啥意思?"}
  27. '处理思路:直接截取:"content"后的文本,替换""和}为空就完事了
  28. recevice = get_text(App.Path + "\pageinfo")
  29. format_text = "{" & Chr(34) & "result" & Chr(34) & ":0,content" & Chr(34) & ":"
  30. '''让}和""为空
  31. ai_text = _
  32. Replace(Replace(Mid(recevice, Len(format_text) + 2), Chr(34), ""), "}", "")
  33. speak ai_text
  34. dialog.AddItem CStr(Time) + Chr(32) + "智能AI:" & ai_text
  35. End Sub
  36. Private Sub Form_Load()
  37. If Not isadmin Then MsgBox "权限不足", vbInformation, "提示": End
  38. If Not isnetworking Then MsgBox "网络未连接", vbInformation, "提示": End
  39. End Sub
  40. Private Sub imagebutt_Click(Index As Integer)
  41. Shell "cmd /c start http://www.baidu.com"
  42. End Sub
  43. '''''无窗体移动
  44. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  45. Call movewindows(Me.hwnd, X, Y)
  46. End Sub
  47. Private Sub Label1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  48. Call movewindows(Me.hwnd, X, Y)
  49. End Sub
  50. '''
  51. '''类似安卓hint
  52. Private Sub Text1_GotFocus()
  53. If Text1.Text = "请输入信息,并点击发送" Then Text1.ForeColor = vbBlack: Text1.Text = ""
  54. End Sub
  55. Private Sub Text1_KeyPress(KeyAscii As Integer)
  56. If KeyAscii = 13 Then Command1_Click
  57. End Sub
  58. Private Sub Text1_LostFocus()
  59. If Text1.Text = "" Then Text1.ForeColor = &H808080: Text1.Text = "请输入信息,并点击发送"
  60. End Sub
  61. '''

SystemAPImoduel.bas

  1. '''''''''''''
  2. '
  3. '系统api模块(包括topmost,无窗体边框移动)
  4. 'power by wh
  5. 'updata:2022 12 6
  6. '
  7. '''''''''''''
  8. '''声明
  9. Private Declare Function ReleaseCapture Lib "user32" () As Long '无窗体解锁
  10. '''无边框窗体移动'''
  11. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  12. Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
  13. Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
  14. '''等待程序运行结束'''
  15. Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
  16. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  17. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
  18. '''延迟,mathv3的yc函数就是调用的这个api'''
  19. Private Declare Sub Sleep Lib "kernel32.DLL" (ByVal dwMilliseconds As Long)
  20. '''常量声明'''
  21. Private Const SWP_NOMOVE = &H2 '不移动窗体
  22. Private Const SWP_NOSIZE = &H1 '不改变窗体尺寸
  23. Private Const Flag = SWP_NOMOVE Or SWP_NOSIZE
  24. Private Const HWND_TOPMOST = -1 '窗体总在最前面
  25. Private Const HWND_NOTOPMOST = -2 '窗体不在最前面
  26. Function isnetworking() As Boolean
  27. '''测试网络连接模块,vb6太特殊写不出不加winsock/其他dll的代码'''
  28. If Dir(Environ("temp") + "\isnetworking.dll") <> "" Then Kill (Environ("temp") + "\isnetworking.dll") '生成临时文件,只要pc环境变量没问题这里就不会有问题
  29. Shell "cmd /c ping /n 1 www.baidu.com && echo %errorlevel% > %temp%\isnetworking.dll"
  30. Sleep 1000 '1000毫秒,1秒
  31. If Not Dir(Environ("temp") + "\isnetworking.dll") <> "" Then isnetworking = False: Exit Function '这种情况属于特殊情况,网络没有连接可能出现,我也不知道为什么,还有就是权限不够
  32. If Val(get_text(Environ("temp") + "\isnetworking.dll")) = 0 Then isnetworking = True Else isnetworking = False '获取cmd传出的errorlevel值
  33. End Function
  34. Function isadmin() As Boolean
  35. On Error GoTo noadmin
  36. set_text Environ("windir") + "\admin.dll", 1, "test" '写入一个文件到windows安装目录,如果存在代表有管理员权限,否则就没有,win7以下系统不用考虑这个问题
  37. If Dir(Environ("windir") + "\admin.dll") <> "" Then Kill Environ("windir") + "\admin.dll": isadmin = True: Exit Function
  38. noadmin:
  39. isadmin = True
  40. End Function
  41. Sub topmost(istopmost As Boolean, formhwnd As Long) ''遇到全屏被底部任务栏遮挡,可以考虑用这个''
  42. Select Case istopmost
  43. Case True
  44. SetWindowPos formhwnd, HWND_TOPMOST, 0, 0, 0, 0, Flag
  45. Case False
  46. SetWindowPos formhwnd, HWND_NOTOPMOST, 0, 0, 0, 0, Flag
  47. End Select
  48. End Sub
  49. Sub movewindows(formhwnd As Long, X As Single, Y As Single)
  50. ReleaseCapture 'api实现拖动窗体移动,无边框窗体本来不可移动,加上这句就可以了
  51. SendMessage formhwnd, &HA1, 2, 0&
  52. End Sub
  53. Sub wait_for_run(exe As String) '不可用于bat!
  54. '''''''''''''''''''
  55. '运行程序模块,逻辑是等待程序运行
  56. '''''''''''''''''''
  57. i = Shell(exe) 'i为pid
  58. p = OpenProcess(&H100000, False, i) '字面意思打开进程句柄
  59. DoEvents
  60. r = WaitForSingleObject(p, -1)
  61. r = CloseHandle(p) '关闭句柄
  62. End Sub

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载