C
C#4d ago
RSC☆Lele

'Emoji Translator': emoji-api.com doesn't work

I need to make a Emoji Translator for my school, and since I like challenging myself a bit, I wanted to use an API to get more emojis than just 10 pre defined emojis. Now when I use the emoji-api.com api, it says it loaded all the emojis (3k+), but they always just get outputtet as plain Text. (Code in the Comments)
4 Replies
RSC☆Lele
RSC☆LeleOP4d ago
// EmojiLib.cs - Loading the Emojis
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace EmojiTranslator {
public static class EmojiLib {
private static readonly HttpClient client = new HttpClient();
private static Dictionary<string, string> _emojis = new Dictionary<string, string>();

public static async Task LoadEmojisAsync(string apiKey) {
try {
var response = await client.GetStringAsync($"https://emoji-api.com/emojis?access_key={apiKey}");
var emojis = JsonConvert.DeserializeObject<List<Emoji>>(response);
_emojis.Clear();
foreach (var emoji in emojis) {
if (!string.IsNullOrEmpty(emoji.Character)) {
_emojis[emoji.Slug.ToLower()] = emoji.Character;
_emojis[emoji.UnicodeName.ToLower()] = emoji.Character;
}
}
Console.WriteLine($"Loaded {_emojis.Count} emojis.");
} catch (Exception ex) {
Console.WriteLine($"Error loading emojis: {ex.Message}");
_emojis["pizza"] = ":pizza:"; _emojis["computer"] = ":computer:"; _emojis["happy"] = ":blush:";
}
}

public static Dictionary<string, string> Emojis => _emojis;

public class Emoji {
[JsonProperty("slug")] public string Slug { get; set; }
[JsonProperty("character")] public string Character { get; set; }
[JsonProperty("unicodeName")] public string UnicodeName { get; set; }
}
}
}
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace EmojiTranslator {
public static class EmojiLib {
private static readonly HttpClient client = new HttpClient();
private static Dictionary<string, string> _emojis = new Dictionary<string, string>();

public static async Task LoadEmojisAsync(string apiKey) {
try {
var response = await client.GetStringAsync($"https://emoji-api.com/emojis?access_key={apiKey}");
var emojis = JsonConvert.DeserializeObject<List<Emoji>>(response);
_emojis.Clear();
foreach (var emoji in emojis) {
if (!string.IsNullOrEmpty(emoji.Character)) {
_emojis[emoji.Slug.ToLower()] = emoji.Character;
_emojis[emoji.UnicodeName.ToLower()] = emoji.Character;
}
}
Console.WriteLine($"Loaded {_emojis.Count} emojis.");
} catch (Exception ex) {
Console.WriteLine($"Error loading emojis: {ex.Message}");
_emojis["pizza"] = ":pizza:"; _emojis["computer"] = ":computer:"; _emojis["happy"] = ":blush:";
}
}

public static Dictionary<string, string> Emojis => _emojis;

public class Emoji {
[JsonProperty("slug")] public string Slug { get; set; }
[JsonProperty("character")] public string Character { get; set; }
[JsonProperty("unicodeName")] public string UnicodeName { get; set; }
}
}
}
// Translator.cs - Translating Text to Emojis
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace EmojiTranslator {
public class Translator {
public async Task<string> TranslateAsync(string text) {
var result = new StringBuilder();
var wordRegex = new Regex(@"\b[\w'-]+\b");
var lastIndex = 0;

foreach (Match match in wordRegex.Matches(text)) {
if (match.Index > lastIndex)
result.Append(text.AsSpan(lastIndex, match.Index - lastIndex));

var word = match.Value.ToLower();
result.Append(EmojiLib.Emojis.TryGetValue(word, out var emoji) ? emoji : match.Value);
lastIndex = match.Index + match.Length;
}
if (lastIndex < text.Length) result.Append(text.AsSpan(lastIndex));
return result.ToString();
}
}
}
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace EmojiTranslator {
public class Translator {
public async Task<string> TranslateAsync(string text) {
var result = new StringBuilder();
var wordRegex = new Regex(@"\b[\w'-]+\b");
var lastIndex = 0;

foreach (Match match in wordRegex.Matches(text)) {
if (match.Index > lastIndex)
result.Append(text.AsSpan(lastIndex, match.Index - lastIndex));

var word = match.Value.ToLower();
result.Append(EmojiLib.Emojis.TryGetValue(word, out var emoji) ? emoji : match.Value);
lastIndex = match.Index + match.Length;
}
if (lastIndex < text.Length) result.Append(text.AsSpan(lastIndex));
return result.ToString();
}
}
}
// Program.cs
using System;
using System.Threading.Tasks;

