python QQ机器人

通过第三方库qqbot来实现,pip install qqbot 直接安装就行

  1. 脚本直接运行会弹出二维码,手机qq扫码登录即可

  2. 通过#weather# city 来查询天气预报

  3. 通过#study# key value 来进行学习,以json格式存储在指定文件

  4. 第一次运行没有这个json文件,可以手动创建一个,内容为一对英文的双引号即可

  5. 通过#get# key 来进行获取之前存储的内容

  6. 只能在qq群进行自动操作

  7. qqbot其他相关功能能查看 https://github.com/pandolia/qqbot/

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import requests
import qqbot
import json

mess = json.load(open('qq.txt', 'r'))


def weather(city):
    r = city
    url = 'http://www.sojson.com/open/api/weather/json.shtml?city=' + city
    html = requests.get(url)
    result = json.loads(html.text)
    if result.get('status') != 200:
        return "暂无【%s】天气预报!" % city
    res = result.get('data').get('forecast')
    for i in res:
        r += '\r\n' + i.get('date') + ' ' + i.get('type') + ' ' + i.get('low') + ' ' + i.get('high') + ' ' + i.get(
            'fengli') + ' ' + i.get('fengxiang')
    return r


@qqbot.QQBotSlot
def onQQMessage(bot, contact, member, content):
    if "@ME" in content:
        con = '圈我干啥??'
        bot.SendTo(contact, con)
    elif content.startswith("#study#") and '#get#' not in content and len(content.split()) >= 3:
        key, *value = content.split()[1:]
        mess[key] = value
        con = "录入成功!"
        json.dump(mess, open('qq.txt', 'w'))
        bot.SendTo(contact, con)
    elif content.startswith('#get#') and len(content.split()) >= 2:
        key = content.split()[1]
        con = mess.get(key) if key in mess else '不存在'
        bot.SendTo(contact, ' '.join(con))
    elif content.startswith("#weather#") and len(content.split()) >= 2:
        con = weather(content.split()[1])
        bot.SendTo(contact, con)


if __name__ == '__main__':
    qqbot.RunBot()

github地址:https://github.com/babyshen/Python/blob/master/qq%E6%9C%BA%E5%99%A8%E4%BA%BA.py

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值