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 '')    # 返回意圖命令,前兩個參數必填,分別表示置信度和調用命令名

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM