Table of Contents

Users and profiles

TTMSFNCCloudSlack reads the members of the workspace and their profile details. It can fetch one user on demand, load the whole directory into its Users collection for local lookup, and download profile pictures. This chapter covers requesting a user, reading profile fields, and resolving users locally by id.

Reading a user

Call GetUserInformation(AUserID) to fetch one member; the result arrives in OnGetUserInformation with a TTMSFNCCloudSlackUser. The user exposes UserName, RealName, Color/HTMLColor, TimeZoneLabel, UpdateTimeStamp, and a Profile sub-object with Email, StatusText, and ImageUrl.

procedure TForm1.LoadUser(const AUserID: string);
begin
  FSlack.GetUserInformation(AUserID); // result arrives in OnGetUserInformation
end;

procedure TForm1.SlackGetUserInformation(Sender: TObject;
  const ARequestResult: TTMSFNCCloudBaseRequestResult;
  AUser: TTMSFNCCloudSlackUser; ErrorMessage: string);
begin
  if not ARequestResult.Success then
  begin
    ShowMessage(ErrorMessage);
    Exit;
  end;

  Label1.Text := AUser.RealName + ' (' + AUser.UserName + ')';
  Label2.Text := AUser.Profile.Email;
  Label3.Text := AUser.Profile.StatusText;

  // The profile picture downloads asynchronously; handle OnGetProfilePicture.
  AUser.Profile.DownloadProfilePicture;
end;

procedure TForm1.LookUpUser(const AUserID: string);
var
  User: TTMSFNCCloudSlackUser;
begin
  // After GetAllUsers completes, look a user up locally by id.
  User := FSlack.Users.GetUserByID(AUserID);
  if Assigned(User) then
    Label1.Text := User.UserName;
end;

Profile pictures

A profile picture is fetched separately: call Profile.DownloadProfilePicture and handle OnGetProfilePicture, which delivers the image as a stream you can load into a bitmap. Set ProfileImageSize (for example siz192) to choose the resolution before downloading.

Looking users up locally

GetAllUsers loads the whole member directory into the Users collection. Once it completes you can resolve a user by id without another request using Users.GetUserByID(AUserID) — handy for turning the UserID on a message into a display name and color.

Pitfalls

  • Read in the completion event. A user's fields are only guaranteed once OnGetUserInformation fires.
  • Profile pictures need OnGetProfilePicture. The picture is not part of the user information result; it arrives through its own event.
  • GetUserByID needs GetAllUsers first. Without the directory loaded the lookup returns nothing.

See also