本文已参与「新人创造礼」活动,一起开启创造之路。

1 背景

看到很多小伙伴用微信玩谈天机器人,能主动回复,看着还挺像模像样的,心里痒痒,也想着自己做个谈天机器人玩玩,开始主意是能每天守时给女朋友“早安”音讯,推送当天的气候,引进图灵机器人能主动回复女朋友的音讯,尽量答复的有水平点。

2 计划完成

2.1 工具

  • 微信号两个,用来做测验
  • 调用wxpy pthon库
  • python3.5 及以上版别
  • 注册图灵机器人

2.2 整个原理

原理是十分简单的,wxpy 是在itchat 基础上,对一些接口进行了优化,并提升了模块调用的易用性,一起对部分功用也进行了扩充。itchat 大家都知道是一个开源的微信个人号接口,这儿列下wxpy 能够做的一些事:

  • 控制路由器、智能家居等具有敞开接口的玩意儿
  • 运转脚本时主动把日志发送到你的微信
  • 加群主为老友,主动拉进群中
  • 跨号或跨群转发音讯
  • 主动陪人谈天
  • 逗人玩

  • 附上wxpy 的github: github.com/youfou/wxpy

原理也是十分简单,wxpy 是利用网页版微信API来完成微信里的联系人查找和音讯发送,一起wxpy 是支持图灵机器人接口,所以能够将机器人的主动回复发送。这儿要注意,假如你的微信不支持网页版登陆,整个也是跑不通的。

2.3 完成过程

装置wxpy

sudo pip3 install -U wxpy

图灵机器人注册

可参照这个链接,购买一个月体验版玩玩:www.turingapi.com/

代码

# -*- coding: UTF-8 -*-    # 避免搜的用户名是中文
from wxpy import *         # 调用wxpy 包
bot = Bot()                # 初始化机器人,网页扫码登陆
friend = bot.friends().search('we_test')[0]          #查找昵称为 test 的老友
friend1 = bot.friends().search('大聪明')[0]           #查找昵称为 大聪明 的老友
group = bot.groups().search('test')[0]               #查找为 test 的群组
tuling = Tuling(api_key='58e4c4c..........')         #调用图灵机器人API
#主动发送音讯
friend.send('hello world')         #向 test 老友主动发送 ‘hello world’
group.send('for test')             #向test群组发送音讯
#主动回复
@bot.register(friend)
def reply_my_friend(msg):
    tuling.do_reply(msg)      #图灵机器人主动回复
@bot.register(friend1)
def reply_my_friend1(msg):
    tuling.do_reply(msg)     #图灵机器人主动回复
@bot.register(group)
def reply_my_group(msg):
    if isinstance(msg.chat, Group) and not msg.is_at:  #msg.chat 和 msg.is_at 可参考文档类型说明。群音讯,
                                                       #假如@ 我的才回复
	return
    else:
        tuling.do_reply(msg)
bot.join()              #坚持登陆状态

里面的代码包含了各个场景下的,大家能够按照自己要求截取,直接ptyhon3 demo.py 运转,然后扫码就能够了。

2.4 改进版别

只是玩一些主动回复还是没啥意思,改进版别加上每天早上和晚上的定点发送“早安”、“晚安”,每隔一小时提示喝水、走动,一起推送气候信息。
思路就是加一个守时线程,一向跑,然后到固守时刻了就调wxpy 的接口去发送告诉音讯。推送气候信息,我用的是goole 的气候API 接口,也是拉取后解析出需求的信息发送音讯。我来贴下代码:

