一個用於k12的bot包
Project description
12
NKUST UCL K12 ChatBot
簡介
NKUST UCL K12 ChatBot 是一個基於 K12 API 的 ChatBot,它能夠自動發送文字訊息、圖片、文件,接收到來自聊天室的訊息,支援消息歷史查詢。
安裝
使用 pip 安裝:
pip install nkust-ucl-k12-bot
用法
一個簡單的所有範例
from nkust_ucl.k12 import BOT as K12
custom_k12 = K12(config_file='config/k12.yaml')
custom_k12.set_chat_bot_info(
SendUserID="1234567890987654321",
SendUserName="test_bot",
SendUserImage="https://upload.wikimedia.org/wikipedia/commons/thumb/0/04/ChatGPT_logo.svg/512px-ChatGPT_logo.svg.png"
)
@K12.on_connect
def my_custom_on_connect(self, client, userdata, flags, rc):
# ... 自定義的 on_connect 行為 ...
client.subscribe(self.client.mqttSubscribe)
print("開始訂閱")
@K12.on_processed_message
def my_custom_on_processed_message(self, chat_msg):
# ... 自定義的 on_processed 行為 ...
# 這邊就是當收到訊息要做什麼事情
# 可以調用chat_msg的屬性
# chat_msg.send_user_name: 發送者的名字
# chat_msg.send_user_id: 發送者的id
# chat_msg.send_user_image: 發送者的頭像
# chat_msg.msg_body: 訊息內容
# chat_msg.room_id: 房間id
# chat_msg.msg_type: 訊息類型
# chat_msg.timestamp: 訊息時間戳
# chat_msg.mode: 訊息模式
# chat_msg.msg_id: 訊息id
if chat_msg.send_user_id == "1234567890987654321":
return
custom_k12.send_text(chat_msg.room_id, 'https://upload.wikimedia.org/wikipedia/commons/thumb/0/04/ChatGPT_logo.svg/512px-ChatGPT_logo.svg.png')
custom_k12.send_image(chat_msg.room_id, 'https://upload.wikimedia.org/wikipedia/commons/thumb/0/04/ChatGPT_logo.svg/512px-ChatGPT_logo.svg.png')
custom_k12.send_document(chat_msg.room_id, 'https://upload.wikimedia.org/wikipedia/commons/thumb/0/04/ChatGPT_logo.svg/512px-ChatGPT_logo.svg.png')
# run要在最後面
custom_k12.run()
初始化 K12 Bot
from nkust_ucl.k12 import BOT as K12
custom_k12 = K12(config_file='config/k12.yaml')
custom_k12.set_chat_bot_info(
SendUserID="my_id",
SendUserName="ChatGPT_ucl",
SendUserImage="https://upload.wikimedia.org/wikipedia/commons/thumb/0/04/ChatGPT_logo.svg/512px-ChatGPT_logo.svg.png"
)
發送文字訊息
custom_k12.send_text(roomid="12345", text="你好!")
發送圖片
# image_path可以是網址或是本地路徑
custom_k12.send_image(roomid="12345", image_path="/path/to/image.png")
發送文件
# doc_path可以是網址或是本地路徑
custom_k12.send_document(roomid="12345", doc_path="/path/to/document.pdf")
接收訊息
註冊 on_processed_message
處理方法以接收來自聊天室的訊息:
@K12.on_processed_message
def my_custom_on_processed_message(self, chat_msg):
# ... 自定義的 on_processed 行為 ...
# ChatMsg 為訊息物件
自定義 on_connect 行為
@K12.on_connect
def my_custom_on_connect(self, client, userdata, flags, rc):
# ... 自定義的 on_connect 行為 ...
# self.client.mqttSubscribe為訂閱的主題
client.subscribe(self.client.mqttSubscribe)
print("開始訂閱")
如何貢獻
- Fork 專案
- 創建新的分支 (
git checkout -b feature/fooBar
) - 提交你的修改 (
git commit -am 'Add some fooBar'
) - 推送到分支 (
git push origin feature/fooBar
) - 創建一個新的 Merge Request
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for nkust_ucl_k12_bot-2.7.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a7e5366a11a9767401847d79e180393df202d56a9670271d55a2101e03d3ccee |
|
MD5 | cf1144b15c3c6e7991488de290fbe5cf |
|
BLAKE2b-256 | d7ceb2f320fa9b2a94070c872c0a7f38e3ce04a860d7d346764af2c9aa987a7c |