Skip to content

Commit 41a0b35

Browse files
authored
Merge pull request #188 from hchen2020/master
Add SelfId to IAgentHook.
2 parents ea3bf3e + 11176f3 commit 41a0b35

File tree

12 files changed

+191
-6
lines changed

12 files changed

+191
-6
lines changed

docs/channels/components.md

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# Messaging Components
2+
3+
Conversations are a lot more than simple text messages when you are building a AI chatbot. In addition to text, the `BotSharp`` allows you to send rich-media, like audio, video, and images, and provides a set of structured messaging options in the form of message templates, quick replies, buttons and more. The UI rendering program can render components according to the returned data format.
4+
5+
6+
## Text Messages
7+
```json
8+
{
9+
"recipient":{
10+
"id":"{{conversation_id}}"
11+
},
12+
"messaging_type": "RESPONSE",
13+
"message":{
14+
"text":"Hello, world!"
15+
}
16+
}
17+
```
18+
## Quick Replies
19+
20+
`content_type`: Text, Phone Number and Email
21+
```json
22+
{
23+
"recipient":{
24+
"id":"{{conversation_id}}"
25+
},
26+
"messaging_type": "RESPONSE",
27+
"message":{
28+
"text": "Pick a color:",
29+
"quick_replies":[
30+
{
31+
"content_type":"text",
32+
"title":"Red",
33+
"payload":"<POSTBACK_PAYLOAD>",
34+
"image_url":"http://example.com/img/red.png"
35+
},{
36+
"content_type":"text",
37+
"title":"Green",
38+
"payload":"<POSTBACK_PAYLOAD>",
39+
"image_url":"http://example.com/img/green.png"
40+
}
41+
]
42+
}
43+
}
44+
```
45+
## Sender Actions
46+
47+
Setting expectations is crucial when creating a chatbot. Sender actions, a key tool, allow you to control typing and read receipt indicators. For instance, you can use them to show when a message has been seen or when a response is being typed, keeping users informed during interactions.
48+
49+
`sender_action`: mark_seen, typing_on and typing_off
50+
```json
51+
{
52+
"recipient":{
53+
"id":"{{conversation_id}}"
54+
},
55+
"sender_action":"typing_on"
56+
}
57+
```
58+
## Message Templates
59+
60+
Message templates are structured message formats used for various purposes to present complex information in a tidy manner during conversations, preventing messy text. These templates also include buttons to enhance interactivity. It gives a way for you to offer a richer in-conversation experience than standard text messages by integrating buttons, images, lists, and more alongside text a single message. Templates can be use for many purposes, such as displaying product information, asking the message recipient to choose from a pre-determined set of options, and showing search results.
61+
62+
```json
63+
{
64+
"recipient":{
65+
"id":"{{conversation_id}}"
66+
},
67+
"message":{
68+
"attachment":{
69+
"type":"template",
70+
"payload":{
71+
"template_type":"TEMPLATE-TYPE",
72+
"elements":[
73+
{
74+
"title":"TEMPLATE-TITLE",
75+
...
76+
}
77+
]
78+
}
79+
}
80+
}
81+
}
82+
```
83+
### Button template
84+
85+
The button template sends a text message with up to three attached buttons. This template is useful for offering the message recipient options to choose from, such as pre-determined responses to a question, or actions to take.
86+
87+
`type`: web_url, postback, phone_number, account_link (log in), account_unlink (log out)
88+
89+
```json
90+
{
91+
"template_type":"button",
92+
"text":"What do you want to do next?",
93+
"buttons":[
94+
{
95+
"type":"web_url",
96+
"url":"https://www.github.com",
97+
"title":"Visit Github"
98+
},
99+
{
100+
"type":"postback",
101+
"title":"Visit Github",
102+
"payload": "<STRING_SENT_TO_WEBHOOK>"
103+
},
104+
{
105+
"type":"phone_number",
106+
"title":"<BUTTON_TEXT>",
107+
"payload":"<PHONE_NUMBER>"
108+
},
109+
{
110+
"type": "account_link",
111+
"url": "<YOUR_LOGIN_URL>"
112+
},
113+
{
114+
"type": "account_unlink"
115+
}
116+
]
117+
}
118+
```
119+
120+
### Generic template
121+
122+
The generic template is a simple structured message that includes a title, subtitle, image, and up to three buttons. You may also specify a `default_action` object that sets a URL that will be opened in the chat webview when the template is tapped.
123+
124+
```json
125+
{
126+
"template_type":"generic",
127+
"elements":[
128+
{
129+
"title":"Welcome!",
130+
"image_url":"https://raw.githubusercontent.com/fbsamples/original-coast-clothing/main/public/styles/male-work.jpg",
131+
"subtitle":"We have the right hat for everyone.",
132+
"default_action": {
133+
"type": "web_url",
134+
"url": "https://www.originalcoastclothing.com/",
135+
"webview_height_ratio": "tall"
136+
},
137+
"buttons":[
138+
{
139+
"type":"web_url",
140+
"url":"https://www.originalcoastclothing.com/",
141+
"title":"View Website"
142+
}
143+
]
144+
}
145+
]
146+
}
147+
```
148+
149+
### Form template
150+
151+
Form template is used to collect information from the user side, and the UI allows users to fill in a structured form.
152+
153+
### Customer Feedback Template
154+

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ The main documentation for the site is organized into the following sections:
6666
:caption: Interactive Channels
6767

6868
channels/intro
69+
channels/components
6970
channels/messenger
7071
channels/wechat
7172

src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace BotSharp.Abstraction.Agents;
55

66
public abstract class AgentHookBase : IAgentHook
77
{
8+
public virtual string SelfId => throw new NotImplementedException("Please set SelfId as agent id!");
9+
810
protected Agent _agent;
911
public Agent Agent => _agent;
1012

src/Infrastructure/BotSharp.Abstraction/Agents/IAgentHook.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ namespace BotSharp.Abstraction.Agents;
44

55
public interface IAgentHook
66
{
7+
/// <summary>
8+
/// Agent Id
9+
/// </summary>
10+
string SelfId { get; }
711
Agent Agent { get; }
812
void SetAget(Agent agent);
913

src/Infrastructure/BotSharp.Abstraction/BotSharp.Abstraction.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@
3131
<PackageReference Include="System.Text.Json" Version="7.0.3" />
3232
</ItemGroup>
3333

34+
<ItemGroup>
35+
<Folder Include="Messaging\Models\" />
36+
</ItemGroup>
37+
3438
</Project>

src/Infrastructure/BotSharp.Abstraction/Functions/Models/FunctionParametersDef.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace BotSharp.Abstraction.Functions.Models;
55
public class FunctionParametersDef
66
{
77
[JsonPropertyName("type")]
8-
public string Type { get; set; } = "object";
8+
public string Type { get; set; } = "string";
99

1010
/// <summary>
1111
/// ParameterPropertyDef

src/Infrastructure/BotSharp.Abstraction/Routing/Models/RoutingRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class RoutingRule
1313
/// <summary>
1414
/// Field type: string, number, object
1515
/// </summary>
16-
public string Type { get; set; }
16+
public string Type { get; set; } = "string";
1717

1818
public bool Required { get; set; }
1919

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public async Task<Agent> LoadAgent(string id)
1515
// Before agent is loaded.
1616
foreach (var hook in hooks)
1717
{
18+
if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != id)
19+
{
20+
continue;
21+
}
22+
1823
hook.OnAgentLoading(ref id);
1924
}
2025

@@ -30,6 +35,11 @@ public async Task<Agent> LoadAgent(string id)
3035
// After agent is loaded
3136
foreach (var hook in hooks)
3237
{
38+
if (!string.IsNullOrEmpty(hook.SelfId) && hook.SelfId != id)
39+
{
40+
continue;
41+
}
42+
3343
hook.SetAget(agent);
3444

3545
if (!string.IsNullOrEmpty(agent.Instruction))

src/Infrastructure/BotSharp.Core/Evaluations/EvaluationConversationHook.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ public override Task OnResponseGenerated(RoleDialogModel message)
3131

3232
public override Task OnHumanInterventionNeeded(RoleDialogModel message)
3333
{
34-
_logger.Append(_conversation.Id, $"[{DateTime.Now}] {AgentRole.Function}: trigger event \"{message.FunctionName}\"");
34+
_logger.Append(_conversation.Id, $"[{DateTime.Now}] {AgentRole.Function}: trigger_event({{\"event\": \"{message.FunctionName}\"}})");
3535
return base.OnHumanInterventionNeeded(message);
3636
}
3737

3838
public override Task OnConversationEnding(RoleDialogModel message)
3939
{
40-
_logger.Append(_conversation.Id, $"[{DateTime.Now}] {AgentRole.Function}: trigger event \"{message.FunctionName}\"");
40+
_logger.Append(_conversation.Id, $"[{DateTime.Now}] {AgentRole.Function}: trigger_event({{\"event\": \"{message.FunctionName}\"}})");
4141
return base.OnConversationEnding(message);
4242
}
4343
}

src/Infrastructure/BotSharp.Core/Evaluations/ExecutionLogger.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using BotSharp.Abstraction.Evaluations;
22
using BotSharp.Abstraction.Repositories;
3+
using System.Text.RegularExpressions;
34

45
namespace BotSharp.Core.Evaluations;
56

@@ -17,6 +18,8 @@ public ExecutionLogger(
1718

1819
public void Append(string conversationId, string content)
1920
{
21+
content = content.Replace("\r\n", " ").Replace("\n", " ");
22+
content = Regex.Replace(content, @"\s+", " ");
2023
var db = _services.GetRequiredService<IBotSharpRepository>();
2124
db.AddExectionLogs(conversationId, new List<string> { content });
2225
}

0 commit comments

Comments
 (0)