1use kovi::RuntimeBot;
2use kovi::bot::runtimebot::{
3 CanSendApi, send_api_await_response, send_api_request, send_api_request_with_forget,
4 send_api_request_with_response,
5};
6use kovi::bot::{ApiReturn, SendApi};
7#[cfg(not(feature = "cqstring"))]
8use kovi::message::Message as KoviMessage;
9use log::info;
10use serde::Serialize;
11use serde_json::{Value, json};
12
13#[cfg(feature = "cqstring")]
14use crate::onebot_message::CQMessage;
15#[cfg(not(feature = "cqstring"))]
16use crate::onebot_message::OneBotMessage;
17
18pub enum HonorType {
19 All,
20 Talkative,
21 Performer,
22 Legend,
23 StrongNewbie,
24 Emotion,
25}
26
27pub enum AddRequestType<'a> {
28 Type(&'a str),
29 SubType(&'a str),
30}
31
32pub trait OnebotTrait: CanSendApi {
34 #[cfg(not(feature = "cqstring"))]
36 fn send_group_msg_return<T>(
37 &self,
38 group_id: i64,
39 msg: T,
40 ) -> impl std::future::Future<Output = Result<i32, ApiReturn>>
41 where
42 KoviMessage: From<T>,
43 T: Serialize,
44 {
45 let msg = KoviMessage::from(msg);
46 let group_id = &group_id;
47 info!("[send] [to group {group_id}]: {}", msg.to_human_string());
48
49 let msg = OneBotMessage::from(msg);
50 let send_api = SendApi::new(
51 "send_msg",
52 json!({
53 "message_type":"group",
54 "group_id":group_id,
55 "message":msg,
56 "auto_escape":true,
57 }),
58 );
59
60 let api_rx = send_api_request(self.__get_api_tx(), send_api);
61
62 async move {
63 let r = send_api_await_response(api_rx).await;
64
65 match r {
66 Ok(v) => match v.data.get("message_id").and_then(|v| v.as_i64()) {
67 Some(b) => Ok(b as i32),
68 None => Err(v),
69 },
70
71 Err(v) => Err(v),
72 }
73 }
74 }
75
76 #[cfg(feature = "cqstring")]
78 fn send_group_msg_return<T>(
79 &self,
80 group_id: i64,
81 msg: T,
82 ) -> impl std::future::Future<Output = Result<i32, ApiReturn>>
83 where
84 CQMessage: From<T>,
85 T: Serialize,
86 {
87 let msg = CQMessage::from(msg);
88 let send_api = SendApi::new(
89 "send_msg",
90 json!({
91 "message_type":"group",
92 "group_id":group_id,
93 "message":msg,
94 "auto_escape":false,
95 }),
96 );
97
98 let group_id = &group_id;
99 info!(
100 "[send] [to group {group_id}]: {}",
101 Message::from(msg).to_human_string()
102 );
103
104 let api_rx = send_api_request(&self.__get_api_tx(), send_api);
105
106 async move {
107 let r = send_api_await_response(api_rx).await;
108 match r {
109 Ok(v) => match v.data.get("message_id").and_then(|v| v.as_i64()) {
110 Some(b) => Ok(b as i32),
111 None => Err(v),
112 },
113
114 Err(v) => Err(v),
115 }
116 }
117 }
118
119 #[cfg(not(feature = "cqstring"))]
120 fn send_private_msg_return<T>(
122 &self,
123 user_id: i64,
124 msg: T,
125 ) -> impl std::future::Future<Output = Result<i32, ApiReturn>>
126 where
127 KoviMessage: From<T>,
128 T: Serialize,
129 {
130 let send_api = SendApi::new(
131 "send_msg",
132 json!({"message_type":"private",
133 "user_id":user_id,
134 "message":msg,
135 "auto_escape":true,}),
136 );
137
138 let msg = KoviMessage::from(msg);
139 let user_id = &user_id;
140 info!("[send] [to private {user_id}]: {}", msg.to_human_string());
141
142 let api_rx = send_api_request(self.__get_api_tx(), send_api);
143
144 async move {
145 let r = send_api_await_response(api_rx).await;
146
147 match r {
148 Ok(v) => match v.data.get("message_id").and_then(|v| v.as_i64()) {
149 Some(b) => Ok(b as i32),
150 None => Err(v),
151 },
152
153 Err(v) => Err(v),
154 }
155 }
156 }
157
158 #[cfg(feature = "cqstring")]
159 fn send_private_msg_return<T>(
161 &self,
162 user_id: i64,
163 msg: T,
164 ) -> impl std::future::Future<Output = Result<i32, ApiReturn>>
165 where
166 CQMessage: From<T>,
167 T: Serialize,
168 {
169 let msg = CQMessage::from(msg);
170 let send_api = SendApi::new(
171 "send_msg",
172 json!({"message_type":"private",
173 "user_id":user_id,
174 "message":msg,
175 "auto_escape":false,}),
176 );
177
178 let user_id = &user_id;
179 info!(
180 "[send] [to private {user_id}]: {}",
181 Message::from(msg).to_human_string()
182 );
183
184 let api_rx = send_api_request(&self.__get_api_tx(), send_api);
185
186 async move {
187 let r = send_api_await_response(api_rx).await;
188 match r {
189 Ok(v) => match v.data.get("message_id").and_then(|v| v.as_i64()) {
190 Some(b) => Ok(b as i32),
191 None => Err(v),
192 },
193
194 Err(v) => Err(v),
195 }
196 }
197 }
198
199 fn can_send_image(&self) -> impl std::future::Future<Output = Result<bool, ApiReturn>> {
201 let send_api = SendApi::new("can_send_image", json!({}));
202
203 let api_rx = send_api_request(self.__get_api_tx(), send_api);
204
205 async move {
206 let r = send_api_await_response(api_rx).await;
207 match r {
208 Ok(v) => match v.data.get("yes").and_then(|v| v.as_bool()) {
209 Some(b) => Ok(b),
210 None => Err(v),
211 },
212
213 Err(v) => Err(v),
214 }
215 }
216 }
217
218 fn can_send_record(&self) -> impl std::future::Future<Output = Result<bool, ApiReturn>> {
220 let send_api = SendApi::new("can_send_record", json!({}));
221
222 let api_rx = send_api_request(self.__get_api_tx(), send_api);
223
224 async move {
225 let r = send_api_await_response(api_rx).await;
226 match r {
227 Ok(v) => match v.data.get("yes").and_then(|v| v.as_bool()) {
228 Some(b) => Ok(b),
229 None => Err(v),
230 },
231
232 Err(v) => Err(v),
233 }
234 }
235 }
236
237 #[cfg(not(feature = "cqstring"))]
238 fn send_group_msg<T>(&self, group_id: i64, msg: T)
240 where
241 KoviMessage: From<T>,
242 T: Serialize,
243 {
244 let msg = KoviMessage::from(msg);
245 let group_id = &group_id;
246 info!("[send] [to group {group_id}]: {}", msg.to_human_string());
247
248 let msg = OneBotMessage::from(msg);
249 let send_api = SendApi::new(
250 "send_msg",
251 json!({
252 "message_type":"group",
253 "group_id":group_id,
254 "message":msg,
255 "auto_escape":true,
256 }),
257 );
258
259 send_api_request_with_forget(self.__get_api_tx(), send_api);
260 }
261
262 #[cfg(feature = "cqstring")]
263 fn send_group_msg<T>(&self, group_id: i64, msg: T)
265 where
266 CQMessage: From<T>,
267 T: Serialize,
268 {
269 let msg = CQMessage::from(msg);
270 let send_api = SendApi::new(
271 "send_msg",
272 json!({
273 "message_type":"group",
274 "group_id":group_id,
275 "message":msg,
276 "auto_escape":false,
277 }),
278 );
279 let group_id = &group_id;
280 info!(
281 "[send] [to group {group_id}]: {}",
282 Message::from(msg).to_human_string()
283 );
284 send_api_request_with_forget(&self.__get_api_tx(), send_api);
285 }
286
287 #[cfg(not(feature = "cqstring"))]
288 fn send_private_msg<T>(&self, user_id: i64, msg: T)
290 where
291 KoviMessage: From<T>,
292 T: Serialize,
293 {
294 let msg = KoviMessage::from(msg);
295 let user_id = &user_id;
296 info!("[send] [to private {user_id}]: {}", msg.to_human_string());
297
298 let msg = OneBotMessage::from(msg);
299 let send_api = SendApi::new(
300 "send_msg",
301 json!({
302 "message_type":"private",
303 "user_id":user_id,
304 "message":msg,
305 "auto_escape":true,
306 }),
307 );
308
309 send_api_request_with_forget(self.__get_api_tx(), send_api);
310 }
311
312 #[cfg(feature = "cqstring")]
313 fn send_private_msg<T>(&self, user_id: i64, msg: T)
315 where
316 CQMessage: From<T>,
317 T: Serialize,
318 {
319 let msg = CQMessage::from(msg);
320 let send_api = SendApi::new(
321 "send_msg",
322 json!({
323 "message_type":"private",
324 "user_id":user_id,
325 "message":msg,
326 "auto_escape":false,
327 }),
328 );
329
330 let user_id = &user_id;
331 info!(
332 "[send] [to private {user_id}]: {}",
333 Message::from(msg).to_human_string()
334 );
335 send_api_request_with_forget(&self.__get_api_tx(), send_api);
336 }
337
338 fn delete_msg(&self, message_id: i32) {
344 let send_api = SendApi::new(
345 "delete_msg",
346 json!({
347 "message_id":message_id,
348 }),
349 );
350
351 send_api_request_with_forget(self.__get_api_tx(), send_api);
352 }
353
354 fn send_like(&self, user_id: i64, times: usize) {
361 let send_api = SendApi::new(
362 "send_like",
363 json!({
364 "user_id":user_id,
365 "times":times,
366 }),
367 );
368
369 send_api_request_with_forget(self.__get_api_tx(), send_api);
370 }
371
372 fn set_group_kick(&self, group_id: i64, user_id: i64, reject_add_request: bool) {
381 let send_api = SendApi::new(
382 "set_group_kick",
383 json!({
384 "group_id":group_id,
385 "user_id":user_id,
386 "reject_add_request":reject_add_request,
387 }),
388 );
389
390 send_api_request_with_forget(self.__get_api_tx(), send_api);
391 }
392
393 fn set_group_ban(&self, group_id: i64, user_id: i64, duration: usize) {
403 let send_api = SendApi::new(
404 "set_group_ban",
405 json!({
406 "group_id":group_id,
407 "user_id":user_id,
408 "duration":duration,
409 }),
410 );
411
412 send_api_request_with_forget(self.__get_api_tx(), send_api);
413 }
414 fn set_group_anonymous_ban_use_anonymous(
424 &self,
425 group_id: i64,
426 anonymous: Value,
427 duration: usize,
428 ) {
429 let send_api = SendApi::new(
430 "set_group_anonymous_ban",
431 json!({
432 "group_id":group_id,
433 "anonymous":anonymous,
434 "duration":duration,
435 }),
436 );
437
438 send_api_request_with_forget(self.__get_api_tx(), send_api);
439 }
440 fn set_group_anonymous_ban_use_flag(&self, group_id: i64, flag: &str, duration: usize) {
450 let send_api = SendApi::new(
451 "set_group_anonymous_ban",
452 json!({
453 "group_id":group_id,
454 "flag":flag,
455 "duration":duration,
456 }),
457 );
458
459 send_api_request_with_forget(self.__get_api_tx(), send_api);
460 }
461
462 fn set_group_whole_ban(&self, group_id: i64, enable: bool) {
470 let send_api = SendApi::new(
471 "set_group_whole_ban",
472 json!({
473 "group_id":group_id,
474 "enable":enable,
475 }),
476 );
477
478 send_api_request_with_forget(self.__get_api_tx(), send_api);
479 }
480
481 fn set_group_admin(&self, group_id: i64, user_id: i64, enable: bool) {
491 let send_api = SendApi::new(
492 "set_group_admin",
493 json!({
494 "group_id":group_id,
495 "user_id":user_id,
496 "enable":enable,
497 }),
498 );
499
500 send_api_request_with_forget(self.__get_api_tx(), send_api);
501 }
502 fn set_group_anonymous(&self, group_id: i64, enable: bool) {
510 let send_api = SendApi::new(
511 "set_group_anonymous",
512 json!({
513 "group_id":group_id,
514 "enable":enable,
515 }),
516 );
517
518 send_api_request_with_forget(self.__get_api_tx(), send_api);
519 }
520
521 fn set_group_card(&self, group_id: i64, user_id: i64, card: &str) {
531 let send_api = SendApi::new(
532 "set_group_card",
533 json!({
534 "group_id":group_id,
535 "user_id":user_id,
536 "card":card,
537 }),
538 );
539
540 send_api_request_with_forget(self.__get_api_tx(), send_api);
541 }
542
543 fn set_group_name(&self, group_id: i64, group_name: &str) {
551 let send_api = SendApi::new(
552 "set_group_name",
553 json!({
554 "group_id":group_id,
555 "group_name":group_name,
556 }),
557 );
558
559 send_api_request_with_forget(self.__get_api_tx(), send_api);
560 }
561
562 fn set_group_leave(&self, group_id: i64, is_dismiss: bool) {
570 let send_api = SendApi::new(
571 "set_group_leave",
572 json!({
573 "group_id":group_id,
574 "is_dismiss":is_dismiss,
575 }),
576 );
577
578 send_api_request_with_forget(self.__get_api_tx(), send_api);
579 }
580
581 fn set_group_special_title(&self, group_id: i64, user_id: i64, special_title: &str) {
591 let send_api = SendApi::new(
592 "set_group_special_title",
593 json!({
594 "group_id":group_id,
595 "user_id":user_id,
596 "special_title":special_title,
597 }),
598 );
599
600 send_api_request_with_forget(self.__get_api_tx(), send_api);
601 }
602 fn set_friend_add_request(&self, flag: &str, approve: bool, remark: &str) {
612 let send_api = SendApi::new(
613 "set_friend_add_request",
614 json!({
615 "flag":flag,
616 "approve":approve,
617 "remark":remark,
618 }),
619 );
620
621 send_api_request_with_forget(self.__get_api_tx(), send_api);
622 }
623 fn set_group_add_request(
635 &self,
636 flag: &str,
637 type_: AddRequestType,
638 approve: bool,
639 reason: &str,
640 ) {
641 let (type_, type_value) = match type_ {
642 AddRequestType::SubType(v) => ("sub_type", v),
643 AddRequestType::Type(v) => ("type", v),
644 };
645 let send_api = SendApi::new(
646 "set_group_add_request",
647 json!({
648 "flag":flag,
649 type_: type_value,
650 "approve":approve,
651 "reason":reason,
652 }),
653 );
654
655 send_api_request_with_forget(self.__get_api_tx(), send_api);
656 }
657
658 fn clean_cache(&self) {
662 let send_api = SendApi::new("clean_cache", json!({}));
663 send_api_request_with_forget(self.__get_api_tx(), send_api);
664 }
665 fn get_msg(
674 &self,
675 message_id: i32,
676 ) -> impl std::future::Future<Output = Result<ApiReturn, ApiReturn>> {
677 let send_api = SendApi::new(
678 "get_msg",
679 json!({
680 "message_id":message_id
681 }),
682 );
683
684 send_api_request_with_response(self.__get_api_tx(), send_api)
685 }
686 fn get_forward_msg(
691 &self,
692 id: &str,
693 ) -> impl std::future::Future<Output = Result<ApiReturn, ApiReturn>> {
694 let send_api = SendApi::new(
695 "get_forward_msg",
696 json!({
697 "id":id
698 }),
699 );
700
701 send_api_request_with_response(self.__get_api_tx(), send_api)
702 }
703 fn get_login_info(&self) -> impl std::future::Future<Output = Result<ApiReturn, ApiReturn>> {
705 let send_api = SendApi::new("get_login_info", json!({}));
706
707 send_api_request_with_response(self.__get_api_tx(), send_api)
708 }
709 fn get_stranger_info(
716 &self,
717 user_id: i64,
718 no_cache: bool,
719 ) -> impl std::future::Future<Output = Result<ApiReturn, ApiReturn>> {
720 let send_api = SendApi::new(
721 "get_stranger_info",
722 json!({
723 "user_id":user_id,
724 "no_cache":no_cache
725 }),
726 );
727
728 send_api_request_with_response(self.__get_api_tx(), send_api)
729 }
730 fn get_friend_list(&self) -> impl std::future::Future<Output = Result<ApiReturn, ApiReturn>> {
732 let send_api = SendApi::new("get_friend_list", json!({}));
733
734 send_api_request_with_response(self.__get_api_tx(), send_api)
735 }
736 fn get_group_info(
743 &self,
744 group_id: i64,
745 no_cache: bool,
746 ) -> impl std::future::Future<Output = Result<ApiReturn, ApiReturn>> {
747 let send_api = SendApi::new(
748 "get_group_info",
749 json!({
750 "group_id":group_id,
751 "no_cache":no_cache
752 }),
753 );
754
755 send_api_request_with_response(self.__get_api_tx(), send_api)
756 }
757 fn get_group_list(&self) -> impl std::future::Future<Output = Result<ApiReturn, ApiReturn>> {
759 let send_api = SendApi::new("get_group_list", json!({}));
760
761 send_api_request_with_response(self.__get_api_tx(), send_api)
762 }
763 fn get_group_member_info(
772 &self,
773 group_id: i64,
774 user_id: i64,
775 no_cache: bool,
776 ) -> impl std::future::Future<Output = Result<ApiReturn, ApiReturn>> {
777 let send_api = SendApi::new(
778 "get_group_member_info",
779 json!({
780 "group_id":group_id,
781 "user_id":user_id,
782 "no_cache":no_cache
783 }),
784 );
785
786 send_api_request_with_response(self.__get_api_tx(), send_api)
787 }
788 fn get_group_member_list(
794 &self,
795 group_id: i64,
796 ) -> impl std::future::Future<Output = Result<ApiReturn, ApiReturn>> {
797 let send_api = SendApi::new(
798 "get_group_member_list",
799 json!({
800 "group_id":group_id,
801 }),
802 );
803
804 send_api_request_with_response(self.__get_api_tx(), send_api)
805 }
806
807 fn get_group_honor_info(
814 &self,
815 group_id: i64,
816 honor_type: HonorType,
817 ) -> impl std::future::Future<Output = Result<ApiReturn, ApiReturn>> {
818 let honor_type = match honor_type {
819 HonorType::All => "all",
820 HonorType::Talkative => "talkative",
821 HonorType::Performer => "performer",
822 HonorType::Legend => "legend",
823 HonorType::StrongNewbie => "strong_newbie",
824 HonorType::Emotion => "emotion",
825 };
826
827 let send_api = SendApi::new(
828 "get_group_honor_info",
829 json!({
830 "group_id":group_id,
831 "type":honor_type
832 }),
833 );
834
835 send_api_request_with_response(self.__get_api_tx(), send_api)
836 }
837
838 fn get_credentials(
844 &self,
845 domain: &str,
846 ) -> impl std::future::Future<Output = Result<ApiReturn, ApiReturn>> {
847 let send_api = SendApi::new(
848 "get_credentials",
849 json!({
850 "domain":domain,
851 }),
852 );
853
854 send_api_request_with_response(self.__get_api_tx(), send_api)
855 }
856
857 fn get_status(&self) -> impl std::future::Future<Output = Result<ApiReturn, ApiReturn>> {
859 let send_api = SendApi::new("get_status", json!({}));
860
861 send_api_request_with_response(self.__get_api_tx(), send_api)
862 }
863 fn get_version_info(&self) -> impl std::future::Future<Output = Result<ApiReturn, ApiReturn>> {
865 let send_api = SendApi::new("get_version_info", json!({}));
866 send_api_request_with_response(self.__get_api_tx(), send_api)
867 }
868 fn get_cookies(
874 &self,
875 domain: &str,
876 ) -> impl std::future::Future<Output = Result<ApiReturn, ApiReturn>> {
877 let send_api = SendApi::new(
878 "get_cookies",
879 json!({
880 "domain":domain,
881 }),
882 );
883 send_api_request_with_response(self.__get_api_tx(), send_api)
884 }
885 fn get_csrf_token(&self) -> impl std::future::Future<Output = Result<ApiReturn, ApiReturn>> {
887 let send_api = SendApi::new("get_csrf_token", json!({}));
888
889 send_api_request_with_response(self.__get_api_tx(), send_api)
890 }
891 fn get_record(
899 &self,
900 file: &str,
901 out_format: &str,
902 ) -> impl std::future::Future<Output = Result<ApiReturn, ApiReturn>> {
903 let send_api = SendApi::new(
904 "get_record",
905 json!({
906 "file":file,
907 "out_format":out_format
908 }),
909 );
910 send_api_request_with_response(self.__get_api_tx(), send_api)
911 }
912 fn get_image(
918 &self,
919 file: &str,
920 ) -> impl std::future::Future<Output = Result<ApiReturn, ApiReturn>> {
921 let send_api = SendApi::new(
922 "get_image",
923 json!({
924 "file":file,
925 }),
926 );
927 send_api_request_with_response(self.__get_api_tx(), send_api)
928 }
929
930 fn send_like_return(
937 &self,
938 user_id: i64,
939 times: usize,
940 ) -> impl std::future::Future<Output = Result<ApiReturn, ApiReturn>> {
941 let send_api = SendApi::new(
942 "send_like",
943 json!({
944 "user_id":user_id,
945 "times":times,
946 }),
947 );
948
949 send_api_request_with_response(self.__get_api_tx(), send_api)
950 }
951}
952
953impl OnebotTrait for RuntimeBot {
954}