作者 邓超

auto

@@ -17,98 +17,62 @@ class AutoMail { @@ -17,98 +17,62 @@ class AutoMail {
17 } 17 }
18 18
19 /** 19 /**
20 - * shopk那边的预热邮箱  
21 - * @var array  
22 - */  
23 - private $shopkHotEmail = [];  
24 -  
25 - /**  
26 * @var \Lib\Db|\Lib\DbPool 20 * @var \Lib\Db|\Lib\DbPool
27 */ 21 */
28 private $db; 22 private $db;
29 23
30 24
31 - private $fids = [];  
32 -  
33 /** 25 /**
34 * @author:dc 26 * @author:dc
35 * @time 2024/7/18 14:04 27 * @time 2024/7/18 14:04
36 */ 28 */
37 private function start(){ 29 private function start(){
38 30
39 - $this->fids = $this->db->all("select `id` from `folders` where `folder` = '收件箱'");  
40 - $this->fids = array_column($this->fids,'id');  
41 -  
42 - $filter = @file_get_contents('https://fob.ai.cc/api/mail/ai_inbox_filter/'.md5('aicc.'.date('ymdh')));  
43 - $filter = @json_decode($filter,true);  
44 - if(!is_array($filter)){  
45 - return 0;  
46 - }  
47 - array_map(function ($v){  
48 - $this->filter[] = [  
49 - $v['type'],  
50 - $v['text'],  
51 - ];  
52 - },$filter[0]);  
53 -  
54 - if($this->filter){  
55 - redis()->set('ai_email_filter_lists',$this->filter,86400);  
56 - }  
57 -  
58 -  
59 - if(redis()->add('auto_mail_sync2',1,60)){  
60 - _echo('正在计算数据');  
61 - $maxId = $this->db->value("select `id` from `lists` order by `id` desc limit 1");  
62 - $startId = $maxId-200000;  
63 -  
64 - foreach (minMaxToArray($startId,$maxId) as $ids){  
65 - redis()->rPush('auto_check_ids',implode(',',$ids));  
66 - }  
67 -  
68 - _echo('计算完成');  
69 - }  
70 -  
71 -  
72 - while (1){  
73 - $ids = redis()->lPop('auto_check_ids');  
74 - if($ids){  
75 - $ids = explode(',',$ids);  
76 - $this->run($ids);  
77 - }else{  
78 - break;  
79 - }  
80 - }  
81 -  
82 - } 31 + $filter = isAiAutoMail(true,true);
83 32
84 - private $filter = []; 33 + $es = es('email_lists');
85 34
86 - private function run($id){  
87 - $list = $this->db->all(\Model\listsSql::all(dbWhere(['id'=>$id]),'`id`,`from`,`subject`,`folder_id`'));  
88 - foreach ($list as $item){  
89 - // 必须是收件箱  
90 - if(in_array($item['folder_id'],$this->fids)){  
91 - foreach ($this->filter as $f){ 35 + $not_must = [];
  36 + foreach ($filter as $f){
92 list($t,$str) = $f; 37 list($t,$str) = $f;
93 - $haystack = '';  
94 if($t==2){ 38 if($t==2){
95 - $haystack = $item['subject']; 39 + $not_must[] = ["match_phrase"=>["subject"=>$str]];
96 }elseif ($t==1){ 40 }elseif ($t==1){
97 - $haystack = $item['from'];  
98 - }  
99 - if(stripos($haystack,$str)!==false){  
100 - if(!$this->db->count("select count(*) from `lists_auto` where `list_id` = ".$item['id'])){  
101 - _echo("插入数据 ".$item['id'].'==>'.$this->db->create('lists_auto',['list_id'=>$item['id']],false).'==>'.$haystack);  
102 - }  
103 - break;  
104 - } 41 + $not_must[] = ["match_phrase"=>["from.email"=>$str]];
105 } 42 }
106 } 43 }
107 44
  45 + // 查询自动回的邮件
  46 + $lists = $es->search([
  47 + "_source" => ["uuid"],
  48 + "query"=>[
  49 + "bool"=>[
  50 + "must"=>[
  51 + ["term"=>["folder_as_int" => 1]], // 收件箱
  52 + ["term"=>["is_auto" => 0]], // 非auto
  53 + ["term"=>["is_hots" => 0]], // 非预热
  54 + ["term"=>["deleted" => 0]], // 非删除
  55 + // 自动关键字
  56 + [
  57 + "bool" => [
  58 + "should"=> $not_must,
  59 + "minimum_should_match" => 1
  60 + ]
  61 + ]
  62 + ]
  63 + ]
  64 + ]
  65 + ],0,1000,[],1000);
  66 +
  67 + foreach ($lists['hits']['hits']??[] as $list){
  68 + $id = intval($list['_source']['uuid']??0);
  69 + if($id){
  70 + _echo("id = ".$id);
  71 + redis()->rPush('sync_to_es',$id);
108 } 72 }
109 } 73 }
110 74
111 - 75 + }
112 76
113 } 77 }
114 78
1 -<?php  
2 -  
3 -//error_reporting();  
4 -  
5 -require_once "../vendor/autoload.php";  
6 -  
7 -  
8 -if(in_array('v2',$argv)){  
9 - define('S_V2','2');  
10 -}else{  
11 - define('S_V2','');  
12 -}  
13 -  
14 -/**  
15 - * 把mysql的数据同步到es  
16 - * @author:dc  
17 - * @time 2025/3/4 10:19  
18 - * Class SyncToEsCmd  
19 - */  
20 -class SyncToEsCmd {  
21 -  
22 - public $isStop = false;  
23 -  
24 - /**  
25 - * 文件夹  
26 - * @var array  
27 - */  
28 - public $folders = [];  
29 -  
30 - /**  
31 - * @var \Lib\Db  
32 - */  
33 - public $fob_db;  
34 -  
35 - public function handler(){  
36 -  
37 - $handler = function ($signal){  
38 - _echo('收到进程信号 '. $signal);  
39 - // 可以处理其他程序  
40 - $this->isStop = true;  
41 - };  
42 - pcntl_signal(SIGTERM, $handler); // 这个是kill  
43 - pcntl_signal(SIGINT, $handler); // 这个是 ctrl+c  
44 -// pcntl_signal(SIGHUP, $handler);  
45 -  
46 -  
47 - if(S_V2){  
48 - $es = es('email_lists');  
49 - }else{  
50 - $es = es();  
51 - }  
52 -  
53 - $db = db();  
54 - $this->fob_db = fob_mysql();  
55 - $startTime = time();  
56 - while (1){  
57 -  
58 - if(time()-43200 > $startTime){  
59 - break;  
60 - }  
61 -  
62 - // 检查是否接收到信号  
63 - pcntl_signal_dispatch();  
64 -  
65 - if($this->isStop) {  
66 - _echo('已退出进程');  
67 - break;  
68 - }  
69 -  
70 - $id = redis()->lPop('sync_to_es'.S_V2);  
71 - $code = 500;  
72 - if($id){  
73 - $doc_id = '';  
74 - try {  
75 - $data = $db->throw()->first(\Model\listsSql::first('`id` = '.$id));  
76 - }catch (Throwable $e){  
77 - redis()->rPush('sync_to_es'.S_V2,$id);  
78 - _echo('sync to es '.$e->getMessage());  
79 - break;  
80 - }  
81 -  
82 - if($data){  
83 - // 设置 进程 是否在运行  
84 - $data['is_auto']=$db->count('select count(*) from `lists_auto` where `list_id` = '.$data['id']) ? 1 : 0;  
85 - try {  
86 - // 文件夹  
87 - if(empty($this->folders[$data['folder_id']])){  
88 - $this->folders[$data['folder_id']] = $db->throw()->value(\Model\folderSql::first($data['folder_id'],'folder'));  
89 - }  
90 - // 再次验证是否是自动回复  
91 - if($this->folders[$data['folder_id']] == '收件箱' && !$data['is_auto']){  
92 - $filter = redis()->get('ai_email_filter_lists',[]);  
93 - $filter = is_array($filter) ? $filter : [];  
94 - foreach ($filter as $f){  
95 - list($t,$str) = $f;  
96 - $haystack = '';  
97 - if($t==2){  
98 - $haystack = $data['subject'];  
99 - }elseif ($t==1){  
100 - $haystack = $data['from'];  
101 - }  
102 - if($haystack && $str && stripos($haystack,$str)!==false){  
103 - $data['is_auto'] = 1;  
104 - break;  
105 - }  
106 - }  
107 - }  
108 -  
109 - // 为文件夹打标 方便查询  
110 - $data['folder_as_int'] = folder2int($this->folders[$data['folder_id']]);  
111 - // postid ai邮箱要用 这个是查询黑格  
112 - $data['postid'] = $this->getPostid($data['email_id']);  
113 - }catch (Throwable $e){  
114 - redis()->rPush('sync_to_es'.S_V2,$id);  
115 - _echo('sync to es '.$e->getMessage());  
116 - break;  
117 - }  
118 -  
119 - $data = $this->getEsData($data);  
120 - $doc_id = $data['email_id'].'_'.$data['folder_id'].'_'.$data['uid'];  
121 -  
122 - $code = $es->save($doc_id,$data);  
123 - }  
124 -  
125 - if($code!==200){  
126 - @file_put_contents(LOG_PATH.'/sync_es_fail'.S_V2.'.log',$id."\n",FILE_APPEND);  
127 - _echo('同步es: '.$doc_id.'===>'.$code);  
128 - }  
129 -  
130 - }else{  
131 - sleep(1);  
132 - }  
133 - }  
134 -  
135 - }  
136 -  
137 - protected $postids = [];  
138 -  
139 - /**  
140 - * 项目id  
141 - * @author:dc  
142 - * @time 2025/5/20 15:44  
143 - */  
144 - public function getPostid($email_id){  
145 - $h = date('dh');  
146 - if(!isset($this->postids[$h][$email_id])){  
147 - // 未删除状态  
148 - $id = (int) $this->fob_db->throw()->value("select `post_id` from `e_mail_binds` where `email_id` = '{$email_id}' and `deleted_at` is null order by `id` desc limit 1 ");  
149 - if(!$id){  
150 - // 已删状态  
151 - $id = (int) $this->fob_db->throw()->value("select `post_id` from `e_mail_binds` where `email_id` = '{$email_id}' order by `id` desc limit 1 ");  
152 - }  
153 - $this->postids[$h][$email_id] = $id;  
154 - }  
155 -  
156 - return $this->postids[$h][$email_id];  
157 -  
158 - return 0;  
159 - }  
160 -  
161 -  
162 - public function getEsData($data){  
163 - if(!empty($data['id'])){  
164 - $data['uuid'] = $data['id'];  
165 - unset($data['id']);  
166 - }  
167 -  
168 - $data['from'] = [  
169 - 'email' => $data['from'],  
170 - 'name' => $data['from_name']??''  
171 - ];  
172 - unset($data['from_name']);  
173 - unset($data['date']);  
174 -  
175 - if(!empty($data['to_name']) && strlen($data['to_name'])>10000){  
176 - $data['to_name'] = @json_decode($data['to_name'],true);  
177 - if(is_array($data['to_name'])){  
178 - $data['to_name'] = array_map(function ($v){  
179 - $v['name'] = '';  
180 - return $v;  
181 - },$data['to_name']);  
182 - $data['to_name'] = array_slice($data['to_name'],0,100);  
183 - $data['to_name'] = json_encode($data['to_name']);  
184 - }else{  
185 - $data['to_name'] = '';  
186 - }  
187 - }  
188 - $data['description'] = str_replace(["\n"],"",$data['description']);  
189 - // unset($data['to_name']);  
190 -  
191 - if(!empty($data['created_at'])){  
192 - $data['created_at'] = date('Y-m-d\TH:i:s',strtotime($data['created_at']));  
193 - }  
194 - if(!empty($data['updated_at'])){  
195 - $data['updated_at'] = date('Y-m-d\TH:i:s',strtotime($data['updated_at']));  
196 - }  
197 - $data['references'] = empty($data['references']) ? '' : $data['references'];  
198 - return $data;  
199 - }  
200 -  
201 -  
202 -}  
203 -  
204 -(new SyncToEsCmd())->handler();  
205 -  
206 -return 1;  
207 -  
208 -  
209 -  
210 -  
211 -  
212 -  
213 -  
214 -  
215 -  
@@ -492,7 +492,7 @@ function isAiAutoMail($from,$subject,$body=''){ @@ -492,7 +492,7 @@ function isAiAutoMail($from,$subject,$body=''){
492 } 492 }
493 493
494 // 是否是自动回复 494 // 是否是自动回复
495 - foreach ($temp($from===true&&$subject===true) as $f){ 495 + foreach ($temp() as $f){
496 list($t,$str) = $f; 496 list($t,$str) = $f;
497 $haystack = ''; 497 $haystack = '';
498 if($t==2){ 498 if($t==2){