Table of contents
TOC
Collapse the table of content
Expand the table of content

Send and receive text in a conversation

Ajinkya Kher|Last Updated: 12/13/2016
|
1 Contributor

Applies to: Skype for Business 2015

Sending and receiving a message in a conversation

Receiving messages in a conversation involves getting a conversation object and conversation instant message modality. When the conversation is connected, listen for new messages. When a message is received, get the sender name and message text.

Receive a message

  1. Get the conversation history service.

    var historyService = conversation.historyService;
    
  2. Listen for new incoming messages.

  historyService.activityItems.added(function (message) {…});
  1. Determine the type and direction of the activity item.
  if (item.type() == 'TextMessage') { …}
  if (item.direction() == 'Incoming') { … }
  1. Get the message sender's name.

    The displayName property contains the display name of the sender. If the display name is not available, the sender's id value is returned. Otherwise, the example returns 'Unknown participant'.

  item.sender.person.displayName() || item.sender.person.id() || 'Unknown participant';
  1. Get the message text.
  item.text();
The message can also be sent as an HTML-formatted message.
  item.html();

Send a message

Sending a message involves getting a connected conversation and the conversation chat service. When the user has provided text for a message, send the message on the chatService of the conversation.

  1. Get the conversation chat service.
  var chatService = conversation.chatService;
  1. Listen for the conversation.SelfParticipant.Chat.state to change to "Connected."
  conversation.selfParticipant.chat.state.when('Connected', function () {…});
  1. Send message text.
  chatService.sendMessage(text)

Examples

The following example puts the previous procedure steps together to show how to receive text messages.

// add the IM modality
var historyService = conversation.historyService;
historyService.activityItems.added(function (item) {
    if (item.type() == 'TextMessage') {
        if (item.direction() == 'Incoming') {
            console.log('received a text message: ', item.text());
        } else if (item.direction() == 'Outgoing') {
            console.log('sent a text message: ', item.text());
        }
    }
});

The following example puts the previous procedure steps to together to show how to send a message.

// get the chat service
var chatService = conversation.chatService;
// when the selfParticipant chat state becomes connected
conversation.selfParticipant.chat.state.when('Connected', function () {
    chatService.sendMessage(text);
});
© 2017 Microsoft