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.
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;
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.
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;
Pitfalls
- Resolve message authors via
Users. A message'sUserIDis an id; callGetAllUsersandUsers.GetUserByIDto show a name (see Users and profiles). - Check
ReplyCountbefore requesting replies. Only threaded messages have replies to fetch. chat:writeis required to post. Without that scopePostMessagefails even though reading works.
See also
- Authentication and connecting — connect first.
- Users and profiles — resolve message authors.
- API reference — full class reference.