Skip to content

Commit 2dc6376

Browse files
committed
Fix rich content bug.
1 parent 80e157a commit 2dc6376

File tree

6 files changed

+66
-28
lines changed

6 files changed

+66
-28
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ It's written in C# running on .Net Core that is full cross-platform framework, t
2929
* Build, test, evaluate and audit your LLM agent in one place.
3030
* Support different open source UI [Chatbot UI](src/Plugins/BotSharp.Plugin.ChatbotUI/Chatbot-UI.md), [HuggingChat UI](src/Plugins/BotSharp.Plugin.HuggingFace/HuggingChat-UI.md).
3131
* Abstract standard Rich Content data structure. Integrate with popular message channels like Facebook Messenger, Slack and Telegram.
32+
* Provide RESTful Open API and WebSocket real-time communication.
3233

3334
### Quick Started
3435
1. Run backend service
@@ -61,7 +62,7 @@ The core module is mainly composed of abstraction and framework function impleme
6162
BotSharp uses component design, the kernel is kept to a minimum, and business functions are implemented by external components. The modular design also allows contributors to better participate. Below are the bulit-in plugins:
6263

6364
#### Data Storages
64-
- BotSharp.Plugin.FileRepository
65+
- BotSharp.Core.Repository
6566
- BotSharp.Plugin.MongoStorage
6667

6768
#### LLMs

src/Infrastructure/BotSharp.Abstraction/Messaging/Models/RichContent/Template/ButtonTemplateMessage.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
namespace BotSharp.Abstraction.Messaging.Models.RichContent.Template
22
{
3+
/// <summary>
4+
/// https://developers.facebook.com/docs/messenger-platform/send-messages/buttons
5+
/// </summary>
36
public class ButtonTemplateMessage : IRichMessage
47
{
58
public string Text { get; set; } = string.Empty;
@@ -11,8 +14,17 @@ public class ButtonTemplateMessage : IRichMessage
1114

1215
public class ButtonElement
1316
{
14-
public string Type { get; set; } = string.Empty;
17+
/// <summary>
18+
/// web_url, postback, phone_number
19+
/// </summary>
20+
public string Type { get; set; } = "web_url";
21+
22+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
1523
public string? Url { get; set; }
24+
25+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
26+
public string? Payload { get; set; }
27+
1628
public string Title { get; set; } = string.Empty;
1729
}
1830
}

src/Plugins/BotSharp.Plugin.MetaMessenger/Controllers/WebhookController.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ public async Task<ActionResult<WebhookResponse>> Messages([FromRoute] string age
5757
if (req.Entry[0].Messaging[0].Message != null)
5858
{
5959
senderId = req.Entry[0].Messaging[0].Sender.Id;
60-
message = req.Entry[0].Messaging[0].Message.Text;
60+
61+
if (req.Entry[0].Messaging[0].Message.QuickReply != null)
62+
{
63+
message = req.Entry[0].Messaging[0].Message.QuickReply.Payload;
64+
}
65+
else
66+
{
67+
message = req.Entry[0].Messaging[0].Message.Text;
68+
}
6169
}
6270
else if (req.Entry[0].Messaging[0].Postback != null)
6371
{

src/Plugins/BotSharp.Plugin.MetaMessenger/Services/MessageHandleService.cs

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Messaging.Models.RichContent;
2+
using BotSharp.Abstraction.Utilities;
23

34
namespace BotSharp.Plugin.MetaMessenger.Services;
45

@@ -53,34 +54,48 @@ await messenger.SendMessage(setting.ApiVersion, setting.PageId,
5354
var replies = new List<IRichMessage>();
5455
var result = await conv.SendMessage(agentId, new RoleDialogModel("user", message), async msg =>
5556
{
56-
if (!string.IsNullOrEmpty(msg.Content))
57+
if (msg.RichContent != null)
5758
{
58-
replies.Add(new TextMessage(msg.Content));
59-
}
59+
// Official API doesn't support to show extra content above the products
60+
if (!string.IsNullOrEmpty(msg.RichContent.Message.Text) &&
61+
// avoid duplicated text
62+
msg.RichContent.Message is not QuickReplyMessage)
63+
{
64+
replies.Add(new TextMessage(msg.RichContent.Message.Text));
65+
}
6066

61-
if (msg.RichContent.Message is GenericTemplateMessage genericTemplate)
62-
{
63-
replies.Add(new AttachmentMessage
67+
if (msg.RichContent.Message is GenericTemplateMessage genericTemplate)
6468
{
65-
Attachment = new AttachmentBody
69+
replies.Add(new AttachmentMessage
6670
{
67-
Payload = genericTemplate
68-
}
69-
});
70-
}
71-
else if (msg.RichContent.Message is CouponTemplateMessage couponTemplate)
72-
{
73-
replies.Add(new AttachmentMessage
71+
Attachment = new AttachmentBody
72+
{
73+
Payload = genericTemplate
74+
}
75+
});
76+
}
77+
else if (msg.RichContent.Message is CouponTemplateMessage couponTemplate)
7478
{
75-
Attachment = new AttachmentBody
79+
replies.Add(new AttachmentMessage
7680
{
77-
Payload = couponTemplate
78-
}
79-
});
81+
Attachment = new AttachmentBody
82+
{
83+
Payload = couponTemplate
84+
}
85+
});
86+
}
87+
else if (msg.RichContent.Message is QuickReplyMessage quickReplyMessage)
88+
{
89+
replies.Add(quickReplyMessage);
90+
}
91+
else
92+
{
93+
replies.Add(msg.RichContent.Message);
94+
}
8095
}
81-
else if (msg.RichContent.Message is not TextMessage)
96+
else
8297
{
83-
replies.Add(msg.RichContent.Message);
98+
replies.Add(new TextMessage(msg.Content));
8499
}
85100
},
86101
_ => Task.CompletedTask,
@@ -90,10 +105,10 @@ await messenger.SendMessage(setting.ApiVersion, setting.PageId,
90105
foreach(var reply in replies)
91106
{
92107
await messenger.SendMessage(setting.ApiVersion, setting.PageId,
93-
new SendingMessageRequest(setting.PageAccessToken, recipient)
94-
{
95-
Message = JsonSerializer.Serialize(reply, _serializerOptions)
96-
});
108+
new SendingMessageRequest(setting.PageAccessToken, recipient)
109+
{
110+
Message = JsonSerializer.Serialize(reply, _serializerOptions)
111+
});
97112
Thread.Sleep(500);
98113
}
99114

src/Plugins/BotSharp.Plugin.MetaMessenger/WebhookModels/WebhookMessageBody.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ public class WebhookMessageBody
1212

1313
[JsonPropertyName("quick_reply")]
1414
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
15-
public QuickReplyMessage QuickReply { get;set; }
15+
public WebhookMessagePostback QuickReply { get;set; }
1616
}

src/Plugins/BotSharp.Plugin.MetaMessenger/WebhookModels/WebhookMessagePostback.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ namespace BotSharp.Plugin.MetaMessenger.WebhookModels;
22

33
public class WebhookMessagePostback
44
{
5+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
56
public string Title { get; set; }
7+
68
public string Payload { get; set; }
79

810
[JsonPropertyName("mid")]

0 commit comments

Comments
 (0)