java sdk 聊天机器人_给女朋友做个聊天机器人,这样就能安心写代码了

这是第 404 次女友因为我没有及时回微信而和我生气了

惹祸的是我,受伤的确是钱包……

但是,我并不后悔

那一局王者荣耀,因为没有回微信,我完成了五杀 carry 全场

那一局刺激战场,因为没有回微信,我绝地反杀成功吃鸡

那一场球赛,因为没有回微信,我看到了极限投篮压哨三分

最重要的是,因为没有回微信,我终于可以安心写代码

两耳不闻窗外事,一心只想写代码,这才是一名程序员的内心独白

作为程序员中的佼佼者,深知有个女友不容易,于是,为了更及时的陪女友聊天回复微信,活学活用的程序员用 Python + itchat 写一个爬虫脚本每天定时给女友发给微信高阶土味情话。

![image.png](https://statics.sdk.cn/articles/img/202102/1f54c9adc7eaf3795004628394_1001101.png?x-oss-process=style/thumb)

再也不怕没能及时回复女友的微信了,女友甚至喜欢上了那个陪她聊天的机器人

![image.png](https://statics.sdk.cn/articles/img/202102/1f54c9adc7eaf3799981524680_1001101.png?x-oss-process=style/thumb)

核心代码

1. 定时任务

每天 9:30 给女朋友们开始给女朋友发送内容。

# 定时任务

scheduler = BlockingScheduler()

# 每天9:30给女朋友发送每日一句

# scheduler.add_job(start_today_info, 'cron', hour=9, minute=30)

scheduler.start()

start_today_info 是方法处理类。

2. 获取每日一句

数据来源 1: ONE●一个

def get_dictum_info(self):

'''

获取格言信息(从『一个。one』获取信息 http://wufazhuce.com/)

:return: str 一句格言或者短语

'''

print('获取格言信息..')

user_url = 'http://wufazhuce.com/'

resp = requests.get(user_url, headers=self.headers)

soup_texts = BeautifulSoup(resp.text, 'lxml')

# 『one -个』 中的每日一句

every_msg = soup_texts.find_all('div', class_='fp-one-cita')[0].find('a').text

return every_msg

数据来源 2: 金山词霸 ● 每日一句

有英文和中文翻译,例如:

When you finally get your own happiness, you will understand the previous sadness is a kind of treasure, which makes you better to hold and cherish the people you love.

等你获得真正属于你的幸福之后,你就会明白一起的伤痛其实是一种财富,它让你学会更好地去把握和珍惜你爱的人。

代码实现 :

def get_ciba_info(self):

'''

从词霸中获取每日一句,带英文。

:return:

'''

resp = requests.get('http://open.iciba.com/dsapi')

if resp.status_code == 200 and self.isJson(resp):

conentJson = resp.json()

content = conentJson.get('content')

note = conentJson.get('note')

# print(f"{content}

{note}")

return f"{content}

{note}

"

else:

print("没有获取到数据")

return None

数据来源 3: 土味情话(感谢 tomatoF、QSCTech-Sange)

def get_lovelive_info(self):

'''

从土味情话中获取每日一句。

'''

resp = requests.get("https://api.lovelive.tools/api/SweetNothings")

if resp.status_code == 200:

return resp.text + "

"

else:

print('每日一句获取失败')

return None

3. 获取今日天气

天气数据来源:SOJSON

def get_weather_info(self, city_code=''):

weather_url = f'http://t.weather.sojson.com/api/weather/city/{city_code}'

resp = requests.get(url=weather_url)

if resp.status_code == 200 and resp.json().get('status') == 200:

weatherJson = resp.json()

# 今日天气

today_weather = weatherJson.get('data').get('forecast')[1]

city_code 城市对应 id。 http://cdn.sojson.com/_city.json

4. 登录微信并发送内容

itchat.auto_login()

itchat.send(today_msg, toUserName=name_uuid)

就是这么简单,女友再也不会打扰我写代码、玩游戏了!

不会 Python 怎么办?

还有个 Node+ wechaty 的,小编能帮你的只能到这里了

Python + itchat GitHub 地址:https://github.com/sfyc23/EverydayWechat

Node+ wechaty GitHub 地址:https://github.com/gengchen528/wechatBot

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值