Use text-to-speech for incoming messages
To use text-to-speech (TTS) to read incoming messages, use the Speak()
function in the message event handler. We recommend that you use the TTSDestination::QueuedLocalPlayback
destination because it ensures that messages are queued and that they only play locally.
- When a text message is received in a channel, the
IChannelSession::EventTextMessageReceived
event is raised. - When a direct message is received in a channel, the
ILoginSession::EventDirectedTextMessageReceived
event is raised.
The following code is an example of how to use TTS for channel messages:
MyChannelSession->EventTextMessageReceived.AddLambda([MyLoginSession](const IChannelTextMessage &Message)
{
// If the text message received is not from me...
if (Message.Sender() != MyLoginSession->LoginSessionId())
{
// Check if optional display name is set.
FString DisplayName = Message.Sender().DisplayName();
if (DisplayName.IsEmpty())
{ DisplayName = "Anonymous"; }
// This queues the message and lets the user know who sent it.
// NB: the colon (:) inserts a slight pause before subsequent text.
FString Text = DisplayName + " says: " + Message.Message();
MyLoginSession->TTS().Speak(Text, TTSDestination::QueuedLocalPlayback);
}
});
Tip: To only read text messages from other users in a channel, compare the current user’s AccountId with the IChannelTextMessage::Sender() value.