# -*- coding: UTF-8 -*-
from wxpy import *
import datetime
import os
import threading
import time
import datetime
import openpyxl_excel
import Timer
#bot = Bot()
bot = Bot(cache_path = True)
bot.messages.max_history = 10000
'''
@bot.register([Friend], SHARING)
def auto_monitor_friend(msg):
with cond :
print(u'收到一条好友分享消息', msg)
'''
tuling = Tuling(api_key='3d131c2185d44aa88def35ac37b5c9ab')
cond = threading.Condition() #线程条件变量
def deamonMsg() :
with cond :
msgCount = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(u'发送消息给文件夹助手,保持连接活跃。消息内容:', msgCount)
bot.file_helper.send(msgCount)
image_cache_path = os.path.join(os.getcwd(), 'image_cache')
timerInterval = 3
orderHeader = 'order:'
#timerInterval = 30 * 60
t = Timer.Timer(deamonMsg, timerInterval)#创建线程 30分钟给自己发一条消息
t.setDaemon(True)
t.start()
def restartTimer() :
global t
if t.is_alive() :
t.stop()
t = Timer.Timer(deamonMsg, timerInterval)
t.setDaemon(True)
t.start()
# 注册好友请求类消息
@bot.register(msg_types = FRIENDS)
def auto_accept_friends(msg):
with cond :
# 接受好友 (msg.card 为该请求的用户对象)
new_friend = bot.accept_friend(msg.card)
new_friend.send(u'图灵接受了你的好友请求,我们可以开始聊天了')
def __dealwith_order(msg) :
orderstr = msg.text.lower()#不区分大小写
orderstr = orderstr[len(orderHeader):]
if orderstr == "1" :
bot.registered.enable(auto_accept_friends)
msg.reply('自动接收好友请求已开启')
elif orderstr == "2" :
bot.registered.disable(auto_accept_friends)
msg.reply('自动接收好友请求已关闭')
elif orderstr == "3" :
if t.is_alive() :
if t.is_pause() :
msg.reply('后台线程已挂起')
else :
msg.reply('后台线程运行正常')
else :
msg.reply('后台线程已退出')
elif orderstr == "4" :
t.resume()
msg.reply('后台线程已恢复')
elif orderstr == "5" :
t.pause()
msg.reply('后台线程已挂起')
elif orderstr == "6" :
restartTimer()
msg.reply('后台线程已重启')
elif orderstr.startswith("7 ") :
global timerInterval
timerInterval = int(orderstr[2:].strip())
restartTimer()
msg.reply('后台线程刷新间隔已修改:{0}'.format(timerInterval))
else :
msg.reply('指令:order+序号\n1、开启自动接收好友请求\n2、关闭自动接收好友请求\n3、查看后台线程是否活跃\n4、恢复后台线程\n5、挂起后台线程\n6、重新启动后台线程\n')
@bot.register([Friend])
def auto_monitor_friend_all(msg):
with cond :
if msg.text.startswith(orderHeader) and msg.sender.name in destusers :
print(u'收到一条好友指令消息:', msg.text)
__dealwith_order(msg)
else :
print(u'收到一条好友消息', msg)
if msg.type == PICTURE :
image_cache = image_cache_path + '/' + msg.file_name
msg.get_file(image_cache)
tuling.do_reply(msg)
@bot.register([Group])
def auto_monitor_group(msg):
writeMsg(msg)
def writeMsg(msg) :
datas = []
data = []
with cond :
#指定群消息
#指定群用户消息
if (msg.member == None or msg.sender.name not in destgroups) and msg.member.name not in destusers:
return
if msg.is_at :
tuling.do_reply(msg)
if msg.type == PICTURE :
print(u'收到一条群图片消息', msg)
image_cache = image_cache_path + '/' + msg.file_name
msg.get_file(image_cache)
return
elif msg.type != SHARING :
print(u'收到一条群消息', msg)
return
print(u'收到一条群分享消息', msg)
data.append(msg.member.name)
data.append(msg.sender.name)
data.append(msg.receiver.name)
data.append(msg.create_time.strftime("%Y-%m-%d %H:%M:%S"))
data.append(msg.receive_time.strftime("%Y-%m-%d %H:%M:%S"))
data.append(msg.text)
data.append(msg.url)
datas.append(data)
openpyxl_excel.Save_Excel(datas)
destgroups = [u'小伙伴们', u'测试2', u'哇哈哈', u'相遇“市”你,青春“政”好^_^']
destusers = [u'证明给他看', u'Two words', u'李静']
def ReadMessage() :
print(u'读取未读历史消息列表:')
for msg in bot.messages :
if msg.member != None and msg.receiver.name == destgroups:
print(u'历史消息:', msg)
writeMsg(msg)
def Init() :
if not os.path.exists(image_cache_path) :
os.makedirs(image_cache_path)
def InitMonitorUsers() :
print(bot.groups(True))
fonds = bot.groups(True).search(destgroups[0])
litteBoy = ensure_one(fonds)
global destusers
for member in litteBoy.members :
destusers.append(member.name)
if __name__ == "__main__":
try:
Init()
InitMonitorUsers()
ReadMessage()#意外掉线的历史消息记录
embed()
except ResponseError as e:
print(e.err_code, e.err_msg) # 查看错误号和错误消息