玩玩微信机器人

安装wxpy库

pip install wxpy

wxpy 库支持Python 3.4-3.6和2.7的版本

导入模块以及登录微信

from wxpy import *
import requests
import json

bot = Bot(cache_path=True)

cache_path缓存登录信息,然而我还是每次都要登一次....

帐号一定要保证能够登录微信网页版,不然会报错:  KeyError:'pass_ticket'

创建图灵机器人

  • 首先要在图灵官网注册帐号:http://www.tuling123.com/,创建成功后得到apikey
  • 之后,请求API,@机器人便会回复。虽然有点智障。
    def talk_robot(info='你好啊'):
    	api_url = 'http://www.tuling123.com/openapi/api'
    	apikey = '*************'
    	data = {'key': apikey,'info':info}
    	r = requests.post(api_url,data=data).text
    
    	response = json.loads(r)['text']
    	return response
    
    @bot.register()
    def reply_my_friends(msg):
    	if msg.is_at :
    		message = '{}'.format(msg.text)
    		response = talk_robot(info=message)
    	return response
    embed()

猜你喜欢

转载自blog.csdn.net/weixin_38854519/article/details/81512329