#守时推送早安和晚安
def run_thread():
    list = [11,12,13,14,15,16,17]  #这些时刻点发送喝水的告诉
    while True:
        now = datetime.now()
        if now.hour == 6 and now.minute == 00 and now.second == 00:
            friend.send('早上好呀')
            # 获取当天的气候
            temp,sky = get_weather()  #封装了一份函数,调取
            friend.send('今气候候:'+ temp + '   ' + sky)
        elif(now.hour in list) and now.minute == 00 and now.second == 00:
            friend.send('喝点水,走动下吧')
        elif now.hour == 18 and now.minute == 30 and now.second == 00:
            friend.send('工作了一天幸苦了'elif now.hour == 23 and now.minute == 00 and now.second == 00:
            friend.send('太晚了,早点歇息吧')
        time.sleep(1) 

获取气候的接口

#调google 气候API,获取气候信息
def get_weather():
    city = "hubei"    #可修改城市
    url = "https://www.google.com/search?q="+"weather"+city
    html = requests.get(url).content
    soup = BeautifulSoup(html, 'html.parser')
    # get the temperature
    temp = soup.find('div', attrs={'class': 'BNeawe iBp4i AP7Wnd'}).text
    # this contains time and sky description
    str = soup.find('div', attrs={'class': 'BNeawe tAd8D AP7Wnd'}).text
    # format the data
    data = str.split('\n')
    time = data[0]
    sky = data[1]
    # list having all div tags having particular clas sname
    listdiv = soup.findAll('div', attrs={'class': 'BNeawe s3v9rd AP7Wnd'})
    # particular list with required data
    strd = listdiv[5].text
    # formatting the string
    pos = strd.find('Wind')
    other_data = strd[pos:]
    # printing all the data
    print("Temperature is", temp)
    #print("Time: ", time)
    #print("Sky Description: ", sky)
    #print(other_data)
    return temp,sky

简简单单,整个功用就做完了。

2.5 项目源代码

运转过程中需求装置的一些库,能够直接pip3 直接装置。

# -*- coding: UTF-8 -*-
from wxpy import *
from datetime import datetime
import threading
import time
import requests
from bs4 import BeautifulSoup
def get_weather():
    city = "hubei"
    url = "https://www.google.com/search?q="+"weather"+city
    html = requests.get(url).content
    soup = BeautifulSoup(html, 'html.parser')
    # get the temperature
    temp = soup.find('div', attrs={'class': 'BNeawe iBp4i AP7Wnd'}).text
    # this contains time and sky description
    str = soup.find('div', attrs={'class': 'BNeawe tAd8D AP7Wnd'}).text
    # format the data
    data = str.split('\n')
    time = data[0]
    sky = data[1]
    # list having all div tags having particular clas sname
    listdiv = soup.findAll('div', attrs={'class': 'BNeawe s3v9rd AP7Wnd'})
    # particular list with required data
    strd = listdiv[5].text
    # formatting the string
    pos = strd.find('Wind')
    other_data = strd[pos:]
    # printing all the data
    print("Temperature is", temp)
    #print("Time: ", time)
    #print("Sky Description: ", sky)
    #print(other_data)
    return temp,sky
#守时推送早安和晚安
def run_thread():
    list = [11,12,13,14,15,16,17]  #这些时刻点发送喝水的告诉
    while True:
        now = datetime.now()
        if now.hour == 6 and now.minute == 00 and now.second == 00:
            friend.send('早上好呀')
            # 获取当天的气候
            temp,sky = get_weather()  #封装了一份函数,调取
            friend.send('今气候候:'+ temp + '   ' + sky)
        elif(now.hour in list) and now.minute == 00 and now.second == 00:
            friend.send('喝点水,走动下吧')
        elif now.hour == 18 and now.minute == 30 and now.second == 00:
            friend.send('工作了一天幸苦了'elif now.hour == 23 and now.minute == 00 and now.second == 00:
            friend.send('太晚了,早点歇息吧')
        time.sleep(1) 
if __name__=="__main__":
    #起一个线程去跑守时使命
    bot = Bot()
    friend = bot.friends().search('we_test')[0]
    friend1 = bot.friends().search('大聪明')[0]
    group = bot.groups().search('test')[0]
    tuling = Tuling(api_key='58e4c4c..................')
    @bot.register(friend)
    def reply_my_friend(msg):
        tuling.do_reply(msg)     #图灵机器人主动回复
    @bot.register(friend1)
    def reply_my_friend1(msg):
        tuling.do_reply(msg)     #图灵机器人主动回复
    @bot.register(group)
    def reply_my_group(msg):
        if isinstance(msg.chat, Group) and not msg.is_at:
            return
        else:
            tuling.do_reply(msg)
    t = threading.Thread(target=run_thread, name='push_msg')
    t.start()
    bot.join() #坚持登陆状态