Skip to main content
Table of Contents

Conversations and messages

Conversations are the channels and direct messages a user belongs to, and each holds a history of messages. TTMSFNCCloudSlack lists a user's conversations, reads a conversation's history, posts new messages, and loads the replies and files attached to a message. This chapter covers listing conversations and reading history, then posting messages and reading their replies.

Listing conversations and reading history

Call GetUserConversations(AUserID, ACount) to list the conversations a user is in; they arrive in OnGetUserConversations. Each TTMSFNCCloudSlackConversation exposes its Name and a Messages collection. Load a conversation's history with GetConversationHistory(AConversation, ACount), which fills Messages and raises OnGetConversationHistory. Each TTMSFNCCloudSlackMessage carries Text, the resolved User (and UserID), ReplyCount, Replies, and Files.

List conversations and read historyPascalC++
procedure TForm1.SlackGetUserConversations(Sender: TObject;
  const ARequestResult: TTMSFNCCloudBaseRequestResult;
  Conversations: TTMSFNCCloudSlackConversations;
  AUser: TTMSFNCCloudSlackUser; ErrorMessage: string);
var
  I: Integer;
begin
  if not ARequestResult.Success then
    Exit;
  cvList.Items.Clear;
  for I := 0 to Conversations.Count - 1 do
    cvList.AddItem(Conversations[I].Name, '', Conversations[I]);
end;

procedure TForm1.LoadHistory(AConversation: TTMSFNCCloudSlackConversation);
begin
  // Loads up to 50 messages; result arrives in OnGetConversationHistory.
  FSlack.GetConversationHistory(AConversation, 50);
end;

procedure TForm1.SlackGetConversationHistory(Sender: TObject;
  const ARequestResult: TTMSFNCCloudBaseRequestResult;
  AConversation: TTMSFNCCloudSlackConversation;
  Messages: TTMSFNCCloudSlackMessages; ErrorMessage: string);
var
  I: Integer;
begin
  if not ARequestResult.Success then
    Exit;
  msgList.Items.Clear;
  for I := 0 to Messages.Count - 1 do
    msgList.AddItem(Messages[I].User.UserName + ': ' + Messages[I].Text, '', Messages[I]);
end;
// cvList and msgList are list controls whose AddItem(Text, Image, DataObject) stores the
// data object with the entry (the Cloud Pack demo wraps this in its own list box).
void __fastcall TForm1::SlackGetUserConversations(TObject *Sender,
  TTMSFNCCloudBaseRequestResult *ARequestResult,
  TTMSFNCCloudSlackConversations *Conversations,
  TTMSFNCCloudSlackUser *AUser, System::UnicodeString ErrorMessage)
{
  if (!ARequestResult->Success)
    return;
  cvList->Items->Clear();
  for (int I = 0; I < Conversations->Count; I++)
    cvList->AddItem(Conversations->Items[I]->Name, "", Conversations->Items[I]);
}

void __fastcall TForm1::LoadHistory(TTMSFNCCloudSlackConversation *AConversation)
{
  // Loads up to 50 messages; result arrives in OnGetConversationHistory.
  FSlack->GetConversationHistory(AConversation, 50);
}

void __fastcall TForm1::SlackGetConversationHistory(TObject *Sender,
  TTMSFNCCloudBaseRequestResult *ARequestResult,
  TTMSFNCCloudSlackConversation *AConversation,
  TTMSFNCCloudSlackMessages *Messages, System::UnicodeString ErrorMessage)
{
  if (!ARequestResult->Success)
    return;
  msgList->Items->Clear();
  for (int I = 0; I < Messages->Count; I++)
    msgList->AddItem(Messages->Items[I]->User->UserName + ": " + Messages->Items[I]->Text, "", Messages->Items[I]);
}

Posting messages and reading replies

Post to a conversation with PostMessage(AConversation, AText); the new message arrives in OnPostMessage. A threaded message exposes its responses through GetAllMessageReplies, which populates the message's Replies and raises OnGetAllMessageReplies; the same message's Files collection lists any attachments. This example, combining a conversation from the previous section with its messages, posts text and reads a thread's replies and files.

Post a message and read repliesPascalC++
procedure TForm1.Send(AConversation: TTMSFNCCloudSlackConversation; const AText: string);
begin
  FSlack.PostMessage(AConversation, AText); // result arrives in OnPostMessage
end;

procedure TForm1.LoadReplies(AMessage: TTMSFNCCloudSlackMessage);
begin
  // A threaded message exposes its replies once requested.
  if AMessage.ReplyCount > 0 then
    FSlack.GetAllMessageReplies(AMessage); // result arrives in OnGetAllMessageReplies
end;

procedure TForm1.SlackGetAllMessageReplies(Sender: TObject;
  const ARequestResult: TTMSFNCCloudBaseRequestResult;
  AMessage: TTMSFNCCloudSlackMessage; ErrorMessage: string);
var
  I: Integer;
begin
  if not ARequestResult.Success then
    Exit;
  rplyList.Items.Clear;
  for I := 0 to AMessage.Replies.Count - 1 do
    rplyList.AddItem(AMessage.Replies[I].UserID + ': ' + AMessage.Replies[I].Text, '', AMessage.Replies[I]);

  // A message can also carry uploaded files.
  for I := 0 to AMessage.Files.Count - 1 do
    fileList.AddItem(AMessage.Files[I].Name, '', AMessage.Files[I]);
end;
void __fastcall TForm1::Send(TTMSFNCCloudSlackConversation *AConversation, const System::UnicodeString AText)
{
  FSlack->PostMessage(AConversation, AText); // result arrives in OnPostMessage
}

void __fastcall TForm1::LoadReplies(TTMSFNCCloudSlackMessage *AMessage)
{
  // A threaded message exposes its replies once requested.
  if (AMessage->ReplyCount > 0)
    FSlack->GetAllMessageReplies(AMessage); // result arrives in OnGetAllMessageReplies
}

void __fastcall TForm1::SlackGetAllMessageReplies(TObject *Sender,
  TTMSFNCCloudBaseRequestResult *ARequestResult,
  TTMSFNCCloudSlackMessage *AMessage, System::UnicodeString ErrorMessage)
{
  if (!ARequestResult->Success)
    return;
  rplyList->Items->Clear();
  for (int I = 0; I < AMessage->Replies->Count; I++)
    rplyList->AddItem(AMessage->Replies->Items[I]->UserID + ": " + AMessage->Replies->Items[I]->Text, "", AMessage->Replies->Items[I]);

  // A message can also carry uploaded files.
  for (int I = 0; I < AMessage->Files->Count; I++)
    fileList->AddItem(AMessage->Files->Items[I]->Name, "", AMessage->Files->Items[I]);
}

Pitfalls

  • Resolve message authors via Users. A message's UserID is an id; call GetAllUsers and Users.GetUserByID to show a name (see Users and profiles).
  • Check ReplyCount before requesting replies. Only threaded messages have replies to fetch.
  • chat:write is required to post. Without that scope PostMessage fails even though reading works.

See also