用微信机器人为TA定制机器人的话题已经冷的不能再冷了,可是这并不妨碍我再来炒一次。权当升级路上的一次试炼。html
你心爱的TA忽然心血来潮,想看天气可是又不想掏手机,想看新闻,想看nba。。等等,别作梦了,醒醒吧!!python
一、准备有安装 python3版本的window 或者 linux 环境。linux
二、安装 wxpygit
三、安装示例所须要的相关模块github
四、一个能登陆网页的微信web
一、所需模块json
#coding=utf8
import requests
from requests import exceptions
from urllib.request import urlopen
from bs4 import BeautifulSoup
from urllib.parse import urlencode
from threading import Timer
import re
from wxpy import *
import schedule
import time
import http
import json
import datetime
import random
复制代码
二、机器人初始化,图灵api初始化,指定好友生效api
bot = Bot(cache_path=True,console_qr = 1)
myself = bot.self
bot.enable_puid('wxpy_puid.pkl')
tuling = Tuling(api_key='换成本身的图片key')
Lie = bot.friends().search(u'Lie')
复制代码
三、定义菜单bash
msgText = '''Helo! 回复'功能'获取对应功能 🙋1.天气(例:苏州天气) 🌹2.今日nba(注:今日全部比赛结果) 💕3.今日黄历 💊4.每日一句 🕎5.开启机器人(关闭机器人) 🏳️🌈6.今日古诗词 🌹7.每日阅读 🎓8.历史上的今天 ☘ 9.nba排名(注:当日东西部排名) 🌎10.新闻 1.头条新闻 2.社会新闻 3.娱乐新闻 4.体育新闻 😡11.星座运势(例如:天秤座) 🐻12.nba新闻 💣13.多日天气 (只有苏州)'''
复制代码
四、实现相关功能微信
// 获取星座指数
def get_star(name):
header = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.235'
}
url = 'http://web.juhe.cn:8080/constellation/getAll?consName='+str(name)+'&type=today&key=换成本身的聚合数据key'
timeout = random.choice(range(80, 180))
rep = requests.get(url, headers=header, timeout=timeout)
data = json.loads(rep.text)
starhtml = '今日'+str(name)+'运势:'+ '\n'\
+ ' 综合指数:' + data['all'] + '\n'\
+ ' 幸运色:' + data['color'] + '\n'\
+ ' 健康指数:' + data['health'] + '\n'\
+ ' 爱情指数:' + data['love'] + '\n'\
+ ' 财运指数:' + data['money'] + '\n'\
+ ' 速配星座:' + data['QFriend'] + '\n'\
+ ' 工做指数:' + data['work'] + '\n'\
+ ' 今日概述:' + data['summary'] + '\n'\
return starhtml
复制代码
五、调用相关功能
@bot.register(Lie)
def auto_reply_all(msg):
if '座' in msg.text:
star = get_star(msg.text)
msg.sender.send(star)
复制代码
六、运行机器人
while True:
schedule.run_pending()
time.sleep(1)
复制代码
到这,就结束了,图上的功能更多在代码里面,这里面没有提现出来,若是有感兴趣的小伙伴,能够自行到github下载。