puniyu_adapter_api 0.8.9

puniyu 统一适配器 API trait 库,定义 AdapterApi 基础接口
Documentation
use async_trait::async_trait;
use puniyu_account::AccountInfo;
use puniyu_adapter_types::{AdapterInfo, SendMsgType};
use puniyu_contact::{Contact, ContactType};
use puniyu_error::Result;
use puniyu_message::Message;

use crate::AdapterApi;
use anyhow::anyhow;

#[async_trait]
pub trait OneBotAdapterApi: Send + Sync {
    /// 发送私聊消息
	async fn send_private_msg(&self, user_id: u64, message: &Message) -> Result<SendMsgType>;

    /// 发送群消息
	async fn send_group_msg(&self, group_id: u64, message: &Message) -> Result<SendMsgType>;

    // 适配器信息
	fn adapter_info(&self) -> AdapterInfo;

    // 账号信息
	fn account_info(&self) -> AccountInfo;
}

#[async_trait]
impl<T: OneBotAdapterApi> AdapterApi for T {
	async fn send_message(
		&self,
		contact: &ContactType<'_>,
		message: &Message,
	) -> Result<SendMsgType> {
		match contact {
			ContactType::Friend(contact) => {
				self.send_private_msg(contact.peer().parse::<u64>()?, message).await
			}
			ContactType::Group(contact) => {
				self.send_group_msg(contact.peer().parse::<u64>()?, message).await
			}
			ContactType::GroupTemp(concat) => {
				self.send_private_msg(concat.peer().parse::<u64>()?, message).await
			}
			_ => Err(anyhow!("unsupported contact type: {:?}", contact).into()),
		}
	}

	fn adapter_info(&self) -> AdapterInfo {
        self.adapter_info()
	}

	fn account_info(&self) -> AccountInfo {
		self.account_info()
	}

	fn as_onebot(&self) -> Option<&dyn OneBotAdapterApi> {
		Some(self)
	}
}