//----------------------[ RTM Messagess Utilities ]-------------------//
        private TranslatedMessage ProcessChatMessage(Quest quest, out string message)
        {
            try
            {
                message = quest.Want <string>("msg");
                return(null);
            }
            catch (InvalidCastException)
            {
                Dictionary <object, object> msg = quest.Want <Dictionary <object, object> >("msg");
                TranslatedMessage           tm  = new TranslatedMessage();
                tm.source     = (string)msg["source"];
                tm.target     = (string)msg["target"];
                tm.sourceText = (string)msg["sourceText"];
                tm.targetText = (string)msg["targetText"];

                if (tm.targetText.Length == 0)
                {
                    message = tm.sourceText;
                    return(null);
                }
                else
                {
                    message = string.Empty;
                    return(tm);
                }
            }
        }
Beispiel #2
0
 public int Translate(out TranslatedMessage translatedMessage, string text,
                      TranslateLanguage destinationLanguage, TranslateLanguage sourceLanguage = TranslateLanguage.None,
                      TranslateType type = TranslateType.Chat, ProfanityType profanity = ProfanityType.Off,
                      bool postProfanity = false, int timeout = 0)
 {
     return(Translate(out translatedMessage, text, GetTranslatedLanguage(destinationLanguage),
                      GetTranslatedLanguage(sourceLanguage), type, profanity, postProfanity, timeout));
 }
Beispiel #3
0
        private int Translate(out TranslatedMessage translatedMessage, string text,
                              string destinationLanguage, string sourceLanguage = "",
                              TranslateType type = TranslateType.Chat, ProfanityType profanity = ProfanityType.Off,
                              bool postProfanity = false, int timeout = 0)
        {
            translatedMessage = null;

            TCPClient client = GetCoreClient();

            if (client == null)
            {
                return(fpnn.ErrorCode.FPNN_EC_CORE_INVALID_CONNECTION);
            }

            Quest quest = new Quest("translate");

            quest.Param("text", text);
            quest.Param("dst", destinationLanguage);

            if (sourceLanguage.Length > 0)
            {
                quest.Param("src", sourceLanguage);
            }

            if (type == TranslateType.Mail)
            {
                quest.Param("type", "mail");
            }
            else
            {
                quest.Param("type", "chat");
            }

            switch (profanity)
            {
            case ProfanityType.Stop: quest.Param("profanity", "stop"); break;

            case ProfanityType.Censor: quest.Param("profanity", "censor"); break;

            case ProfanityType.Off: quest.Param("profanity", "off"); break;
            }

            quest.Param("postProfanity", postProfanity);

            Answer answer = client.SendQuest(quest, timeout);

            if (answer.IsException())
            {
                return(answer.ErrorCode());
            }

            try
            {
                translatedMessage = new TranslatedMessage
                {
                    source     = answer.Want <string>("source"),
                    target     = answer.Want <string>("target"),
                    sourceText = answer.Want <string>("sourceText"),
                    targetText = answer.Want <string>("targetText")
                };

                return(fpnn.ErrorCode.FPNN_EC_OK);
            }
            catch (Exception)
            {
                return(fpnn.ErrorCode.FPNN_EC_CORE_INVALID_PACKAGE);
            }
        }
Beispiel #4
0
        //-- Action<TranslatedMessage, errorCode>
        private bool Translate(Action <TranslatedMessage, int> callback, string text,
                               string destinationLanguage, string sourceLanguage = "",
                               TranslateType type = TranslateType.Chat, ProfanityType profanity = ProfanityType.Off,
                               bool postProfanity = false, int timeout = 0)
        {
            TCPClient client = GetCoreClient();

            if (client == null)
            {
                return(false);
            }

            Quest quest = new Quest("translate");

            quest.Param("text", text);
            quest.Param("dst", destinationLanguage);

            if (sourceLanguage.Length > 0)
            {
                quest.Param("src", sourceLanguage);
            }

            if (type == TranslateType.Mail)
            {
                quest.Param("type", "mail");
            }
            else
            {
                quest.Param("type", "chat");
            }

            switch (profanity)
            {
            case ProfanityType.Stop: quest.Param("profanity", "stop"); break;

            case ProfanityType.Censor: quest.Param("profanity", "censor"); break;

            case ProfanityType.Off: quest.Param("profanity", "off"); break;
            }

            quest.Param("postProfanity", postProfanity);

            return(client.SendQuest(quest, (Answer answer, int errorCode) => {
                TranslatedMessage tm = null;

                if (errorCode == fpnn.ErrorCode.FPNN_EC_OK)
                {
                    try
                    {
                        tm = new TranslatedMessage();
                        tm.source = answer.Want <string>("source");
                        tm.target = answer.Want <string>("target");
                        tm.sourceText = answer.Want <string>("sourceText");
                        tm.targetText = answer.Want <string>("targetText");
                    }
                    catch (Exception)
                    {
                        errorCode = fpnn.ErrorCode.FPNN_EC_CORE_INVALID_PACKAGE;
                    }
                }
                callback(tm, errorCode);
            }, timeout));
        }
        public Answer PushBroadcastMessage(Int64 connectionId, string endpoint, Quest quest)
        {
            AdvanceAnswer.SendAnswer(new Answer(quest));

            if (questProcessor == null)
            {
                return(null);
            }

            long from = quest.Want <long>("from");
            long mid  = quest.Want <long>("mid");

            if (duplicatedFilter.CheckBroadcastMessage(from, mid) == false)
            {
                return(null);
            }

            byte   mtype = quest.Want <byte>("mtype");
            string attrs = quest.Want <string>("attrs");
            long   mtime = quest.Want <long>("mtime");

            if (mtype == RTMClient.MessageMType_Chat)
            {
                TranslatedMessage tm = ProcessChatMessage(quest, out string orginialMessage);
                if (tm == null)
                {
                    questProcessor.PushBroadcastChat(from, mid, orginialMessage, attrs, mtime);
                }
                else
                {
                    questProcessor.PushBroadcastChat(from, mid, tm, attrs, mtime);
                }

                return(null);
            }

            MessageInfo messageInfo = BuildMessageInfo(quest);

            if (mtype == RTMClient.MessageMType_Audio)
            {
                byte[] audioData = messageInfo.binaryData;

                if (!messageInfo.isBinary)
                {
                    audioData = RTMClient.ConvertStringToByteArray(messageInfo.message);
                }

                questProcessor.PushBroadcastAudio(from, mid, audioData, attrs, mtime);
                return(null);
            }

            if (mtype == RTMClient.MessageMType_Cmd)
            {
                questProcessor.PushBroadcastCmd(from, mid, messageInfo.message, attrs, mtime);
            }
            else if (mtype >= 40 && mtype <= 50)
            {
                questProcessor.PushBroadcastFile(from, mtype, mid, messageInfo.message, attrs, mtime);
            }
            else
            {
                if (messageInfo.isBinary)
                {
                    questProcessor.PushBroadcastMessage(from, mtype, mid, messageInfo.binaryData, attrs, mtime);
                }
                else
                {
                    questProcessor.PushBroadcastMessage(from, mtype, mid, messageInfo.message, attrs, mtime);
                }
            }

            return(null);
        }