Skip to content

Commit 3773f24

Browse files
authored
Allow fetching first message of a bot conversation (#998)
* Allow fetching first message of a bot conversation * Export Swift list type * Fix spelling issue
1 parent 058d012 commit 3773f24

File tree

6 files changed

+67
-12
lines changed

6 files changed

+67
-12
lines changed

native/swift/Sources/wordpress-api/Exports.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ public typealias SubscriberImportJobsListParams = WordPressAPIInternal.Subscribe
271271
public typealias AddSubscribersParams = WordPressAPIInternal.AddSubscribersParams
272272

273273
// MARK: Support Bots
274+
public typealias ListBotConversationParams = WordPressAPIInternal.ListBotConversationsParams
274275
public typealias CreateBotConversationParams = WordPressAPIInternal.CreateBotConversationParams
275276
public typealias AddMessageToBotConversationParams = WordPressAPIInternal.AddMessageToBotConversationParams
276277
public typealias GetBotConversationParams = WordPressAPIInternal.GetBotConversationParams

wp_api/src/wp_com/endpoint/support_bots_endpoint.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::wp_com::support_bots::BotId;
22
use crate::wp_com::support_bots::ChatId;
3+
use crate::wp_com::support_bots::ListBotConversationsParams;
34
use crate::wp_com::support_bots::MessageId;
45
use crate::{
56
request::endpoint::{AsNamespace, DerivedRequest},
@@ -18,8 +19,8 @@ use wp_derive_request_builder::WpDerivedRequest;
1819
enum SupportBotsRequest {
1920
#[post(url = "/odie/chat/<bot_id>", params = &CreateBotConversationParams, output = BotConversation)]
2021
CreateBotConversation,
21-
#[get(url = "/odie/conversations/<bot_id>", output = Vec<BotConversationSummary>)]
22-
GetBotConverationList,
22+
#[get(url = "/odie/conversations/<bot_id>", params = &ListBotConversationsParams, output = Vec<BotConversationSummary>)]
23+
GetBotConversationList,
2324
#[get(url = "/odie/chat/<bot_id>/<chat_id>", params = &GetBotConversationParams, output = BotConversation)]
2425
GetBotConversation,
2526
#[post(url = "/odie/chat/<bot_id>/<chat_id>", params = &AddMessageToBotConversationParams, output = BotConversation)]

wp_api/src/wp_com/support_bots.rs

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,53 @@
1+
use crate::url_query::FromUrlQueryPairs;
2+
use crate::url_query::UrlQueryPairsMap;
13
use crate::{
24
date::WpGmtDateTime,
3-
impl_as_query_value_for_new_type,
5+
impl_as_query_value_for_new_type, impl_as_query_value_from_to_string,
46
url_query::{AppendUrlQueryPairs, QueryPairs, QueryPairsExtension},
57
users::UserId,
68
};
79
use serde::{Deserialize, Serialize};
810
use serde_repr::*;
911
use std::collections::HashMap;
12+
use wp_derive::WpDeriveParamsField;
1013
use wp_serde_helper::deserialize_empty_vec_or_none;
1114

1215
use super::WpComSiteId;
1316

17+
#[derive(
18+
Debug,
19+
Default,
20+
Clone,
21+
PartialEq,
22+
Eq,
23+
PartialOrd,
24+
Ord,
25+
Hash,
26+
Serialize,
27+
Deserialize,
28+
uniffi::Enum,
29+
strum_macros::EnumString,
30+
strum_macros::Display,
31+
)]
32+
#[serde(rename_all = "snake_case")]
33+
#[strum(serialize_all = "snake_case")]
34+
pub enum ListBotConversationsSummaryMethod {
35+
FirstMessage,
36+
#[default]
37+
LastMessage,
38+
}
39+
40+
impl_as_query_value_from_to_string!(ListBotConversationsSummaryMethod);
41+
42+
#[derive(
43+
Debug, Default, PartialEq, Eq, Serialize, Deserialize, uniffi::Record, WpDeriveParamsField,
44+
)]
45+
#[supports_pagination(false)]
46+
pub struct ListBotConversationsParams {
47+
#[uniffi(default = None)]
48+
pub summary_method: Option<ListBotConversationsSummaryMethod>,
49+
}
50+
1451
#[derive(Debug, PartialEq, Eq, Serialize, uniffi::Record)]
1552
pub struct CreateBotConversationParams {
1653
pub message: String,
@@ -107,7 +144,9 @@ pub struct UserPaidSupportPlan {
107144
pub struct BotConversationSummary {
108145
pub chat_id: u64,
109146
pub created_at: WpGmtDateTime,
110-
pub last_message: BotMessageSummary,
147+
#[serde(alias = "first_message")]
148+
#[serde(alias = "last_message")]
149+
pub summary_message: BotMessageSummary,
111150
}
112151

113152
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, uniffi::Record)]
@@ -231,17 +270,20 @@ mod tests {
231270
#[case] json_file_path: &str,
232271
#[case] expected_chat_id: u64,
233272
) {
234-
let json = test_json(json_file_path).expect("Failed to read JSON file");
273+
let json: Vec<u8> = test_json(json_file_path).expect("Failed to read JSON file");
235274
let conversation: BotConversation = serde_json::from_slice(json.as_slice())
236275
.expect("Failed to deserialize bot conversation");
237276
assert_eq!(conversation.chat_id, expected_chat_id);
238277
}
239278

240-
#[test]
241-
fn test_bot_conversation_summary_deserialization() {
242-
let json = include_str!("../../tests/wpcom/support_bots/converation-list.json");
243-
let conversations: Vec<BotConversationSummary> =
244-
serde_json::from_str(json).expect("Failed to deserialize bot conversation summary");
279+
#[rstest]
280+
#[case("conversation-list-01.json")]
281+
#[case("conversation-list-02.json")]
282+
fn test_bot_conversation_summary_deserialization(#[case] json_file_path: &str) {
283+
let json = test_json(json_file_path).expect("Failed to read JSON file");
284+
let conversations: Vec<BotConversationSummary> = serde_json::from_slice(json.as_slice())
285+
.expect("Failed to deserialize bot conversation summary");
286+
245287
assert_eq!(conversations.len(), 1);
246288
assert_eq!(conversations[0].chat_id, 1965758);
247289
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[
2+
{
3+
"chat_id": 1965758,
4+
"created_at": "2025-10-16 18:41:19",
5+
"first_message": {
6+
"content": "oh hi there I have a new android tablet here",
7+
"role": "user",
8+
"created_at": "2025-10-16 18:41:20"
9+
}
10+
}
11+
]

wp_com_e2e/src/support_bot_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use wp_api::wp_com::{
44
client::WpComApiClient,
55
support_bots::{
66
AddMessageToBotConversationParams, BotConversation, BotId, ChatId,
7-
CreateBotConversationParams, GetBotConversationParams,
7+
CreateBotConversationParams, GetBotConversationParams, ListBotConversationsParams,
88
},
99
};
1010

@@ -14,7 +14,7 @@ pub async fn support_bots_test(client: &WpComApiClient, allow_writes: bool) -> a
1414

1515
let conversations = client
1616
.support_bots()
17-
.get_bot_converation_list(&bot_id)
17+
.get_bot_conversation_list(&bot_id, &ListBotConversationsParams::default())
1818
.await?
1919
.data;
2020

0 commit comments

Comments
 (0)