using Newtonsoft.Json; using System; using System.Collections.Generic; namespace LineMessaging { public class LineTextMessage : ILineMessage { [JsonProperty("type")] public MessageType Type => MessageType.Text; [JsonProperty("text")] public string Text { get; set; } [JsonProperty("emojis")] public List Emojis { get; set; } public void AddEmoji(int index, string productId, string emojiId) { if (Emojis == null) Emojis = new List(); Emojis.Add(new Emoji() { Index = index, ProductId = productId, EmojiId = emojiId }); } public void AddEmoji(int indexStart, int indexEnd, string productId, string emojiId) { if (Emojis == null) Emojis = new List(); for (int i = indexStart; i < indexEnd; i++) { Emojis.Add(new Emoji() { Index = i, ProductId = productId, EmojiId = emojiId }); } } public void AddEmoji(string replaceSymbol, string productId, string emojiId) { if (Emojis == null) Emojis = new List(); this.Text = this.Text.Replace(replaceSymbol, "|"); foreach (var i in AllIndexesOf(this.Text, "|")) { Emojis.Add(new Emoji() { Index = i, ProductId = productId, EmojiId = emojiId }); } this.Text = this.Text.Replace("|", "$"); } private IEnumerable AllIndexesOf(string str, string searchstring) { int minIndex = str.IndexOf(searchstring); while (minIndex != -1) { yield return minIndex; minIndex = str.IndexOf(searchstring, minIndex + searchstring.Length); } } } public class Emoji { [JsonProperty("index")] public int Index { get; set; } [JsonProperty("productId")] public string ProductId { get; set; } [JsonProperty("emojiId")] public string EmojiId { get; set; } } }