241 lines
9.3 KiB
C#
241 lines
9.3 KiB
C#
|
using Api.Framework.SDK;
|
|||
|
using Chat.Framework.WXSdk;
|
|||
|
using Chat.Framework.WXSdk.Implement;
|
|||
|
using CsharpHttpHelper;
|
|||
|
using FissionSend.Properties;
|
|||
|
using System;
|
|||
|
using System.Collections.Concurrent;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace FissionSend
|
|||
|
{
|
|||
|
public class Class1 : Plugin
|
|||
|
{
|
|||
|
public Class1()
|
|||
|
{
|
|||
|
this.Name = "裂变-辅助发送";
|
|||
|
this.Note = "随便啦";
|
|||
|
this.Logo = Resources.bbbbbbbb;
|
|||
|
}
|
|||
|
|
|||
|
#region 自定义变量
|
|||
|
public static Config Config;
|
|||
|
private Form1 mainForm = null;
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 默认
|
|||
|
public override void Start()
|
|||
|
{
|
|||
|
Config = this.ReadConfig<Config>();
|
|||
|
|
|||
|
SDK.WebRequestEvent += SDK_WebRequestEvent;
|
|||
|
SDK.ReciveIMEvent += SDK_ReciveIMEvent;
|
|||
|
}
|
|||
|
|
|||
|
public override void ShowForm()
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
if (mainForm == null || mainForm.IsDisposed)
|
|||
|
{
|
|||
|
mainForm = new Form1();
|
|||
|
mainForm.Show();
|
|||
|
}
|
|||
|
mainForm.TopMost = true;
|
|||
|
mainForm.TopMost = false;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
this.OnLog(ex.Message);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public override void Stop()
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
if (mainForm != null)
|
|||
|
{
|
|||
|
mainForm.CloseForm();
|
|||
|
mainForm = null;
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
this.OnLog(ex.Message);
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
private ConcurrentDictionary<string, string> groupDic = new ConcurrentDictionary<string, string>();
|
|||
|
|
|||
|
private void SDK_ReciveIMEvent(object sender, ReciveIMEvent e)
|
|||
|
{
|
|||
|
//try
|
|||
|
//{
|
|||
|
// if (!string.IsNullOrWhiteSpace(e.Groupid))
|
|||
|
// {
|
|||
|
// if (!groupDic.ContainsKey(e.Groupid))
|
|||
|
// {
|
|||
|
// groupDic.TryAdd(e.Groupid, e.Groupid);
|
|||
|
// if (sender.GetType() == typeof(WXClientImpl_HOOK))
|
|||
|
// {
|
|||
|
// var hook = sender as WXClientImpl_HOOK;
|
|||
|
// if (hook == null) return;
|
|||
|
// var group = hook.GetContact(e.Groupid);
|
|||
|
// this.OnLog($"添加群缓存 {e.Groupid} - {(group == null ? string.Empty : group.NickName)} ,当前群数量:{groupDic.Count}");
|
|||
|
// }
|
|||
|
// else this.OnLog($"添加群缓存 {e.Groupid},当前群数量:{groupDic.Count}");
|
|||
|
// }
|
|||
|
// }
|
|||
|
//}
|
|||
|
//catch (Exception ex)
|
|||
|
//{
|
|||
|
// this.OnLog(ex.Message);
|
|||
|
//}
|
|||
|
}
|
|||
|
|
|||
|
private void SDK_WebRequestEvent(object sender, Api.Framework.Events.WebRequestEvents e)
|
|||
|
{
|
|||
|
var method = e.Param.ContainsKey("method") ? e.Param["method"] : string.Empty;
|
|||
|
if (string.IsNullOrWhiteSpace(method)) return;
|
|||
|
if (method.ToLower() != "fission") return;
|
|||
|
|
|||
|
var text = string.Empty;
|
|||
|
var min = 0;
|
|||
|
var max = 0;
|
|||
|
var sleepmin = 5000;
|
|||
|
var sleepmax = 40000;
|
|||
|
var groups = string.Empty;
|
|||
|
try
|
|||
|
{
|
|||
|
if (e.Param.ContainsKey("text"))//方法名称
|
|||
|
{
|
|||
|
text = e.Param["text"].Trim();
|
|||
|
text = HttpHelper.URLEncode(text);
|
|||
|
text = HttpHelper.URLDecode(text, Encoding.UTF8);
|
|||
|
}
|
|||
|
if (e.Param.ContainsKey("min"))//方法名称
|
|||
|
min = getInt(e.Param["min"]);
|
|||
|
if (e.Param.ContainsKey("max"))//方法名称
|
|||
|
max = getInt(e.Param["max"]);
|
|||
|
if (e.Param.ContainsKey("sleepmin"))
|
|||
|
sleepmin = getInt(e.Param["sleepmin"]) * 1000;
|
|||
|
if (e.Param.ContainsKey("sleepmax"))
|
|||
|
sleepmax = getInt(e.Param["sleepmax"]) * 1000;
|
|||
|
if (e.Param.ContainsKey("groups"))
|
|||
|
groups = e.Param["groups"];
|
|||
|
|
|||
|
if (string.IsNullOrWhiteSpace(text)) throw new Exception("发送的内容不能为空");
|
|||
|
if (min <= 0) throw new Exception("min不能小于不能零");
|
|||
|
if (max <= 0) throw new Exception("max不能小于等于零");
|
|||
|
if (min > max) throw new Exception("min的值不能比max大");
|
|||
|
if (sleepmin > sleepmax) throw new Exception("sleepmin的值不能比sleepmax大");
|
|||
|
//e.Send("OK");
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
this.OnLog(ex.Message);
|
|||
|
e.Send("异常:" + ex.Message, -1);
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
var wxs = Chat.Framework.ChatClient.WXClient.Values.Where(f => f.WeixinType == WeixinType.Hook微信 && f.Status == WxStatus.在线);
|
|||
|
if (wxs == null || wxs.Count() == 0) throw new Exception("没有可执行的机器人");
|
|||
|
var count = wxs.Count();
|
|||
|
|
|||
|
//if (groupDic.Count == 0)
|
|||
|
//{
|
|||
|
// var groupInfos = (wxs.ToArray()[0] as WXClientImpl_HOOK).GetGroupsNotInMember();
|
|||
|
// if (groupInfos == null || groupInfos.Count == 0)
|
|||
|
// throw new Exception("群缓存列表为空");
|
|||
|
// foreach (var item in groupInfos)
|
|||
|
// {
|
|||
|
// if (!groupDic.ContainsKey(item.UserName))
|
|||
|
// {
|
|||
|
// groupDic.TryAdd(item.UserName, item.UserName);
|
|||
|
// this.OnLog($"添加群缓存 {item.UserName} - {item.NickName} ,当前群数量:{groupDic.Count}");
|
|||
|
// }
|
|||
|
// }
|
|||
|
//}
|
|||
|
|
|||
|
var _list = new List<string>();
|
|||
|
if (string.IsNullOrWhiteSpace(groups))
|
|||
|
//_list = groupDic.Keys.ToList();
|
|||
|
_list = Config.SendGroups.Replace(",", ",").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(f => f.Trim()).Where(f => f.EndsWith("@chatroom")).ToList();
|
|||
|
else
|
|||
|
_list = groups.Replace(",", ",").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(f => f.Trim()).Where(f => f.EndsWith("@chatroom")).ToList();
|
|||
|
|
|||
|
//if (string.IsNullOrWhiteSpace(Config.SendGroups)) throw new Exception("没有设置要发送群id");
|
|||
|
|
|||
|
//var _list = Config.SendGroups.Replace(",", ",").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(f => f.Trim()).Where(f => f.EndsWith("@chatroom")).ToList();
|
|||
|
|
|||
|
e.Send(_list.Count);
|
|||
|
|
|||
|
foreach (var group in _list)
|
|||
|
{
|
|||
|
//if (notSendGroups.Contains(group)) continue;
|
|||
|
var textMsg = text;
|
|||
|
var groupTemp = group;
|
|||
|
Task.Run(() =>
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
var num = getnum(count, min, max);
|
|||
|
var _wxs = wxs.ToList().Select(a => new { a, newID = Guid.NewGuid() }).OrderBy(b => b.newID).Select(c => c.a).Distinct().ToList();//打乱顺序
|
|||
|
_wxs = _wxs.GetRange(0, num);
|
|||
|
var sendUCache = new List<string>();//缓存已经发送的账号
|
|||
|
foreach (var wx in _wxs)
|
|||
|
{
|
|||
|
if (sendUCache.Contains(wx.WeixinHao)) continue;
|
|||
|
sendUCache.Add(wx.WeixinHao);
|
|||
|
wx.SendMessage(groupTemp, textMsg);
|
|||
|
this.OnLog($"群:{groupTemp},微信数量:{_wxs.Count}, 机器人:{wx.User.Username}({wx.User.Nick})({wx.User.Uin}),发送内容:text={textMsg} 已经发送");
|
|||
|
Thread.Sleep(new Random(Guid.NewGuid().GetHashCode()).Next(sleepmin, sleepmax));
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
this.OnLog("!!" + ex.Message);
|
|||
|
}
|
|||
|
});
|
|||
|
Thread.Sleep(10);
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
this.OnLog(ex.Message);
|
|||
|
e.Send(ex, -1);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private int getnum(int count, int min, int max)
|
|||
|
{
|
|||
|
var num = 0;//执行的数量
|
|||
|
var random = new Random(Guid.NewGuid().GetHashCode());
|
|||
|
if (count >= max)//随机获取min max的区间值
|
|||
|
num = random.Next(min, max);
|
|||
|
else if (count <= min)//执行count数量
|
|||
|
num = count;
|
|||
|
else//随机获取min count的区间值
|
|||
|
num = random.Next(min, count);
|
|||
|
return num;
|
|||
|
}
|
|||
|
|
|||
|
private int getInt(string intStr)
|
|||
|
{
|
|||
|
int result = 0;
|
|||
|
int.TryParse(intStr, out result);
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
}
|