CQ酷Q机器人使用

之前使用卡卡网站提醒软件,发现qq提醒要收费,所以自己写一个论坛提醒工具

https://cqp.cc/forum.php

一款可以发送qq消息,管理群的程序

配合其他插件可以更方便编程

https://cqp.cc/t/15124

首先使用了

不会导入一些其他python的库,放弃

后面使用

HTTP API by richardchien  的NoneBot

感觉还不错,教程非常详细

https://none.rclab.tk/guide/nl-processor.html

学到了很多python的知识,包括自然语言的处理,结巴分词https://github.com/fxsjy/jieba

图灵机器人http://www.tuling123.com

下面举出一些主动发送消息和调用 API 的例子:

await bot.send_private_msg(user_id=12345678, message='你好~')
await bot.send_group_msg(group_id=123456, message='大家好~')

ctx = session.ctx.copy()
del ctx['message']
await bot.send_msg(**ctx, message='喵~')

await bot.delete_msg(**session.ctx)
await bot.set_group_card(**session.ctx, card='新人请改群名片')
self_info = await bot.get_login_info()
group_member_info = await bot.get_group_member_info(group_id=123456, user_id=12345678, no_cache=True)

配合网络爬虫可以抓取论坛的新帖子和其他数据发送到qq进行提醒

 1 from nonebot import on_command, CommandSession
 2 from nonebot import on_natural_language, NLPSession, IntentCommand
 3 from jieba import posseg
 4 
 5 from .data_source import get_weather_of_city
 6 
 7 
 8 @on_command('weather', aliases=('天气', '天气预报', '查天气'))   #命令名以及触发词语
 9 async def weather(session: CommandSession): #weather为函数名
10 
11     city = session.get('city', prompt='你想查询哪个城市的天气呢?') # 从会话状态(session.state)中获取城市名称(city),如果当前不存在,则询问用户
12     weather_report = await get_weather_of_city(city)# 获取城市的天气预报
13     await session.send(weather_report)# 向用户发送天气预报
14 
15 
16 
17 @weather.args_parser# 将函数声明为 weather 命令的参数解析器,用于将用户输入的参数解析成命令真正需要的数据
18 async def _(session: CommandSession):
19     stripped_arg = session.current_arg_text.strip()# 去掉消息首尾的空白符
20 
21     if session.is_first_run: # 该命令第一次运行(第一次进入命令会话)
22         if stripped_arg:# 第一次运行参数不为空,意味着用户直接将城市名跟在命令名后面,作为参数传入
23             session.state['city'] = stripped_arg# 例如:天气 南京
24         return
25 
26     if not stripped_arg: # 用户没有发送有效的城市名称(而是发送了空白字符),则提示重新输入
27         session.pause('要查询的城市名称不能为空呢,请重新输入')   # 这里 session.pause() 将会发送消息并暂停当前会话(该行后面的代码不会被运行)
28     # 如果当前正在向用户询问更多信息(例如本例中的要查询的城市),且用户输入有效,则放入会话状态
29     session.state[session.current_key] = stripped_arg
30 
31 
32 
33 @on_natural_language(keywords={'天气'})   # 将函数声明为一个自然语言处理器 # keywords 表示需要响应的关键词,类型为任意可迭代对象,元素类型为 str,如果不传入 keywords,则响应所有没有被当作命令处理的消息
34 async def _(session: NLPSession):
35     stripped_msg = session.msg_text.strip()    # 去掉消息首尾的空白符
36     words = posseg.lcut(stripped_msg)    # 对消息进行分词和词性标注
37     city = None
38     # 遍历 posseg.lcut 返回的列表
39     for word in words:
40         if word.flag == 'ns':        # 每个元素是一个 pair 对象,包含 word 和 flag 两个属性,分别表示词和词性
41             city = word.word            # ns 词性表示地名
42     return IntentCommand(90.0, 'weather', current_arg=city or '')    # 返回意图命令,前两个参数必填,分别表示置信度和调用命令名
原文地址:https://www.cnblogs.com/jdzhang1995/p/10576029.html