对接ChatGPT接口

This commit is contained in:
ZouJin 2023-03-05 03:03:51 +11:00
parent 543bff7d2f
commit 33e7656415
4 changed files with 129 additions and 0 deletions

View File

@ -24,6 +24,7 @@ namespace VPet_Simulator.Windows
{ {
private NotifyIcon notifyIcon; private NotifyIcon notifyIcon;
public System.Timers.Timer AutoSaveTimer = new System.Timers.Timer(); public System.Timers.Timer AutoSaveTimer = new System.Timers.Timer();
public TalkBox TalkBox;
public MainWindow() public MainWindow()
{ {
//判断是不是Steam用户,因为本软件会发布到Steam //判断是不是Steam用户,因为本软件会发布到Steam
@ -154,6 +155,11 @@ namespace VPet_Simulator.Windows
winSetting = new winGameSetting(this); winSetting = new winGameSetting(this);
Main = new Main(Core) { }; Main = new Main(Core) { };
if (IsSteamUser)
{
TalkBox = new TalkBox(Main);
Main.ToolBar.MainGrid.Children.Add(TalkBox);
}
Main.DefaultClickAction = () => Main.DefaultClickAction = () =>
{ {

View File

@ -116,6 +116,9 @@
<Compile Include="Function\CoreMOD.cs" /> <Compile Include="Function\CoreMOD.cs" />
<Compile Include="Function\Setting.cs" /> <Compile Include="Function\Setting.cs" />
<Compile Include="MainWindow.cs" /> <Compile Include="MainWindow.cs" />
<Compile Include="WinDesign\TalkBox.xaml.cs">
<DependentUpon>TalkBox.xaml</DependentUpon>
</Compile>
<Compile Include="WinDesign\winCharacterPanel.xaml.cs"> <Compile Include="WinDesign\winCharacterPanel.xaml.cs">
<DependentUpon>winCharacterPanel.xaml</DependentUpon> <DependentUpon>winCharacterPanel.xaml</DependentUpon>
</Compile> </Compile>
@ -149,6 +152,10 @@
<DependentUpon>MainWindow.xaml</DependentUpon> <DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Page Include="WinDesign\TalkBox.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="WinDesign\winCharacterPanel.xaml"> <Page Include="WinDesign\winCharacterPanel.xaml">
<SubType>Designer</SubType> <SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>

View File

@ -0,0 +1,22 @@
<UserControl x:Class="VPet_Simulator.Windows.TalkBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:pu="clr-namespace:Panuon.WPF.UI;assembly=Panuon.WPF.UI" Height="500" Width="500">
<Border Background="{DynamicResource PrimaryLighter}" BorderBrush="{DynamicResource Primary}" BorderThickness="5"
VerticalAlignment="Top" Margin="5,50,5,5" CornerRadius="5" Padding="5">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<TextBox x:Name="tbTalk" Style="{DynamicResource StandardTextBoxStyle}" Height="Auto"
pu:TextBoxHelper.Watermark="和桌宠说" FontSize="30" AcceptsReturn="True" TextWrapping="WrapWithOverflow"/>
<Button pu:ButtonHelper.CornerRadius="4" Content="发送" BorderThickness="2"
Background="{DynamicResource SecondaryLight}" Grid.Column="2" BorderBrush="{DynamicResource DARKPrimaryDarker}"
FontSize="30" Click="SendMessage_Click" />
</Grid>
</Border>
</UserControl>

View File

@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Web;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VPet_Simulator.Core;
using Timer = System.Timers.Timer;
namespace VPet_Simulator.Windows
{
/// <summary>
/// MessageBar.xaml 的交互逻辑
/// </summary>
public partial class TalkBox : UserControl
{
Main m;
public TalkBox(Main m)
{
InitializeComponent();
this.m = m;
}
private void SendMessage_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(tbTalk.Text))
{
return;
}
var cont = tbTalk.Text;
var sid = Steamworks.SteamClient.SteamId.Value;
tbTalk.Text = "";
Task.Run(() => OPENAI(sid, cont));
}
/// <summary>
/// 使用OPENAI-LB进行回复
/// </summary>
/// <param name="steamid">steamid,用于记录历史</param>
/// <param name="content">内容 说话内容</param>
public void OPENAI(ulong steamid, string content)
{
Dispatcher.Invoke(() => this.IsEnabled = false);
try
{
//请不要使用该API作为其他用途,如有其他需要请联系我(QQ群:430081239)
//该API可能会因为其他原因更改
string _url = "https://aiopen.exlb.net:5810/VPet/Talk";
//参数
StringBuilder sb = new StringBuilder();
sb.Append($"steamid={steamid}");
sb.AppendLine($"&content={HttpUtility.UrlEncode(content)}");
var request = (HttpWebRequest)WebRequest.Create(_url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";//ContentType
byte[] byteData = Encoding.UTF8.GetBytes(sb.ToString());
int length = byteData.Length;
request.ContentLength = length;
using (Stream writer = request.GetRequestStream())
{
writer.Write(byteData, 0, length);
writer.Close();
writer.Dispose();
}
string responseString;
using (var response = (HttpWebResponse)request.GetResponse())
{
responseString = new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd();
response.Dispose();
}
m.Say(responseString);
}
catch (Exception exp)
{
m.Say(exp.ToString());
}
Dispatcher.Invoke(() => this.IsEnabled = true);
}
}
}