ChannelSurf icon indicating copy to clipboard operation
ChannelSurf copied to clipboard

Feature Enhancement: Replace @UserCode in message with display Name.

Open traxinv opened this issue 7 years ago • 1 comments
trafficstars

Good day,

This is hardly the best way to do this but I needed to get it to work quickly, it turns this message:

<@SD578SDSS> joined the channel

into this:

@Steven_User Joined the channel

Call From code:

static List<Models.Combined.AttachmentsMapping> GetAndUploadMessages(Models.Combined.ChannelsMapping channelsMapping, string basePath, List<ViewModels.SimpleUser> slackUserList, String aadAccessToken, String selectedTeamId,

bool copyFileAttachments) { . . . var messageTs = (string)obj.SelectToken("ts"); var messageText = (string)obj.SelectToken("text");

if (messageText.IndexOf("<@") > -1) { messageText = ReplaceQuotedUsersInMessage(messageText, slackUserList); } . . . }

Procedure:

static string ReplaceQuotedUsersInMessage(string MessageText, List<ViewModels.SimpleUser> slackUserList) {

for (int i = 0; i < slackUserList.Count; i++) { if (MessageText.IndexOf("<@") > -1) { MessageText = MessageText.Replace(string.Concat("<@", slackUserList[i].userId, ">"), string.Concat("@", slackUserList[i].real_name.Replace(" ","_"))); } else { break; } }

return MessageText;

}

traxinv avatar Mar 27 '18 15:03 traxinv

Here's my solution to the in-message usernames:

Match match = Regex.Match(messageText, @"<@([A-Za-z0-9-]+)>", RegexOptions.IgnoreCase); while (match.Success) { string inMsgSlackUserId = match.Value.Replace("<", "").Replace("@", "").Replace(">", ""); messageText = Regex.Replace(messageText, @"<@([A-Za-z0-9-]+)>", findInMsgUser(inMsgSlackUserId, slackUserList)); match = Regex.Match(messageText, @"<@([A-Za-z0-9-]+)>", RegexOptions.IgnoreCase); }

    static string findInMsgUser(string user, List<ViewModels.SimpleUser> slackUserList)
    {            
        if (!String.IsNullOrEmpty(user))
        {
            if (user != "U00|")
            {
                var simpleUser = slackUserList.FirstOrDefault(w => w.userId == user);
                if (simpleUser != null)
                {
                    return simpleUser.name;
                }

            }
            else
            {
                return "SlackBot";
            }
        }
        return "";
    }

argvMatey avatar Apr 30 '18 18:04 argvMatey