Links
Encode URLs (www, http, https) as clickable links or create hyperlinks with custom text.
Generic referencing
Channel references, user mentions, and links share the same MessageElement structure with different MentionTarget types.
Add links
AddMention() adds a link to a draft message. Use CreateUrlMentionTarget() to create a mention target linking to a specified URL.
Method signature
Call AddMention() with MentionTarget of type Url. See AddMention() for details.
Refer to the AddMention() method for details.
Sample code
Reference code
This example is a self-contained code snippet ready to be run. Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code. Use it as a reference when working with other examples in this document.
Create the Hello Alex! I have sent you this link on the #offtopic channel. message where link is a URL.
Actor.h
1UFUNCTION(BlueprintCallable, Category = "PubnubChat|Samples|ChatMessageDraft")
2void AddUrlLinkSample();
Actor.cpp
1
2// ACTION REQUIRED: Replace ASample_ChatMessageDraft with name of your Actor class
3void ASample_ChatMessageDraft::AddUrlLinkSample()
4{
5
6 // Assumes Channel is a valid UPubnubChatChannel (e.g. from GetChannel)
7
8 UPubnubChatMessageDraft* MyMessageDraft = Channel->CreateMessageDraft();
9
10 MyMessageDraft->InsertText(0, "Hello Alex! I have sent you this link on the #offtopic channel.");
11
12 FString Url = "https://www.example.com";
13 FPubnubChatMentionTarget UrlMentionTarget = UPubnubChatMessageDraftUtilities::CreateUrlMentionTarget(Url);
14
15 MyMessageDraft->AddMention(33, 4, UrlMentionTarget);
show all 18 linesRemove links
RemoveMention() removes a link from a draft message.
Method signature
Call RemoveMention() at the exact offset where the link starts. See RemoveMention() for details.
Refer to the RemoveMention() method for details.
Offset value
Provide the exact position of the first character; otherwise the element is not removed.
Sample code
Reference code
This example is a self-contained code snippet ready to be run. Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code. Use it as a reference when working with other examples in this document.
Remove the link from the Hello Alex! I have sent you this link on the #offtopic channel. message where link is a URL.
Actor.h
1UFUNCTION(BlueprintCallable, Category = "PubnubChat|Samples|ChatMessageDraft")
2void MessageDraftRemoveMentionSample();
Actor.cpp
1
2// ACTION REQUIRED: Replace ASample_ChatMessageDraft with name of your Actor class
3void ASample_ChatMessageDraft::MessageDraftRemoveMentionSample()
4{
5
6 // Assumes MessageDraft is a valid UPubnubChatMessageDraft with existing mentions
7 // e.g. the message reads: "Hello Alex! I have sent you this link on the #offtopic channel."
8
9 // Remove the link mention at position 33
10 MessageDraft->RemoveMention(33);
11}
Get link suggestions
The message elements listener returns link suggestions along with user mentions and channel references.
Method signature
Refer to the Listen for message draft changes (with suggestions) section for details.
Sample code
Reference code
This example is a self-contained code snippet ready to be run. Set up your Unreal project and follow the instructions in the lines marked with ACTION REQUIRED before running the code. Use it as a reference when working with other examples in this document.
Insert the first suggested link whenever the draft is updated and links are detected.
Actor.h
1UFUNCTION(BlueprintCallable, Category = "PubnubChat|Samples|ChatMessageDraft")
2void MessageDraftInsertSuggestedMentionSample();
3
4void OnMessageDraftUpdateWithSuggestions_InsertSample(const TArray<FPubnubChatMessageElement>& MessageElements, const TArray<FPubnubChatSuggestedMention>& SuggestedMentions);
Actor.cpp
1
2// ACTION REQUIRED: Replace ASample_ChatMessageDraft with name of your Actor class
3void ASample_ChatMessageDraft::MessageDraftInsertSuggestedMentionSample()
4{
5
6 // Assumes Channel is a valid UPubnubChatChannel (e.g. from GetChannel)
7
8 UPubnubChatMessageDraft* MyMessageDraft = Channel->CreateMessageDraft();
9
10 // Bind to receive suggestions and automatically insert the first one
11 MyMessageDraft->OnMessageDraftUpdatedWithSuggestionsNative.AddUObject(this, &ASample_ChatMessageDraft::OnMessageDraftUpdateWithSuggestions_InsertSample);
12
13 // Trigger the delegate and suggestions
14 MyMessageDraft->Update("Please coordinate with @Al for the meeting.");
15}
show all 27 linesGet text links
Removed method
TextLinks() and GetTextLinks() have been removed from the Message object in the latest version of the Unreal Chat SDK. Links are now managed through MessageDraft and message elements.