闲着无聊,在网上找了一些好玩的代码,比如用Python实现微信自动回消息
之前弄了一个定时自动给好友发天气或者发心灵鸡汤的脚本,下载链接:https://download.csdn.net/download/silent_f/11162951
今天这里就介绍几个微信接入图灵机器人,自动和好友聊天的脚本
1、自动和所有好友聊天,只要好友发消息来,就会给好友回复
这里笔者用的是3.6版本python ,安装itchat,
用下面命令
然后去图灵机器人官网注册账号,获取一个key
具体代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import requests import itchat import importlib,sys importlib.reload(sys) import sys def get_response(_info): print(_info) api_url = 'http://www.tuling123.com/openapi/api' data = { 'key': '466856743b3442bf95acb1e21a5218e2', 'info': _info, 'userid': 'wechat-robot', } r = requests.post(api_url, data=data).json() print(r.get('text')) return r @itchat.msg_register(itchat.content.TEXT) def text_reply(msg): return "" + get_response(msg["Text"])["text"] if __name__ == '__main__': itchat.auto_login() itchat.run()
|
2、和指定好友聊天
代码:
1 2 3 4 5 6 7 8
| from wxpy import Bot,Tuling,embed,ensure_one bot = Bot() my_friend = ensure_one(bot.search('张三')) tuling = Tuling(api_key='466856743b3442bf95acb1e21a5218e2') @bot.register(my_friend) def reply_my_friend(msg): tuling.do_reply(msg) embed()
|
3、在指定微信群里聊天
代码:
1 2 3 4 5 6 7 8
| from wxpy import Bot,Tuling,embed bot = Bot(cache_path=True) my_group = bot.groups().search('8个人儿? ? ? ? ?')[0] tuling = Tuling(api_key='466856743b3442bf95acb1e21a5218e2') @bot.register(my_group, except_self=False) def reply_my_friend(msg): print(tuling.do_reply(msg)) embed()
|