-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoReply.py
More file actions
742 lines (642 loc) · 30.8 KB
/
AutoReply.py
File metadata and controls
742 lines (642 loc) · 30.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
#!/usr/bin/python
import itchat, time, re, sys, urllib, urllib3, json
from itchat.content import *
from urllib import parse #quote,unquote,urlencode
import numpy as np
import requests
import argparse
import base64
API_ENDPOINT = 'http://www.tuling123.com/openapi/api'
API_KEY = 'a98151f5b12a43e88488f71895a195a7'
API_ENDPOINT_pic = 'https://vision.googleapis.com/v1/images:annotate'
API_KEY_pic = 'AIzaSyANGVE1gWK-_PKq7tnp4eKmF1aGkVyjpKk'
#print(itchat.search_chatrooms(name='树群'))
@itchat.msg_register([TEXT])
def textReply(msg):
global index
index = index + 1
print(msg['Text'])
match = re.search('新年', msg['Text']) or re.search('新春', msg['Text']) #.span()
match2 = re.search('电话', msg['Text'])
print(msg['FromUserName'])
print(msg['ToUserName'])
if match:
itchat.send(('助手A:新年好啊!'), msg['FromUserName'])
itchat.send(('助手A:TA不在,发送文本消息,我可以陪您畅所欲言,衣食住行,天文地理,有问必答哦~'), msg['FromUserName'])
itchat.send(('助手A:发送图片,我还可以为您做图片分析呢。'), msg['FromUserName'])
elif match2:
itchat.send(('助手A:您好,请稍候。'))
itchat.send(('助手A:TA不在,发送文本消息,我可以陪您畅所欲言,衣食住行,天文地理,有问必答哦~'), msg['FromUserName'])
itchat.send(('助手A:发送图片,我还可以为您做图片分析呢。'), msg['FromUserName'])
if index == 1:
itchat.send(('系统提示:内容若涉及国家机密,请您遵守所在国家及地区法律。'), msg['FromUserName'])
itchat.send(('助手A:TA不在,发送文本消息,我可以陪您畅所欲言,衣食住行,天文地理,有问必答哦~'), msg['FromUserName'])
itchat.send(('助手A:发送图片,我还可以为您做图片分析呢。'), msg['FromUserName'])
parameters = {
'key': API_KEY,
'info': msg['Text'],
'loc': '波士顿',
'userid': '0'
}
req = requests.post(url = API_ENDPOINT, data = parameters)
try:
replied_data = req.json()
if replied_data['code'] == 100000:
print(replied_data['code'])
print(replied_data['text'])
itchat.send(('助手A: ' + replied_data['text']), msg['FromUserName'])
elif replied_data['code'] == 200000:
print(replied_data['code'])
print(replied_data['text'])
print(replied_data['url'])
itchat.send(('助手A: ' + replied_data['text']), msg['FromUserName'])
itchat.send(('助手A: 请点击如下链接吧,\n' + replied_data['url']), msg['FromUserName'])
elif replied_data['code'] == 302000: #news
print(replied_data['code'])
print(replied_data['text'])
print(replied_data['list'])
news = ''
news_number = 10
news_i = 0
for news_i in range(np.array([len(replied_data['list']), news_number]).min()):
news = news + replied_data['list'][news_i]['article'] + ', url: ' + replied_data['list'][news_i]['detailurl'] + ' --来自' + replied_data['list'][news_i]['source'] + ';' + '\n'
# news_i += 1
itchat.send(('助手A: ' + replied_data['text']), msg['FromUserName'])
itchat.send(('助手A: 请浏览如下内容并点击阅读详情啦,\n' + news), msg['FromUserName'])
elif replied_data['code'] == 308000: #recipe
print(replied_data['code'])
print(replied_data['text'])
print(replied_data['list'])
recipes = ''
recipes_number = 5
recipes_i = 0
for recipes_i in range(np.array([len(replied_data['list']), recipes_number]).min()):
recipes = recipes + replied_data['list'][recipes_i]['name'] + ', url: ' + replied_data['list'][recipes_i]['info'] + ' --详情' + replied_data['list'][recipes_i]['detailurl'] + ';' + '\n'
# recipes_i += 1
itchat.send(('助手A: ' + replied_data['text']), msg['FromUserName'])
itchat.send(('助手A: 请参考如下内容并点击查看详情哦 ~ \n' + recipes), msg['FromUserName'])
elif replied_data['code'] == 313000: #song
print(replied_data['code'])
print(replied_data['text'])
print(replied_data['function'])
itchat.send(('助手A: ' + replied_data['text']), msg['FromUserName'])
itchat.send(('助手A: 请欣赏如下内容并点击仔细品味吧,\n' + replied_data['function']['song'] + ' --来自' + replied_data['function']['singer']), msg['FromUserName'])
elif replied_data['code'] == 314000: #poetry
print(replied_data['code'])
print(replied_data['text'])
print(replied_data['function'])
itchat.send(('助手A: ' + replied_data['text']), msg['FromUserName'])
itchat.send(('助手A: 请欣赏如下内容并点击仔细品味吧,\n' + replied_data['function']['name'] + ' --来自' + replied_data['function']['author']), msg['FromUserName'])
else:
print(replied_data['code'])
print(replied_data['text'])
itchat.send(('助手A: ' + replied_data['text']), msg['FromUserName'])
except:
pass
m = parse.quote(msg['Text'].encode('utf8'))
#m = msg['Text']
#http://api.qingyunke.com/api.php?key=free&appid=0&msg=
#url = 'http://sandbox.api.simsimi.com/request.p?key=9470057a-909b-4e6c-a25d-19e0834d6667&lc=zh&ft=1.0&text='+m
url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg='+m #tuling123, API: http://www.tuling123.com/openapi/api APIkey: a98151f5b12a43e88488f71895a195a7
print(url)
raw = urllib.request.urlopen(url)
try:
data = json.loads(raw.read().decode('utf-8'))
#print data['response']
print(data['content'])
itchat.send(('助手B: ' + data['content']), msg['FromUserName'])
#print('助手B: ' + data['content'])
except:
pass
group_name_list = ['我家(My Family)','娘家人']
#group_list = np.array(['%s'%gname[0] for gname in group_name_list])
group_list = np.array([itchat.search_chatrooms(name='%s'%gname)[0]['UserName'] for gname in group_name_list])
@itchat.msg_register(itchat.content.TEXT, isGroupChat=True)
def group_text_reply(msg):
# sender = msg['FromUserName']
# nick = msg['FromUserName']
# print(itchat.search_chatrooms(name='树群'))
# print(itchat.search_chatrooms(name='我家(My Family)'))
#print(itchat.search_chatrooms(name='树群')[0]['NickName'])
#print(itchat.search_chatrooms(name='我家(My Family)')[0]['NickName'])
# print(itchat.search_chatrooms(name='树群')[0]['UserName'])
# print(itchat.search_chatrooms(name='我家(My Family)')[0]['UserName'])
# print(msg['FromUserName'])
# print(msg['ToUserName'])
if msg['ToUserName'] in [itchat.search_chatrooms(name='我家(My Family)')[0]['UserName']]: #itchat.search_chatrooms(name='树群')[0]['UserName'],
print(msg['Text'])
if msg['FromUserName'] in [itchat.search_chatrooms(name='我家(My Family)')[0]['UserName']] or ('助手A' not in msg['Text'] and '助手A' not in msg['Text'] and 'Picture收到' not in msg['Text'] and '系统提示' not in msg['Text']): #itchat.search_chatrooms(name='树群')[0]['UserName'],
global freqs
freqs = freqs + 1
match3 = re.search('新年', msg['Text']) or re.search('新春', msg['Text']) #.span()
match4 = re.search('电话', msg['Text'])
if match3:
itchat.send(('助手A:新年好啊!'), msg['FromUserName'])
itchat.send(('助手A:TA不在,发送文本消息,我可以陪您畅所欲言,衣食住行,天文地理,有问必答哦~'), msg['FromUserName'])
itchat.send(('助手A:发送图片,我还可以为您做图片分析呢。'), msg['FromUserName'])
elif match4:
itchat.send(('助手A:您好,请稍候。'))
itchat.send(('助手A:TA不在,发送文本消息,我可以陪您畅所欲言,衣食住行,天文地理,有问必答哦~'), msg['FromUserName'])
itchat.send(('助手A:发送图片,我还可以为您做图片分析呢。'), msg['FromUserName'])
if index == 1:
itchat.send(('系统提示:内容若涉及国家机密,请您遵守所在国家及地区法律。'), msg['FromUserName'])
itchat.send(('助手A:TA不在,发送文本消息,我可以陪您畅所欲言,衣食住行,天文地理,有问必答哦~'), msg['FromUserName'])
itchat.send(('助手A:发送图片,我还可以为您做图片分析呢。'), msg['FromUserName'])
parameters = {
'key': API_KEY,
'info': msg['Text'],
'loc': '',
'userid': '0'
}
req = requests.post(url = API_ENDPOINT, data = parameters)
try:
replied_data = req.json()
if replied_data['code'] == 100000:
print(replied_data['code'])
print(replied_data['text'])
itchat.send(('助手A: ' + replied_data['text']), msg['FromUserName'])
elif replied_data['code'] == 200000:
print(replied_data['code'])
print(replied_data['text'])
print(replied_data['url'])
itchat.send(('助手A: ' + replied_data['text']), msg['FromUserName'])
itchat.send(('助手A: 请点击如下链接吧,\n' + replied_data['url']), msg['FromUserName'])
elif replied_data['code'] == 302000: #news
print(replied_data['code'])
print(replied_data['text'])
print(replied_data['list'])
news = ''
news_number = 10
news_i = 0
for news_i in range(np.array([len(replied_data['list']), news_number]).min()):
news = news + replied_data['list'][news_i]['article'] + ', url: ' + replied_data['list'][news_i]['detailurl'] + ' --来自' + replied_data['list'][news_i]['source'] + ';' + '\n'
news_i += 1
itchat.send(('助手A: ' + replied_data['text']), msg['FromUserName'])
itchat.send(('助手A: 请浏览如下内容并点击阅读详情啦,\n' + news), msg['FromUserName'])
elif replied_data['code'] == 308000: #recipe
print(replied_data['code'])
print(replied_data['text'])
print(replied_data['list'])
recipes = ''
recipes_number = 5
recipes_i = 0
for recipes_i in range(np.array([len(replied_data['list']), recipes_number]).min()):
recipes = recipes + replied_data['list'][recipes_i]['name'] + ', url: ' + replied_data['list'][recipes_i]['info'] + ' --详情' + replied_data['list'][recipes_i]['detailurl'] + ';' + '\n'
recipes_i += 1
itchat.send(('助手A: ' + replied_data['text']), msg['FromUserName'])
itchat.send(('助手A: 请参考如下内容并点击查看详情哦 ~ \n' + recipes), msg['FromUserName'])
elif replied_data['code'] == 313000: #song
print(replied_data['code'])
print(replied_data['text'])
print(replied_data['function'])
itchat.send(('助手A: ' + replied_data['text']), msg['FromUserName'])
itchat.send(('助手A: 请欣赏如下内容并点击仔细品味吧,\n' + replied_data['function']['song'] + ' --来自' + replied_data['function']['singer']), msg['FromUserName'])
elif replied_data['code'] == 314000: #poetry
print(replied_data['code'])
print(replied_data['text'])
print(replied_data['function'])
itchat.send(('助手A: ' + replied_data['text']), msg['FromUserName'])
itchat.send(('助手A: 请欣赏如下内容并点击仔细品味吧,\n' + replied_data['function']['name'] + ' --来自' + replied_data['function']['author']), msg['FromUserName'])
else:
print(replied_data['code'])
print(replied_data['text'])
itchat.send(('助手A: ' + replied_data['text']), msg['FromUserName'])
except:
pass
m = parse.quote(msg['Text'].encode('utf8'))
#m = msg['Text']
#http://api.qingyunke.com/api.php?key=free&appid=0&msg=
#url = 'http://sandbox.api.simsimi.com/request.p?key=9470057a-909b-4e6c-a25d-19e0834d6667&lc=zh&ft=1.0&text='+m
url = 'http://api.qingyunke.com/api.php?key=free&appid=0&msg='+m #tuling123, API: http://www.tuling123.com/openapi/api APIkey: a98151f5b12a43e88488f71895a195a7
print(url)
raw = urllib.request.urlopen(url)
try:
data = json.loads(raw.read().decode('utf-8'))
#print data['response']
print(data['content'])
itchat.send(('助手B: ' + data['content']), msg['FromUserName'])
except:
pass
print(itchat.search_chatrooms(userName=msg['FromUserName'])['NickName'])
@itchat.msg_register(itchat.content.PICTURE, isGroupChat=True)
def group_text_reply(msg):
# sender = msg['FromUserName']
# nick = msg['FromUserName']
# print(itchat.search_chatrooms(name='树群'))
# print(itchat.search_chatrooms(name='我家(My Family)'))
#print(itchat.search_chatrooms(name='树群')[0]['NickName'])
#print(itchat.search_chatrooms(name='我家(My Family)')[0]['NickName'])
# print(itchat.search_chatrooms(name='树群')[0]['UserName'])
# print(itchat.search_chatrooms(name='我家(My Family)')[0]['UserName'])
# print(msg['FromUserName'])
# print(msg['ToUserName'])助手A
print('Content: ' + msg['FileName'] + '\n')
print('FileName: ' + msg.fileName + '\n')
print('MsgId: ' + msg['MsgId'] + '\n')
print('Text: ' + msg.content + '\n')
#print('Url: ' + msg.url + '\n')
#print('Data: ' + msg.data + '\n')
# print('CommentUrl: ' + msg.commenturl + '\n')
# print('Imag: ' + msg.img + '\n')
# print(f)
# print(fd)
#print(itchat.search_chatrooms(userName=msg['FromUserName'])['NickName'])
if msg['FromUserName'] in [itchat.search_chatrooms(name='我家(My Family)')[0]['UserName']] or msg['ToUserName'] in [itchat.search_chatrooms(name='我家(My Family)')[0]['UserName']]: #itchat.search_chatrooms(name='树群')[0]['UserName'],
global freqs2
freqs2 = freqs2 + 1
if freqs2 <= 1:
itchat.send(('系统提示:内容若涉及国家机密,请您遵守所在国家及地区法律。'), msg['FromUserName'])
itchat.send(('助手A:TA不在,发送文本消息,我可以陪您畅所欲言,衣食住行,天文地理,有问必答哦~'), msg['FromUserName'])
itchat.send(('助手A:发送图片,我还可以为您做图片分析呢。'), msg['FromUserName'])
msg.download(msg.fileName)
# itchat.send('@%s@%s' % (
# 'img', msg['FileName']),
# msg['FromUserName'])
# with open(msg.fileName, 'wb') as f:
# f.write(msg.fileName)
# with open(msg.fileName, 'wb') as fd:
# fd.write(msg.download(msg.fileName))
itchat.send(('助手A:收到~'), msg['FromUserName'])
image_filename = msg['FileName']
image_filename_ne = image_filename.split('.')[0]
parameters_pic_raw = {
"requests": [
{
"image": {
"content": base64.b64encode(open(image_filename,'rb').read()).decode('UTF-8')
},
"features": [
{
"type": "FACE_DETECTION",
"maxResults": "10"
},
{
"type": "LABEL_DETECTION",
"maxResults": "10"
},
{
"type": "TEXT_DETECTION",
"maxResults": "10"
},
{
"type": "LANDMARK_DETECTION",
"maxResults": "10"
},
{
"type": "WEB_DETECTION",
"maxResults": "10"
}
]
}
]
}
output_filename = 'vision_%s.json'%image_filename_ne
with open(output_filename, 'w') as output_file:
json.dump(parameters_pic_raw, output_file, indent = 4)
try:
parameters_pic = open(output_filename,'rb').read()
response_pic = requests.post(url = API_ENDPOINT_pic+'?key='+API_KEY_pic, data = parameters_pic)
vision_results = response_pic.json()
except:
pass
try:
itchat.send(('助手A:\n 图片文字:' + vision_results['responses'][0]['textAnnotations'][0]['description']), msg['FromUserName'])
except:
pass
try:
itchat.send(('助手A:\n 图片内容分析:' + str([vision_results['responses'][0]['labelAnnotations'][index]['description'] for index in range(len(vision_results['responses'][0]['labelAnnotations']))])[1:-1]), msg['FromUserName'])
except:
pass
#vision_results['responses'][0]['webDetection']['bestGuessLabels'][0]['label']
#itchat.send(('助手A:\n 相似图片:' + vision_results['responses'][0]['webDetection']['visuallySimilarImages'][0]['url']), msg['FromUserName'])
try:
itchat.send(('助手A:\n 相似图片:' + str([vision_results['responses'][0]['webDetection']['visuallySimilarImages'][ind]['url'] for ind in range(len(vision_results['responses'][0]['webDetection']['visuallySimilarImages']))])[1:-1]), msg['FromUserName'])
except:
pass
try:
vision_results_face_raw = vision_results['responses'][0]['faceAnnotations'][0]
vision_results_face = dict(vision_results_face_raw)
del vision_results_face['landmarks']
itchat.send(('助手A:\n 人脸分析:' + str(vision_results_face)[1:-1]), msg['FromUserName'])
except:
pass
try:
vision_results_landmark = ('分析师A:\n 地标分析:' + str([(vision_results['responses'][0]['landmarkAnnotations'][index]['description'], vision_results['responses'][0]['landmarkAnnotations'][index]['locations']) for index in range(len(vision_results['responses'][0]['landmarkAnnotations']))])[1:-1])
itchat.send(('助手A:\n 人脸分析:' + str(vision_results_landmark)), msg['FromUserName'])
print(vision_results_landmark)
except:
pass
# parameters_pic = open(output_filename,'rb').read()
# response_pic = requests.post(url = API_ENDPOINT_pic+'?key='+API_KEY_pic, data = parameters_pic)
#
# vision_results = response_pic.json()
## try:
# itchat.send(('助手A:\n 图片文字:' + vision_results['responses'][0]['textAnnotations'][0]['description']), msg['FromUserName'])
# itchat.send(('助手A:\n 图片内容分析:' + str([vision_results['responses'][0]['labelAnnotations'][index]['description'] for index in range(len(vision_results['responses'][0]['labelAnnotations']))])[1:-1]), msg['FromUserName'])
# #vision_results['responses'][0]['webDetection']['bestGuessLabels'][0]['label']
# itchat.send(('助手A:\n 联想图片:' + str(vision_results['responses'][0]['webDetection']['bestGuessLabels'][0]['label'])+ ', ' + str([vision_results['responses'][0]['webDetection']['visuallySimilarImages'][ind]['url'] for ind in range(len(vision_results['responses'][0]['webDetection']['visuallySimilarImages']))])[1:-1]), msg['FromUserName'])
# vision_results_face_raw = vision_results['responses'][0]['faceAnnotations'][0]
# vision_results_face = dict(vision_results_face_raw)
# del vision_results_face['landmarks']
# itchat.send(('助手A:\n 人脸分析:' + str(vision_results_face)[1:-1]), msg['FromUserName'])
# except:
# pass
#return '%s received' % msg['Type']
@itchat.msg_register(itchat.content.SHARING, isGroupChat=True)
def group_text_reply(msg):
# sender = msg['FromUserName']
# nick = msg['FromUserName']
# print(itchat.search_chatrooms(name='树群'))
# print(itchat.search_chatrooms(name='我家(My Family)'))
# print(itchat.search_chatrooms(name='树群')[0]['NickName'])
# print(itchat.search_chatrooms(name='我家(My Family)')[0]['NickName'])
# print(itchat.search_chatrooms(name='树群')[0]['UserName'])
# print(itchat.search_chatrooms(name='我家(My Family)')[0]['UserName'])
# print(msg['FromUserName'])
# print(msg['ToUserName'])
if msg['FromUserName'] in [itchat.search_chatrooms(name='我家(My Family)')[0]['UserName']]: #itchat.search_chatrooms(name='树群')[0]['UserName'],
global freqs3
freqs3 = freqs3 + 1
if freqs3 <= 1:
itchat.send(('系统提示:内容若涉及国家机密,请您遵守所在国家及地区法律。'), msg['FromUserName'])
itchat.send(('助手A:TA不在,发送文本消息,我可以陪您畅所欲言,衣食住行,天文地理,有问必答哦~'), msg['FromUserName'])
itchat.send(('助手A:发送图片,我还可以为您做图片分析呢。'), msg['FromUserName'])
itchat.send(('助手A:已接收~'), msg['FromUserName'])
print(itchat.search_chatrooms(userName=msg['FromUserName'])['NickName'])
@itchat.msg_register(itchat.content.VIDEO, isGroupChat=True)
def group_text_reply(msg):
# sender = msg['FromUserName']
# nick = msg['FromUserName']
# print(itchat.search_chatrooms(name='树群'))
# print(itchat.search_chatrooms(name='我家(My Family)'))
# print(itchat.search_chatrooms(name='树群')[0]['NickName'])
# print(itchat.search_chatrooms(name='我家(My Family)')[0]['NickName'])
# print(itchat.search_chatrooms(name='树群')[0]['UserName'])
# print(itchat.search_chatrooms(name='我家(My Family)')[0]['UserName'])
# print(msg['FromUserName'])
# print(msg['ToUserName'])
if msg['FromUserName'] in [itchat.search_chatrooms(name='我家(My Family)')[0]['UserName']]: #itchat.search_chatrooms(name='树群')[0]['UserName'],
global freqs4
freqs4 = freqs4 + 1
if freqs4 <= 1:
itchat.send(('系统提示:内容若涉及国家机密,请您遵守所在国家及地区法律。'), msg['FromUserName'])
itchat.send(('助手A:TA不在,发送文本消息,我可以陪您畅所欲言,衣食住行,天文地理,有问必答哦~'), msg['FromUserName'])
itchat.send(('助手A:发送图片,我还可以为您做图片分析呢。'), msg['FromUserName'])
itchat.send(('助手A:%s收到啦~' % msg['Type']), msg['FromUserName'])
print(itchat.search_chatrooms(userName=msg['FromUserName'])['NickName'])
@itchat.msg_register(itchat.content.RECORDING, isGroupChat=True)
def group_text_reply(msg):
# sender = msg['FromUserName']
# nick = msg['FromUserName']
# print(itchat.search_chatrooms(name='树群'))
# print(itchat.search_chatrooms(name='我家(My Family)'))
# print(itchat.search_chatrooms(name='树群')[0]['NickName'])
# print(itchat.search_chatrooms(name='我家(My Family)')[0]['NickName'])
# print(itchat.search_chatrooms(name='树群')[0]['UserName'])
# print(itchat.search_chatrooms(name='我家(My Family)')[0]['UserName'])
# print(msg['FromUserName'])
# print(msg['ToUserName'])
if msg['FromUserName'] in [itchat.search_chatrooms(name='我家(My Family)')[0]['UserName']]: #itchat.search_chatrooms(name='树群')[0]['UserName'],
global freqs5
freqs5 = freqs5 + 1
if freqs5 <= 1:
itchat.send(('系统提示:内容若涉及国家机密,请您遵守所在国家及地区法律。'), msg['FromUserName'])
itchat.send(('助手A:TA不在,发送文本消息,我可以陪您畅所欲言,衣食住行,天文地理,有问必答哦~'), msg['FromUserName'])
itchat.send(('助手A:发送图片,我还可以为您做图片分析呢。'), msg['FromUserName'])
itchat.send(('助手A:%s已收到' % msg['Type']), msg['FromUserName'])
print(itchat.search_chatrooms(userName=msg['FromUserName'])['NickName'])
@itchat.msg_register([VIDEO])
def videoReply(msg):
order = 0
if order <=1:
itchat.send(('系统提示:内容若涉及国家机密,请您遵守所在国家及地区法律。'), msg['FromUserName'])
itchat.send(('助手A:这不方便说话,文字比较好啦。'), msg['FromUserName'])
itchat.send(('助手A:TA不在,发送文本消息,我可以陪您畅所欲言,衣食住行,天文地理,有问必答哦~'), msg['FromUserName'])
itchat.send(('助手A:发送图片,我还可以为您做图片分析呢。'), msg['FromUserName'])
itchat.send(('助手A:收到。'), msg['FromUserName'])
@itchat.msg_register([PICTURE])
def picReply(msg):
order = 0
if order <=1:
itchat.send(('系统提示:内容若涉及国家机密,请您遵守所在国家及地区法律。'), msg['FromUserName'])
itchat.send(('助手A:TA不在,发送文本消息,我可以陪您畅所欲言,衣食住行,天文地理,有问必答哦~'), msg['FromUserName'])
itchat.send(('助手A:发送图片,我还可以为您做图片分析呢。'), msg['FromUserName'])
print(msg['FileName'])
print(msg['MsgId'])
#print(msg['Url'])
msg.download(msg.fileName)
# itchat.send('@%s@%s' % (
# 'img', msg['FileName']),
# msg['FromUserName'])
# with open(msg.fileName, 'wb') as f:
# f.write(msg.download())
itchat.send(('助手A:%s收到~' % msg['Type']), msg['FromUserName'])
image_filename = msg['FileName']
image_filename_ne = image_filename.split('.')[0]
parameters_pic_raw = {
"requests": [
{
"image": {
"content": base64.b64encode(open(image_filename,'rb').read()).decode('UTF-8')
},
"features": [
{
"type": "FACE_DETECTION",
"maxResults": "10"
},
{
"type": "LABEL_DETECTION",
"maxResults": "10"
},
{
"type": "TEXT_DETECTION",
"maxResults": "10"
},
{
"type": "LANDMARK_DETECTION",
"maxResults": "10"
},
{
"type": "WEB_DETECTION",
"maxResults": "10"
}
]
}
]
}
output_filename = 'vision_%s.json'%image_filename_ne
with open(output_filename, 'w') as output_file:
json.dump(parameters_pic_raw, output_file, indent = 4)
try:
parameters_pic = open(output_filename,'rb').read()
response_pic = requests.post(url = API_ENDPOINT_pic+'?key='+API_KEY_pic, data = parameters_pic)
vision_results = response_pic.json()
except:
pass
try:
itchat.send(('助手A:\n 图片文字:' + vision_results['responses'][0]['textAnnotations'][0]['description']), msg['FromUserName'])
except:
pass
try:
itchat.send(('助手A:\n 图片内容分析:' + str([vision_results['responses'][0]['labelAnnotations'][index]['description'] for index in range(len(vision_results['responses'][0]['labelAnnotations']))])[1:-1]), msg['FromUserName'])
except:
pass
#vision_results['responses'][0]['webDetection']['bestGuessLabels'][0]['label']
#itchat.send(('助手A:\n 相似图片:' + vision_results['responses'][0]['webDetection']['visuallySimilarImages'][0]['url']), msg['FromUserName'])
try:
itchat.send(('助手A:\n 相似图片:' + str([vision_results['responses'][0]['webDetection']['visuallySimilarImages'][ind]['url'] for ind in range(len(vision_results['responses'][0]['webDetection']['visuallySimilarImages']))])[1:-1]), msg['FromUserName'])
except:
pass
try:
vision_results_face_raw = vision_results['responses'][0]['faceAnnotations'][0]
vision_results_face = dict(vision_results_face_raw)
del vision_results_face['landmarks']
itchat.send(('助手A:\n 人脸分析:' + str(vision_results_face)[1:-1]), msg['FromUserName'])
except:
pass
try:
vision_results_landmark = ('分析师A:\n 地标分析:' + str([(vision_results['responses'][0]['landmarkAnnotations'][index]['description'], vision_results['responses'][0]['landmarkAnnotations'][index]['locations']) for index in range(len(vision_results['responses'][0]['landmarkAnnotations']))])[1:-1])
print(vision_results_landmark)
except:
pass
# except:
# pass
#return '%s received' % msg['Type']
#@itchat.msg_register([PICTURE])
#def download_files(msg):
# msg.download(msg.fileName)
# itchat.send('@%s@%s' % (
# 'img', msg['FileName']),
# msg['FromUserName'])
## with open(msg.fileName, 'wb') as f:
## f.write(msg.download())
# itchat.send(('%s received' % msg['Type']), msg['FromUserName'])
#
# image_filename = msg['FileName']
# image_filename_ne = image_filename.split('.')[0]
# parameters_pic_raw = {
# "requests": [
# {
# "image": {
# "content": base64.b64encode(open(image_filename,'rb').read()).decode('UTF-8')
# },
# "features": [
# {
# "type": "FACE_DETECTION",
# "maxResults": "10"
# },
# {
# "type": "LABEL_DETECTION",
# "maxResults": "10"
# },
# {
# "type": "TEXT_DETECTION",
# "maxResults": "10"
# },
# {
# "type": "LANDMARK_DETECTION",
# "maxResults": "10"
# },
# {
# "type": "WEB_DETECTION",
# "maxResults": "10"
# }
# ]
# }
# ]
# }
# output_filename = 'vision_%s.json'%image_filename_ne
#
# with open(output_filename, 'w') as output_file:
# json.dump(parameters_pic_raw, output_file, indent = 4)
#
# parameters_pic = open(output_filename,'rb').read()
# response_pic = requests.post(url = API_ENDPOINT_pic+'?key='+API_KEY_pic, data = parameters_pic)
#
# vision_results = response_pic.json()
# itchat.send(('助手A:\n 图片文字:' + vision_results['responses'][0]['textAnnotations'][0]['description']), msg['FromUserName'])
# itchat.send(('助手A:\n 图片内容分析:' + str([vision_results['responses'][0]['labelAnnotations'][index]['description'] for index in range(len(vision_results['responses'][0]['labelAnnotations']))])[1:-1]), msg['FromUserName'])
# #vision_results['responses'][0]['webDetection']['bestGuessLabels'][0]['label']
# itchat.send(('助手A:\n 相似图片:' + vision_results['responses'][0]['webDetection']['visuallySimilarImages'][0]['url']), msg['FromUserName'])
# #return '%s received' % msg['Type']
#@itchat.msg_register([RECORDING, ATTACHMENT, VIDEO])
#def download_files(msg):
# f = open(msg.fileName, 'wb')
# print(f)
@itchat.msg_register([RECORDING])
def recordReply(msg):
order = 0
if order <=1:
itchat.send(('系统提示:内容若涉及国家机密,请您遵守所在国家及地区法律。'), msg['FromUserName'])
itchat.send(('助手A:TA不在,发送文本消息,我可以陪您畅所欲言,衣食住行,天文地理,有问必答哦~'), msg['FromUserName'])
itchat.send(('助手A:发送图片,我还可以为您做图片分析呢。'), msg['FromUserName'])
itchat.send(('助手A:%s收到啦' % msg['Type']), msg['FromUserName'])
@itchat.msg_register([SHARING])
def sharingReply(msg):
order = 0
if order <=1:
itchat.send(('系统提示:内容若涉及国家机密,请您遵守所在国家及地区法律。'), msg['FromUserName'])
itchat.send(('助手A:TA不在,发送文本消息,我可以陪您畅所欲言,衣食住行,天文地理,有问必答哦~'), msg['FromUserName'])
itchat.send(('助手A:发送图片,我还可以为您做图片分析呢。'), msg['FromUserName'])
itchat.send(('助手A:%s已接收' % msg['Type']), msg['FromUserName'])
# pass
#@itchat.msg_register([PICTURE])
#def download_files(msg):
# msg.download(msg.fileName)
# itchat.send('@%s@%s' % (
# 'img', msg['FileName']),
# msg['FromUserName'])
# return '%s received' % msg['Type']
if __name__ == '__main__':
index = 0
freqs = 0
freqs2 = 0
freqs3 = 0
freqs4 = 0
freqs5 = 0
itchat.auto_login(enableCmdQR=False,hotReload=False)
itchat.run()
#data = open('request.json', 'rb').read()
# 李TA @@3a3683f0a1f883491e0e9f0d3d7787a0ccac8517cad1c2a47924395933229462
# 李TA(Victor)@07be37eb8d53d09e8b6a1d83110ab507a9dfa9a35ea338ba778da235d58d2bdd
# 树群 @@1304c51149762594d5ce4e7256d5771315262722996a68019cb8cd6964354fc4
# 我家(My Family) @@20e51126fe77612b69daa7f7b45a500f214697b1b7a2fb3cd49cf04d55f755cd
#@itchat.msg_register([TEXT])
#def text_reply(msg):
# match = re.search('年', msg['Text']).span()
# if match:
# itchat.send(('新年快乐啊!'), msg['FromUserName'])
# else:
# itchat.send(('系统错误:内容涉及涉及国家机密。'))
#
#@itchat.msg_register([PICTURE, RECORDING, VIDEO, SHARING])
#
#def other_reply(msg):
# itchat.send(('系统错误:内容涉及涉及国家机密。'))
#
#@itchat.msg_register([VIDEO])
#def videoReply(msg):
# itchat.send(('2333!Got it!'), msg['FromUserName'])
#
#@itchat.msg_register([PICTURE])
#def picReply(msg):
# itchat.send(('系统错误:内容涉及涉及国家机密。'))
#
#@itchat.msg_register([RECORDING])
#def recordReply(msg):
# itchat.send(('系统错误:内容涉及涉及国家机密。'))
#
#@itchat.msg_register([SHARING])
#def sharingReply(msg):
# itchat.send(('系统错误:内容涉及涉及国家机密。'))
#
#if __name__ == '__main__':
#
# itchat.auto_login(enableCmdQR=False,hotReload=False)
# itchat.run()
#itchat.auto_login(True)
#itchat.run()
#def other_reply(msg):
# itchat.send(('系统错误:内容涉及涉及国家机密。'))
#itchat.auto_login(enableCmdQR=True, hotReload=True)
#itchat.run()