Skip to content

Commit

Permalink
feat(plugins/moliplugin): 私聊成测试功
Browse files Browse the repository at this point in the history
  • Loading branch information
yiyungent committed Apr 15, 2022
1 parent c20f6da commit ce2afc6
Show file tree
Hide file tree
Showing 12 changed files with 509 additions and 0 deletions.
7 changes: 7 additions & 0 deletions QQBotHub.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QQStatPlugin", "plugins\QQS
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QQBotHub.Sdk", "src\QQBotHub.Sdk\QQBotHub.Sdk.csproj", "{56C78578-C690-4013-8AB6-1ED28770C2B1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MoLiPlugin", "plugins\MoLiPlugin\MoLiPlugin.csproj", "{F6087CC7-5239-42E3-92D6-51F25F6E3EE8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -51,6 +53,10 @@ Global
{56C78578-C690-4013-8AB6-1ED28770C2B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{56C78578-C690-4013-8AB6-1ED28770C2B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{56C78578-C690-4013-8AB6-1ED28770C2B1}.Release|Any CPU.Build.0 = Release|Any CPU
{F6087CC7-5239-42E3-92D6-51F25F6E3EE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F6087CC7-5239-42E3-92D6-51F25F6E3EE8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F6087CC7-5239-42E3-92D6-51F25F6E3EE8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F6087CC7-5239-42E3-92D6-51F25F6E3EE8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -61,6 +67,7 @@ Global
{DD974327-3D52-447A-9652-62811800140D} = {F5C1CDD2-053C-4C8E-8192-7A97477CFC44}
{D4A8C051-C14E-4123-AF20-794A98FD3662} = {4B4BFBBE-8CE4-453C-B05A-EAFE933B79C9}
{56C78578-C690-4013-8AB6-1ED28770C2B1} = {BDFD03E4-0A99-49A3-95A7-F7132EC4FF00}
{F6087CC7-5239-42E3-92D6-51F25F6E3EE8} = {4B4BFBBE-8CE4-453C-B05A-EAFE933B79C9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3F1D77E3-11F8-4B5B-BEED-EE2816D57A86}
Expand Down
30 changes: 30 additions & 0 deletions plugins/MoLiPlugin/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PluginCore;

namespace MoLiPlugin.Controllers
{
/// <summary>
/// 其实也可以不写这个, 直接访问 Plugins/QQStatPlugin/index.html
///
/// 下面的方法, 是去掉 index.html
///
/// 若 wwwroot 下有其它需要访问的文件, 如何 css, js, 而你又不想每次新增 action 指定返回, 则 Route 必须 Plugins/{PluginId},
/// 这样访问 Plugins/HelloWorldPlugin/css/main.css 就会访问到你插件下的 wwwroot/css/main.css
/// </summary>
[Route($"Plugins/{(nameof(MoLiPlugin))}")]
public class HomeController : Controller
{
public async Task<ActionResult> Get()
{
string indexFilePath = System.IO.Path.Combine(PluginPathProvider.PluginWwwRootDir(nameof(MoLiPlugin)), "index.html");

return PhysicalFile(indexFilePath, "text/html");
}

}
}
132 changes: 132 additions & 0 deletions plugins/MoLiPlugin/MoLiPlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using PluginCore.IPlugins;
using Konata.Core;
using Konata.Core.Events.Model;
using Konata.Core.Interfaces.Api;
using QQBotHub.Sdk.IPlugins;
using MoLiPlugin.Utils;

namespace MoLiPlugin
{
public class MoLiPlugin : BasePlugin, IQQBotPlugin
{
public override (bool IsSuccess, string Message) AfterEnable()
{
Console.WriteLine($"{nameof(MoLiPlugin)}: {nameof(AfterEnable)}");
return base.AfterEnable();
}

public override (bool IsSuccess, string Message) BeforeDisable()
{
Console.WriteLine($"{nameof(MoLiPlugin)}: {nameof(BeforeDisable)}");
return base.BeforeDisable();
}

#region QQBot
public void OnGroupMessage((Bot s, GroupMessageEvent e) obj, string message, string groupName, uint groupUin, uint memberUin)
{
SettingsModel settingsModel = PluginCore.PluginSettingsModelFactory.Create<SettingsModel>(nameof(MoLiPlugin));

Console.WriteLine($"茉莉: 来自: {groupUin}-{memberUin}");
if (settingsModel.AllowGroup != null && settingsModel.AllowGroup.Count >= 1 && settingsModel.AllowGroup.Contains(groupUin.ToString()))
{
Console.WriteLine("茉莉准备回复: ");
MoLiApiResponseModel resModel = new MoLiApiResponseModel();
try
{
resModel = Utils.MoLiApiUtil.Reply(new MoLiApiRequestModel
{
content = message,
type = "2",
from = memberUin.ToString(),
fromName = obj.e.Message.Sender.Name,
to = groupUin.ToString(),
toName = groupName
});
}
catch (Exception ex)
{
Console.WriteLine("茉莉回复出错: ");
Console.WriteLine(ex.ToString());
}

if (resModel != null && resModel.code == "00000")
{
if (resModel.data != null && resModel.data.Count >= 1)
{
foreach (var item in resModel.data)
{
obj.s.SendGroupMessage(groupUin: groupUin, message: item.content);
}
}
}
}
}

public void OnFriendMessage((Bot s, FriendMessageEvent e) obj, string message, uint friendUin)
{
SettingsModel settingsModel = PluginCore.PluginSettingsModelFactory.Create<SettingsModel>(nameof(MoLiPlugin));

Console.WriteLine($"茉莉: 来自: {friendUin}");
if (settingsModel.AllowFriends != null && settingsModel.AllowFriends.Count >= 1 && settingsModel.AllowFriends.Contains(friendUin.ToString()))
{
Console.WriteLine("茉莉准备回复: ");
MoLiApiResponseModel resModel = new MoLiApiResponseModel();
try
{
resModel = Utils.MoLiApiUtil.Reply(new MoLiApiRequestModel
{
content = message,
type = "1",
from = friendUin.ToString(),
fromName = obj.e.Message.Sender.Name,
to = "",
toName = ""
});
}
catch (Exception ex)
{
Console.WriteLine("茉莉回复出错: ");
Console.WriteLine(ex.ToString());
}

if (resModel != null && resModel.code == "00000")
{
if (resModel.data != null && resModel.data.Count >= 1)
{
foreach (var item in resModel.data)
{
obj.s.SendFriendMessage(friendUin: friendUin, message: item.content);
}
}
}
}
}

public void OnBotOnline((Bot s, BotOnlineEvent e) obj, string botName, uint botUin)
{
//SettingsModel settingsModel = PluginCore.PluginSettingsModelFactory.Create<SettingsModel>(nameof(QQStatPlugin));

//if (settingsModel != null && !string.IsNullOrEmpty(settingsModel.AdminQQ))
//{
// obj.s.SendFriendMessage(Convert.ToUInt32(settingsModel.AdminQQ), $"{obj.s.Name}({obj.s.Uin}) 上线啦");
//}
}

public void OnBotOffline((Bot s, BotOfflineEvent e) obj, string botName, uint botUin)
{
//SettingsModel settingsModel = PluginCore.PluginSettingsModelFactory.Create<SettingsModel>(nameof(QQStatPlugin));

//if (settingsModel != null && !string.IsNullOrEmpty(settingsModel.AdminQQ))
//{
// obj.s.SendFriendMessage(Convert.ToUInt32(settingsModel.AdminQQ), $"{obj.s.Name}({obj.s.Uin}) 离线啦");
//}
}
#endregion


}
}
53 changes: 53 additions & 0 deletions plugins/MoLiPlugin/MoLiPlugin.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0">
<ExcludeAssets>runtime</ExcludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<!--<PackageReference Include="SqlSugarCore" Version="5.0.7.3">
<ExcludeAssets>runtime</ExcludeAssets>
</PackageReference>-->
<!--<PackageReference Include="SqlSugarCore" Version="5.0.7.3" />-->
<!--<PackageReference Include="SqlSugarCoreNoDrive" Version="5.0.7.3" />-->
</ItemGroup>

<!-- 方便开发debug,与发布到nuget -->
<ItemGroup Condition="'$(Configuration)' == 'Release'">
<PackageReference Include="QQBotHub.Sdk" Version="0.0.1">
<ExcludeAssets>runtime</ExcludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
<ProjectReference Include="..\..\src\QQBotHub.Sdk\QQBotHub.Sdk.csproj">
<Private>false</Private>
<ExcludeAssets>runtime</ExcludeAssets>
</ProjectReference>
</ItemGroup>

<!-- 发布插件相关文件 -->
<ItemGroup>
<Content Include="info.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="README.md">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="settings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<!-- 发布 wwwroot -->
<ItemGroup>
<Content Include="wwwroot\**\*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
21 changes: 21 additions & 0 deletions plugins/MoLiPlugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@





## 使用


前往 [https://mly.app](https://mly.app) 注册, 创建机器人, 获取 `ApiKey` 以及 `ApiSecret` 填入设置, 然后启用





## 前端视图

- [/Plugins/MoLiPlugin](/Plugins/MoLiPlugin)




19 changes: 19 additions & 0 deletions plugins/MoLiPlugin/SettingsModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using PluginCore.Models;

namespace MoLiPlugin
{
public class SettingsModel : PluginSettingsModel
{
public string ApiKey { get; set; }

public string ApiSecret { get; set; }

public List<string> AllowGroup { get; set; }

public List<string> AllowFriends { get; set; }
}
}
104 changes: 104 additions & 0 deletions plugins/MoLiPlugin/Utils/DateTimeUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System;

namespace MoLiPlugin.Utils
{
/// <summary>
/// JavaScript时间戳:是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总毫秒数。(13 位数字)
///
/// Unix时间戳:是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。(10 位数字)
/// </summary>
public static class DateTimeUtil
{
public static DateTime DateTime1970 = new DateTime(1970, 1, 1).ToLocalTime();

#region Unix 10位时间戳-总秒数
/// <summary>
/// C# DateTime转换为Unix时间戳
/// </summary>
public static long ToTimeStamp10(this DateTime dateTime)
{
// 相差秒数
long timeStamp = (long)(dateTime.ToLocalTime() - DateTime1970).TotalSeconds;

return timeStamp;
}

/// <summary>
/// C# DateTime转换为Unix时间戳
/// </summary>
public static long ToTimeStamp10(this DateTime? dateTime)
{
if (dateTime == null)
{
return 0;
}
// 相差秒数
long timeStamp = ToTimeStamp10((DateTime)dateTime);

return timeStamp;
}

/// <summary>
/// Unix时间戳转换为C# DateTime
/// </summary>
public static DateTime ToDateTime10(this long timeStamp10)
{
DateTime dateTime = DateTime1970.AddSeconds(timeStamp10).ToLocalTime();

return dateTime;
}
#endregion

#region JavaScript 13位时间戳-总毫秒数
/// <summary>
/// C# DateTime转换为JavaScript时间戳
/// </summary>
public static long ToTimeStamp13(this DateTime dateTime)
{
// 相差毫秒数
long timeStamp = (long)(dateTime.ToLocalTime() - DateTime1970).TotalMilliseconds;

return timeStamp;
}

/// <summary>
/// C# DateTime转换为JavaScript时间戳
/// </summary>
public static long ToTimeStamp13(this DateTime? dateTime)
{
if (dateTime == null)
{
return 0;
}
// 相差秒数
long timeStamp = ToTimeStamp13((DateTime)dateTime);

return timeStamp;
}

/// <summary>
/// JavaScript时间戳转换为C# DateTime
/// </summary>
public static DateTime ToDateTime13(this long timeStamp13)
{
DateTime dateTime = DateTime1970.AddMilliseconds(timeStamp13).ToLocalTime();

return dateTime;
}
#endregion

#region 获取当前Unix时间戳
public static long NowTimeStamp10()
{
return ToTimeStamp10(DateTime.Now);
}
#endregion

#region 获取当前JavaScript时间戳
public static long NowTimeStamp13()
{
return ToTimeStamp13(DateTime.Now);
}
#endregion
}
}
Loading

0 comments on commit ce2afc6

Please sign in to comment.