Ejemplo n.º 1
0
        /// <summary>
        /// Detect the language of the message and then translates
        /// it if there is a rule for the language
        /// </summary>
        /// <param name="message">The text to be translated.</param>
        /// <returns>The translated text or <c>null</c> if either the translation
        /// failed or the text was empty or there was no translation rule for this language.</returns>
        async Task TranslateText(string message, IMessageProcessedCallback callback)
        {
            try
            {
                string sourceLang = await yandexTranslationService.DetectLanguage(message,
                                                                                  new List <string>(translationRules.Keys));

                string targetLang = translationRules.GetValueOrDefault(sourceLang, null);

                string translation = await yandexTranslationService.
                                     TranslateText(message, sourceLang, targetLang);

                await callback.OnAnswerMessageGenerated(translation);
            } catch (Exception e)
            {
                Console.WriteLine("Exception when trying to translate. message:  " +
                                  message + " Exception: " + e);
                await callback.OnAnswerMessageGenerated(TRANSLATION_FAILED_MESSAGE);
            }
        }
Ejemplo n.º 2
0
 public async Task OnMessageReceived(Message m, IMessageProcessedCallback callback)
 {
     if (m.Type == MessageType.Text && !string.IsNullOrWhiteSpace(m.Text))
     {
         Command command = Command.TryParse(m.Text);
         if (command != null)
         {
             await callback.OnAnswerMessageGenerated(await ReactToCommand(command));
         }
         else if (active)
         {
             //message is a text to be translated:
             await TranslateText(m.Text.Trim(), callback);
         }
         else if (firstMessage)
         {
             await callback.OnAnswerMessageGenerated(FIRST_MESSAGE);
         }
         firstMessage = false;
     }
 }