当前位置:   article > 正文

Python实现微信自动回复_python微信自动回复

python微信自动回复

 先安装 itchat 、requests、itchat-uos

itchat-uos主要解决微信提示禁止网页登录导致登录失败的问题

以下有三种可玩方式:

1.回复好友

  1. # 源代码如下:
  2. # wechat autoreply
  3. import itchat
  4. import requests
  5. import re
  6. # 抓取网页
  7. def getHtmlText(url):
  8. try:
  9. r = requests.get(url,timeout=30)
  10. r.raise_for_status()
  11. r.encoding = r.apparent_encoding
  12. return r.text
  13. except:
  14. return ""
  15. # 自动回复
  16. # 封装好的装饰器,当接收到的消息是Text,即文字消息
  17. @itchat.msg_register(['Text','Map', 'Card', 'Note', 'Sharing', 'Picture'])
  18. def text_reply(msg):
  19. # 回复给好友
  20. url = "http://www.tuling123.com/openapi/api?key=图灵apikey&info="
  21. url = url + msg['Text']
  22. html = getHtmlText(url)
  23. message = re.findall(r'\"text\"\:\".*?\"',html)
  24. reply = eval(message[0].split(':')[1]) + "--from zorro robot auto reply"
  25. return reply
  26. if __name__ == '__main__':
  27. itchat.auto_login()
  28. # 获取自己的UserName
  29. friends = itchat.get_friends(update=True)[0:]
  30. Name = {}
  31. Nic = []
  32. User = []
  33. for i in range(len(friends)):
  34. Nic.append(friends[i]["NickName"])
  35. User.append(friends[i]["UserName"])
  36. for i in range(len(friends)):
  37. Name[Nic[i]] = User[i]
  38. itchat.run()

2.群回复

  1. import itchat
  2. import requests
  3. import re
  4. # 抓取网页
  5. def getHtmlText(url):
  6. try:
  7. r = requests.get(url,timeout=30)
  8. r.raise_for_status()
  9. r.encoding = r.apparent_encoding
  10. return r.text
  11. except:
  12. return ""
  13. # 自动回复
  14. # 封装好的装饰器,当接收到的消息是Text,即文字消息
  15. # @itchat.msg_register(['Text','Map', 'Card', 'Note', 'Sharing', 'Picture'])
  16. def text_reply(msg):
  17. # 回复给好友
  18. url = "http://www.tuling123.com/openapi/api?key=7a924ffc67374b939670e5c1c9247cd1&info="
  19. url = url + msg['Text']
  20. html = getHtmlText(url)
  21. message = re.findall(r'\"text\"\:\".*?\"',html)
  22. reply = eval(message[0].split(':')[1]) + "--from zorro robot auto reply"
  23. return reply
  24. @itchat.msg_register([itchat.content.TEXT], isGroupChat=True)
  25. def print_group_msg(msg):
  26. gname='内部舆情风向群'
  27. context=text_reply(msg)
  28. print(context)
  29. myroom = itchat.get_chatrooms(update=True) # 获取所有群的相关信息,update=True表示信息更新
  30. myroom = itchat.search_chatrooms(name=gname) # 传入指定群名进行搜索,之所以搜索,是因为群员的名称信息也在里面
  31. for room in myroom:
  32. print(room)
  33. if room['NickName'] == gname:
  34. username = room['UserName']
  35. itchat.send_msg(context, username)
  36. # print(room['NickName'])
  37. # if msg['IsAt']: #['IsAt']这个标签是TRUE说明有人@我
  38. # username = room['UserName'] #获取该群聊的UserName
  39. # print(username)
  40. # itchat.send_msg(context, username) #发送信息
  41. # elif '所有人' in msg['Text']: #判断是否发送的是群公告
  42. # username = room['UserName']
  43. # itchat.send_msg(context, username)
  44. else:
  45. print('No groups found')
  46. itchat.auto_login() # 登录微信
  47. itchat.run()

3.使用微软小冰自动回复指定群消息

  1. import itchat
  2. import requests
  3. import re
  4. # //监控公众号消息
  5. @itchat.msg_register(itchat.content.TEXT, isMpChat=True)#isMpChat=True表明只监听公众号信息,注意小冰是个公众号
  6. def reply_msg(msg):
  7. print("小冰回复:"+str(msg['Content']))#将小冰的消息显示出来
  8. # print(msg)
  9. name=msg['User']['NickName']
  10. if(name=="AI小冰"):
  11. reply_to_group(msg['Content'])
  12. # itchat.send_msg(msg['Content'], gname)#将这个消息发送给原来的那个人
  13. # //回复群消息
  14. def reply_to_group(context):
  15. myroom = itchat.search_chatrooms(name=gname) # 传入指定群名进行搜索,之所以搜索,是因为群员的名称信息也在里面
  16. # print(room)
  17. for room in myroom:
  18. if room['NickName'] == gname:
  19. username = room['UserName']
  20. itchat.send_msg(context+"-from bot", username)
  21. print("已转发到群"+str(gname))
  22. # //监控群消息
  23. @itchat.msg_register([itchat.content.TEXT], isGroupChat=True)
  24. def print_group_msg(msg):
  25. global gname
  26. gname='指定的群名称'
  27. print("收到:"+str(msg['Text']))
  28. mps = itchat.search_mps(name='小冰')#搜索小冰
  29. if len(mps) > 0:
  30. # print(mps[0]['NickName'])
  31. itchat.send_msg(msg['Content'], toUserName=mps[0]['UserName'])#给小冰发消息
  32. # itchat.auto_login(hotReload=True) # 登录微信
  33. itchat.auto_login() # 登录微信
  34. itchat.run()

更多信息请查看 博客 或者关注公众号:Z技术

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号