Skip to content

Commit

Permalink
Merge pull request #19 from praekeltfoundation/feature/DELTA-941/dyna…
Browse files Browse the repository at this point in the history
…mic-CMS-buttons

Change from next_prompt to CMS buttons
  • Loading branch information
rudigiesler authored Sep 25, 2024
2 parents 88db7a8 + fad6243 commit 07eb825
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
uses: actions/checkout@v4
with:
repository: praekeltfoundation/flow_tester
ref: v0.2.1
ref: v0.3.0
path: flow_tester
ssh-key: ${{ secrets.SSH_PRIVATE_KEY }}
- name: Check formatting
Expand Down
74 changes: 61 additions & 13 deletions Browsable FAQs/QA/flows/Browsable FAQs.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ card FetchContent, then: DisplayContent do
)
end
card DisplayContent when count(content_data.body.body.text.value.buttons) > 0 do
content_buttons = content_data.body.body.text.value.buttons
selected_button =
buttons(ProcessButton, map(content_buttons, &[&1.value.title, &1.value.title])) do
text("""
@content_data.body.title
@content_data.body.body.text.value.message
""")
end
end
card DisplayContent when content_data.body.has_children do
# If there are children, then give the user a list of options to choose from
parent_body =
Expand Down Expand Up @@ -144,19 +156,6 @@ card DisplayContent when content_data.body.has_children do
end
end
card DisplayContent when isnumber(content_data.body.body.next_message) do
next_prompt = "@content_data.body.body.text.value.next_prompt"
next_prompt = if(len(next_prompt) > 0, next_prompt, "Tell me more")
message = content_data.body.body.next_message
buttons(FetchContent: "@next_prompt") do
text("""
@content_data.body.title
@content_data.body.body.text.value.message
""")
end
end
card DisplayContent when count(content_data.body.related_pages) > 0 do
related_data =
get(
Expand Down Expand Up @@ -326,4 +325,53 @@ card DisplayMedia do
end
end
```

# ProcessButton & ActionButton

These cards handle processing a button action that is defined in the CMS, and performing that action

```stack
card ProcessButton do
selected_button = find(content_buttons, &(&1.value.title == selected_button))
then(ActionButton)
end
card ActionButton when selected_button.type == "next_message" do
message = content_data.body.body.next_message
then(FetchContent)
end
card ActionButton when selected_button.type == "go_to_page" do
selected_content_id = selected_button.value.page
message = 1
content_data =
get(
"https://content-repo-api-qa.prk-k8s.prd-p6t.org/api/v2/pages/@selected_content_id/",
headers: [
["Authorization", "Token @global.config.contentrepo_token"]
],
query: [["whatsapp", "true"], ["message", "@message"]]
)
page_list_data =
get(
"https://content-repo-api-qa.prk-k8s.prd-p6t.org/api/v2/pages/",
headers: [
["Authorization", "Token @global.config.contentrepo_token"]
],
query: [["child_of", "@content_data.body.meta.parent.id"]]
)
then(DisplayContent)
end
card ActionButton do
log("ERROR: Unknown button type @selected_button.type")
# Cause an error
base64_decode("ERROR: Unknown button type @selected_button.type")
end
```
2 changes: 1 addition & 1 deletion Browsable FAQs/QA/flows_json/Browsable FAQs.json

Large diffs are not rendered by default.

65 changes: 49 additions & 16 deletions Browsable FAQs/QA/tests/browsable_faqs_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule BrowsableFAQsTest do
use FlowTester.Case
use FakeCMS

alias FlowTester.WebhookHandler, as: WH
alias FlowTester.WebhookHandler.FakeCMS.Content.{Image}

defp flow_path(flow_name), do: Path.join([__DIR__, "..", "flows_json", flow_name <> ".json"])

Expand Down Expand Up @@ -45,8 +45,11 @@ defmodule BrowsableFAQsTest do
slug: "multiple-messages-leaf",
title: "Multiple Messages Leaf",
wa_messages: [
%WAMsg{message: "First message"},
%WAMsg{message: "Second message"}
%WAMsg{message: "First message", buttons: [%Btn.Next{title: "Next"}]},
%WAMsg{
message: "Second message",
buttons: [%Btn.GoToPage{title: "Another page", page: "leaf-page-1"}]
}
]
}

Expand Down Expand Up @@ -178,19 +181,49 @@ defmodule BrowsableFAQsTest do
# })
# end

# TODO: next_message support for fake CMS
# test "allow to page through all messages of leaf content" do
# setup_flow()
# |> FlowTester.start()
# |> receive_message(%{})
# |> FlowTester.send("Topic 1")
# |> receive_message(%{})
# |> FlowTester.send("Multiple Messages Leaf")
# |> receive_message(%{
# text: "Multiple Messages Leaf\nFirst message\n",
# buttons: [{"Main Menu", "Main Menu"}]
# })
# end
test "show list of buttons defined in CMS" do
setup_flow()
|> FlowTester.start()
|> receive_message(%{})
|> FlowTester.send("Topic 1")
|> receive_message(%{})
|> FlowTester.send("Multiple Messages Leaf")
|> receive_message(%{
text: "Multiple Messages Leaf\nFirst message\n",
buttons: [{"Next", "Next"}]
})
end

test "next message buttons should show the next message" do
setup_flow()
|> FlowTester.start()
|> receive_message(%{})
|> FlowTester.send("Topic 1")
|> receive_message(%{})
|> FlowTester.send("Multiple Messages Leaf")
|> receive_message(%{})
|> FlowTester.send("Next")
|> receive_message(%{
text: "Multiple Messages Leaf\nSecond message\n"
})
end

test "go_to_page buttons should go to the specified page" do
setup_flow()
|> FlowTester.start()
|> receive_message(%{})
|> FlowTester.send("Topic 1")
|> receive_message(%{})
|> FlowTester.send("Multiple Messages Leaf")
|> receive_message(%{})
|> FlowTester.send("Next")
|> receive_message(%{buttons: [{"Another page", "Another page"}]})
|> FlowTester.send("Another page")
|> receive_message(%{
text: "Leaf Page 1\nTest leaf content page\n",
buttons: [{"Main Menu", "Main Menu"}]
})
end

# TODO: Add media support to FakeCMS
# test "give the user a choice when both image and document is present" do
Expand Down

0 comments on commit 07eb825

Please sign in to comment.