微信聊天机器人-存储好友分享消息
2018-11-21 19:21
302 查看
一、背景
一般大公司都比较重视企业文化,和学习氛围,这不我们的团队也是如此,每个人每月微信分享必须超过5篇内容,一个是提高自己的阅读量,另外还可以把有用的资源分享给其他人。开始记录的一段时间还算比较顺利,可是当大家的分享内容一多的时候,总是发生记漏的情况,后来我就萌生了一个想法,可不可以让程序来记录分享数据,这样就不会发生错误了。正好这一段时间刚好在学习python,那就拿这个案例来练练手,网上搜索了一些资料,发现我的需求果然可以用程序来实现,那么还等什么直接开干吧
二、效果展示
1、自动回复
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')View Code
四、读写excel
1、使用pip命令安装openpyxl:pip install openpyxl2、使用openpyxl.load_workbook加载excel文件,如果文件不存在则使用openpyxl.Workbook()构造工作簿,操作完成后使用工作簿save接口保存文件
if os.path.exists(fileName) : wbk = openpyxl.load_workbook(fileName) names = wbk.sheetnames else : wbk = openpyxl.Workbook() sheet = wbk.active sheet.title = 'all' names.append('all') FixedSheetWidth(sheet)
3、修改列宽度和内容
headList = ['发送者', '群聊', '接受者', '发送时间', '接受时间','分享内容', '网址'] def GetCellKey(r, c) : cell = chr(ord('A') + c - 1) + str(r) return cell def FixedSheetWidth(sheet) : for i in range(0, len(cwidth)): sheet.column_dimensions[chr(ord('A') + i)].width = cwidth[i] def WriteSheetTitle(sheet) : i = 1 for svalue in headList: sheet[GetCellKey(1, i)] = svalue sheet[GetCellKey(1, i)].font = openpyxl.styles.Font(bold = True) i = i + 1
4、添加行数据
def WriteSheetRow(wbk, sheet, rowValueList, rowIndex, isBold): i = 1 for svalue in rowValueList : if isBold : sheet[GetCellKey(rowIndex, i)] = svalue sheet[GetCellKey(rowIndex, i)].font = openpyxl.styles.Font(bold = True) else: sheet[GetCellKey(rowIndex, i)] = svalue i = i + 1 #写入单独已用户名为标签的sheet name = rowValueList[0] subsheet = None if name not in names : subsheet = wbk.create_sheet(name) WriteSheetTitle(subsheet) FixedSheetWidth(subsheet) names.append(name) else : subsheet = wbk[name] j = 1 rowIndex = subsheet.max_row + 1 for svalue in rowValueList: if isBold : subsheet[GetCellKey(rowIndex, j)] = svalue else: subsheet[GetCellKey(rowIndex, j)] = svalue j = j + 1View Code
5、备份用于查看的文件
#备份文件 file2see = os.path.join(os.getcwd(), generateFileName('')) if not os.path.exists(file2see) : shutil.copyfile(fileName, file2see) else : if os.access(file2see, os.W_OK) : shutil.copyfile(fileName, file2see)
五、定时器
定时器主要用于后台定时发送消息给机器人自己,保持自己在线状态
定时器对象使用python的线程对象thread.Thread作为父类,并添加了pause、is_pause、resume和stop接口,使定时器控制起来更方便
# -*- coding: UTF-8 -*- import time import threading class Timer(threading.Thread): def __init__(self, fun, seconds): self.__runTime = seconds self.__runfun = fun self.__elapsed = 0.0 #流失的时间 self.__flag = threading.Event() # 用于暂停线程的标识 self.__flag.set() # 设置为True self.__running = threading.Event() # 用于停止线程的标识 self.__running.set() # 将running设置为True threading.Thread.__init__(self) print("initialize Timer completed!") def run(self): while self.__running.isSet(): self.__flag.wait() # 为True时立即返回, 为False时阻塞直到内部的标识位为True后返回 time.sleep(0.1) #100ms检测一次退出状态 self.__elapsed = self.__elapsed + 0.1 if self.__elapsed > self.__runTime : self.__elapsed = 0.0 self.__runfun() def pause(self): self.__flag.clear() # 设置为False, 让线程阻塞 def is_pause(self) : return self.__flag.isSet() == False def resume(self): self.__flag.set() # 设置为True, 让线程停止阻塞 def stop(self): self.__flag.set() # 将线程从暂停状态恢复, 如何已经暂停的话 self.__running.clear() # 设置为False self.__elapsed = 0.0View Code
六、demo下载
需要全部代码的到csdn直接下载:自动聊天机器人-存储好友分享消息
七、参考文章
转载声明:本站文章无特别说明,皆为原创,版权所有,转载请注明:朝十晚八
相关文章推荐
- Swift - 发送消息(文本,图片,文件等)给微信好友或分享到朋友圈
- 封装好的popupwiindow功能类分享到微信好友、朋友圈、qq好友、qq空间
- iOS QQ、QQ空间、微信好友、微信朋友圈分享成功如何显示图文
- Android 分享app到QQ好友、微信好友、朋友圈(无需提供SDK,只需调用相关的Activity)
- 请问android使用友盟分享,分享到微信好友和朋友圈详细步骤是什么,我现在分享之后没结果。
- Unity3d ios 微信链接分享好友,朋友圈的一些坑
- Android APP中跳转至微信,分享图文给好友或者朋友圈(加跳转QQ好友或QQ群)
- andriod 实现新浪、QQ空间、微信朋友圈、微信好友分享功能
- Android平台好友点击微信分享的内容后跳转来源App的实现方案研究
- 分享到微信的消息跳转到第三方APP
- 在微信浏览器中做分享到朋友圈和分享到好友
- React-Native之微信好友、朋友圈分享、支付
- MUI分享链接到微信好友、朋友圈、QQ好友(终极版)
- js 微信小游戏 开放数据域(子域)获取 用户信息 ,存储托管数据,获取好友数据 和微信托管云数据
- 微信分享到朋友圈,分享给好友设置标题图片描述参数
- 微信分享到好友或者朋友圈的截屏图片
- phone3.4 分享微信好友,微信朋友圈
- 将企业协作与微信结合,明道打通微信实现从微信到明道的消息分享,目前已支持文字、图片以及链接
- popuwindow下的微信好友和朋友圈分享
- Swift 分享多媒体消息到 微信/新浪微博/QQ