namespace EmojiTranslator {
class Program {
static async Task Main(string[] args) {
Console.OutputEncoding = System.Text.Encoding.UTF8;
const string apiKey = "YOUR_API_KEY";
var translator = new Translator();

Console.WriteLine("Loading emojis...");
await EmojiLib.LoadEmojisAsync(apiKey);

Console.WriteLine("\nExamples:");
Console.WriteLine($"pizza → {await translator.TranslateAsync("pizza")}");
Console.WriteLine($"computer → {await translator.TranslateAsync("computer")}");

while (true) {
Console.Write("\nEnter text (or 'quit'): ");
var input = Console.ReadLine();
if (input?.ToLower() == "quit") break;
Console.WriteLine($"Result: {await translator.TranslateAsync(input)}");
}
}
}
}
using System;
using System.Threading.Tasks;

namespace EmojiTranslator {
class Program {
static async Task Main(string[] args) {
Console.OutputEncoding = System.Text.Encoding.UTF8;
const string apiKey = "YOUR_API_KEY";
var translator = new Translator();

Console.WriteLine("Loading emojis...");
await EmojiLib.LoadEmojisAsync(apiKey);

Console.WriteLine("\nExamples:");
Console.WriteLine($"pizza → {await translator.TranslateAsync("pizza")}");
Console.WriteLine($"computer → {await translator.TranslateAsync("computer")}");

while (true) {
Console.Write("\nEnter text (or 'quit'): ");
var input = Console.ReadLine();
if (input?.ToLower() == "quit") break;
Console.WriteLine($"Result: {await translator.TranslateAsync(input)}");
}
}
}
}
I also Tried using the Predefined Emojis, and they work, so I think its not the encoding Output: (not using the predefined emojis):
Unicode Unterstützung: Microsoft Windows NT 10.0.26100.0
UTF-8 Output: Unicode (UTF-8)
Test-Emoji: :computer::pizza::blush:ᾮ0 //These work in the Console)

Loading emojis...
Loaded 3775 emoji mappings

Sample emoji mappings:

Test translations:
'pizza' => 'pizza'
No emoji found for 'pizza'
'computer' => 'computer'
No emoji found for 'computer'
'happy face' => 'happy face'
No emoji found for 'happy'
No emoji found for 'face'
Unicode Unterstützung: Microsoft Windows NT 10.0.26100.0
UTF-8 Output: Unicode (UTF-8)
Test-Emoji: :computer::pizza::blush:ᾮ0 //These work in the Console)

Loading emojis...
Loaded 3775 emoji mappings

Sample emoji mappings:

Test translations:
'pizza' => 'pizza'
No emoji found for 'pizza'
'computer' => 'computer'
No emoji found for 'computer'
'happy face' => 'happy face'
No emoji found for 'happy'
No emoji found for 'face'
TanzFang
TanzFang4d ago
Your issue is you're matching the exact text to an emoji slug or unicodeName, when really the text is a substring of either. ie pizza actually has a unicodeName of E0.6 Pizza which is your key in the dictionary _emojis. What you need to do is use your wordRegex against the key in _emojis:
var keyValuePair = EmojiLib.Emojis.FirstOrDefault(kvp => Regex.IsMatch(kvp.Key, text));

return keyValuePair.Value;
var keyValuePair = EmojiLib.Emojis.FirstOrDefault(kvp => Regex.IsMatch(kvp.Key, text));

return keyValuePair.Value;
RSC☆Lele
RSC☆LeleOP2d ago
Ohh okay thank you. I'll try it out once I get home (:

Did you find this page helpful?