Wxpy之图灵机器人的接入

Zss 发表于:

将之前写的天气和星座的查询给WXPY模块来调用,实现机器人来查询天气和星座运势,再将其接入到图灵机器人

不过一个现成的api和自己写的两个查询,总是只能执行一个的,譬如说查天气,如果自己写的查询回复了,那么图灵将不再回复

实则并没有执行图灵机器人,所以得用特定的关键字来唤醒,如果不唤醒的话会看见一句话就回复一句。。。在群里面那得吵死了

中间的天气查询做了一些的判断,判断嘿,天气,查询等几个关键字加上去除,空格等字符来防止干扰

图灵机器人使用@请叫我旺纸蝈蝈来唤醒,使用嘿 来唤醒自己写的查询

#coding:utf-8
import wxpy,time,sys
from get_info import Constellation
from datetime import datetime
from get_weather import Weather
reload(sys)
sys.setdefaultencoding('utf-8')

class Send_conste():
    def conste_format(slef,data):
        pingyin = {'双子':'shuangzi','白羊':'baiyang','金牛':'jinniu','巨蟹':'juxie',\
                   '狮子':'shizi','处女':'chunv','天枰':'tiancheng','天蝎':'tianxie',\
                   '射手':'sheshou','摩羯':'mojie','水瓶':'shuiping','双鱼':'shuangyu'}
        for i in pingyin:
            if data == i:
                return pingyin[i]

    def send_info(slef,conste,msg):
        nt = datetime.now()
        date = nt.strftime('%Y年%m月%d日')
        get_info = Constellation()
        info_dict = get_info.get_constellation_info(slef.conste_format(conste))
        msg.reply('%s    %s 的运势如下:'%(conste,date))
        for i in info_dict:
            msg.reply(i + ':' + info_dict[i])
            time.sleep(0.5)

class Send_weather():
    """
    发送近三天的天气
    """
    def send_info(slef,city,msg):
        weather = Weather()
        weather_info = weather.get_weather_info(city)
        print(weather_info)
        for i in weather_info:
            msg.reply(i)
            time.sleep(0.5)

def main(msg):
    """
      星运势查询  关键字为:嘿 狮子  双子
      天气查询   关键字为:嘿 城市 天气
    """
    send_conste = Send_conste()
    send_weather = Send_weather()
    if '双子' or '白羊'or '金牛' or '巨蟹' or '狮子' or '处女' or '天枰' or '天蝎' or '射手' or '摩羯' or '水瓶' or '双鱼' in msg.text:
        if  '嘿'in msg.text:
          for conste in ['双子','白羊','金牛','巨蟹','狮子','处女','天枰','天蝎','射手','摩羯','水瓶','双鱼']:
              if conste in msg.text:
                  send_conste.send_info(conste,msg)
    if '嘿'in msg.text:
          text = str(msg.text).split('嘿')
          for i in text:
              if '天气'in i.strip():
                  text1 = i.strip()
                  text1 = text1.strip(',')
                  text1 = text1.strip(',')
                  text1 = text1.strip().split('天气')
                  for i in text1:
                      if '的'in i:
                          text2 = i.split('的')
                          for i in text2:
                              if  '查询'in i:
                                  text3 = i.split('查询')
                                  city = text3[1]
                                  send_weather.send_info(city,msg)
                              else:
                                  city = text2[0]
                                  send_weather.send_info(city,msg)
                      else:
                          if  '查询'in i:
                              text3 = i.split('查询')
                              city = text3[1]
                              send_weather.send_info(city,msg)
                          else:
                              city = text1[0]
                              send_weather.send_info(city,msg)



if __name__ == '__main__':
    robot = wxpy.Bot(cache_path="logoo.pkl")
    #friend = robot.friends().search('湘潭刘德华'.decode('utf-8'))[0]
    #group = robot.groups().search('旺纸蝈蝈的弟妹们'.decode('utf-8'))[0]
    tuling = wxpy.Tuling(api_key='...........')
    @robot.register()
    def print_others(msg):
        if '@请叫我旺纸蝈蝈'in msg.text:
            tuling.do_reply(msg)
        else:
            main(msg)

    robot.join()