优化自动回复

This commit is contained in:
wule_5900x committed 2026-04-11 02:16:59 +08:00
1 parent e80d281017
commit 43587dbdd4
2 files changed
+102 -83

No files matched your search

+87 -66
View File
@@ -242,16 +242,29 @@ namespace TemuKJD.WXForms.TalkForms
if (chkIsAutoReply.Checked && 1==1)
{
while (lstAutoReplayParam.Count > 0)
{
AutoReplayParam autoReplayParam = lstAutoReplayParam.FirstOrDefault();
var autoReplayParamLst = lstAutoReplayParam.Where(r => r.nickName == autoReplayParam.nickName).ToList();
var shop = GlobalConfig.ShopLoginList.FirstOrDefault(s => s.ShopId == autoReplayParam.shopId);
if (shop == null)
continue;
Invoke(new Action(() => { lblAutoReplyStatus.Visible = true; lblAutoReplyStatus.Text = "自动回复中... " + DateTime.Now.ToString("HH:mm:ss"); }));
// 选择正确的用户
var jsStr1 = @"function clickUser() {
if (lstAutoReplayParam.Count > 0)
{
var tmpAutoReplayParamLst = new List<AutoReplayParam>();
foreach (var rp in lstAutoReplayParam)
{
if (!tmpAutoReplayParamLst.Any(r => r.nickName == rp.nickName))
tmpAutoReplayParamLst.Add(rp);
}
foreach (AutoReplayParam autoReplayParam in tmpAutoReplayParamLst)
{
//AutoReplayParam autoReplayParam = lstAutoReplayParam.FirstOrDefault();
var autoReplayParamLst = lstAutoReplayParam.Where(r => r.nickName == autoReplayParam.nickName).ToList();
var shop = GlobalConfig.ShopLoginList.FirstOrDefault(s => s.ShopId == autoReplayParam.shopId);
if (shop == null)
{
lstAutoReplayParam.RemoveAll(r => r.shopId == autoReplayParam.shopId);
continue;
}
// 选择正确的用户
var jsStr1 = @"function clickUser() {
var users = document.getElementsByClassName('user-nickname');
for (i=0; i<users.length; i++) {
if (users[i].innerHTML == '" + autoReplayParam.nickName + @"') {
@@ -263,12 +276,13 @@ namespace TemuKJD.WXForms.TalkForms
return 'fail';
}
clickUser();";
var response1 = shop.Chat.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr1);
response1.Wait(2000);
if (response1.Result.Result.ParseToString() != "success")
{
Thread.Sleep(500);
jsStr1 = @"function clickUser() {
var response1 = shop.Chat.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr1);
response1.Wait(2000);
if (response1.Result.Result.ParseToString() != "success")
{
Thread.Sleep(Randomx.GetId(500, 1000));
Invoke(new Action(() => { lblAutoReplyStatus.Text = "当前用户获取失败,尝试获取全部用户 " + DateTime.Now.ToString("HH:mm:ss"); }));
var jsStr2 = @"function clickAllUser() {
var users = document.getElementsByClassName('tab-list-item');
for (i=0; i<users.length; i++) {
if (users[i].innerHTML == '全部' && users[i].parentElement.innerHTML.indexOf('当前会话') !== -1) {
@@ -276,72 +290,79 @@ namespace TemuKJD.WXForms.TalkForms
}
}
}
clickUser();";
clickAllUser();";
var response2 = shop.Chat.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr2);
response2.Wait(2000);
Thread.Sleep(Randomx.GetId(500, 1000));
response1 = shop.Chat.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr1);
response1.Wait(2000);
}
Thread.Sleep(Randomx.GetId(1500, 2000));
// 输入内容
// 默认回复
string replyContent = "亲,客服打包去了,请稍候!";
// 分割完整语句匹配
var replyContentLst = LstTalkTemplatePair
.Where(t => lstAutoReplayParam.Any(a => t.Key.Split('|').Any(k => !k.IsNullOrEmpty() && a.talkContent == k)))
.OrderBy(x => Randomx.GetId(100000)).ToList();
// 分割完整语句匹配(过滤标点符号)
if (replyContentLst.Count == 0)
replyContentLst = LstTalkTemplatePair
.Where(t => lstAutoReplayParam.Any(a => t.Key.Split('|').Any(k => !k.IsNullOrEmpty() && a.talkContent.Trim('?', '?', ',', ',', '.', '。', '!', '!') == k)))
.OrderBy(x => Randomx.GetId(100000)).ToList();
// 反向包含匹配
if (replyContentLst.Count == 0)
replyContentLst = LstTalkTemplatePair.Where(t => lstAutoReplayParam.Any(a => a.talkContent.Contains(t.Key))).OrderBy(x => Randomx.GetId(100000)).ToList();
// 不能匹配的发默认消息
if (replyContentLst.Count == 0)
replyContentLst = LstTalkTemplatePair.Where(t => t.Key.IsNullOrEmpty()).OrderBy(x => Randomx.GetId(100000)).ToList();
if (replyContentLst.Count > 0)
replyContent = replyContentLst.FirstOrDefault().Value;
shop.Chat.GetBrowser().MainFrame.EvaluateScriptAsync(KJDForms.ShopForms.ShopLoginForm.sInputValueChange2);
jsStr1 = "var txtarea = document.getElementById('input-textarea'); " +
"changeReactInputValue(txtarea,'" + replyContent + "');";
response1 = shop.Chat.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr1);
response1.Wait(2000);
Thread.Sleep(500);
Thread.Sleep(Randomx.GetId(100, 200));
continue;
}
Thread.Sleep(1500);
// 输入内容
// 默认回复
string replyContent = "亲,客服打包去了,请稍候!";
// 分割完整语句匹配
var replyContentLst = LstTalkTemplatePair
.Where(t => lstAutoReplayParam.Any(a => t.Key.Split('|').Any(k => !k.IsNullOrEmpty() && a.talkContent == k)))
.OrderBy(x => Randomx.GetId(100000)).ToList();
// 分割完整语句匹配(过滤标点符号)
if (replyContentLst.Count == 0)
replyContentLst = LstTalkTemplatePair
.Where(t => lstAutoReplayParam.Any(a => t.Key.Split('|').Any(k => !k.IsNullOrEmpty() && a.talkContent.Trim('?', '?', ',', ',', '.', '。', '!', '!') == k)))
.OrderBy(x => Randomx.GetId(100000)).ToList();
// 反向包含匹配
if (replyContentLst.Count == 0)
replyContentLst = LstTalkTemplatePair.Where(t => lstAutoReplayParam.Any(a => a.talkContent.Contains(t.Key))).OrderBy(x => Randomx.GetId(100000)).ToList();
// 不能匹配的发默认消息
if (replyContentLst.Count == 0)
replyContentLst = LstTalkTemplatePair.Where(t => t.Key.IsNullOrEmpty()).OrderBy(x => Randomx.GetId(100000)).ToList();
if (replyContentLst.Count > 0)
replyContent = replyContentLst.FirstOrDefault().Value;
shop.Chat.GetBrowser().MainFrame.EvaluateScriptAsync(KJDForms.ShopForms.ShopLoginForm.sInputValueChange2);
jsStr1 = "var txtarea = document.getElementById('input-textarea'); " +
"changeReactInputValue(txtarea,'" + replyContent + "');";
response1 = shop.Chat.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr1);
response1.Wait(2000);
Thread.Sleep(Randomx.GetId(100, 200));
// 点击回车发送自动回复
jsStr1 = @"var event = new KeyboardEvent('keydown', {
// 点击回车发送自动回复
jsStr1 = @"var event = new KeyboardEvent('keydown', {
key: 'Enter',
keyCode: 13,
code: 'Enter',
which: 13
});
txtarea.dispatchEvent(event);";
response1 = shop.Chat.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr1);
response1.Wait(2000);
response1 = shop.Chat.GetBrowser().MainFrame.EvaluateScriptAsync(jsStr1);
response1.Wait(2000);
// 删除队列里的消息
lstAutoReplayParam.RemoveAll(r => r.nickName == autoReplayParam.nickName);
// 删除队列里的消息
lstAutoReplayParam.RemoveAll(r => r.nickName == autoReplayParam.nickName);
var row = dgvShop.Rows.Cast<DataGridViewRow>().FirstOrDefault(x => x.Cells["colShopId"].Value.ToString() == shop.ShopId);
if (row != null)
{
int AutoReplyCount = dicTalkAutoReplyCount.FirstOrDefault(t => t.Key == shop.ShopId).Value.ParseToInt();
AutoReplyCount++;
dicTalkAutoReplyCount[shop.ShopId] = AutoReplyCount;
NaiBa.Util.Helper.ConfigHelper.SaveConfig("wx_talk_auto_reply_count", dicTalkAutoReplyCount);
Invoke(new Action(() =>
var row = dgvShop.Rows.Cast<DataGridViewRow>().FirstOrDefault(x => x.Cells["colShopId"].Value.ToString() == shop.ShopId);
if (row != null)
{
row.Cells["colAutoReplyCount"].Value = AutoReplyCount;
}));
int AutoReplyCount = dicTalkAutoReplyCount.FirstOrDefault(t => t.Key == shop.ShopId).Value.ParseToInt();
AutoReplyCount++;
dicTalkAutoReplyCount[shop.ShopId] = AutoReplyCount;
NaiBa.Util.Helper.ConfigHelper.SaveConfig("wx_talk_auto_reply_count", dicTalkAutoReplyCount);
Invoke(new Action(() =>
{
row.Cells["colAutoReplyCount"].Value = AutoReplyCount;
}));
}
Invoke(new Action(() => { lblAutoReplyStatus.Text = autoReplayParam.shopId + " 已尝试发送自动回复 " + DateTime.Now.ToString("HH:mm:ss"); }));
Thread.Sleep(Randomx.GetId(500, 600));
}
Thread.Sleep(Randomx.GetId(100, 200));
}
}
else
{
Invoke(new Action(() => { lblAutoReplyStatus.Visible = false; }));
}
Thread.Sleep(1000);
}
});