Beispiel #1
0
        public static async Task LoadLibraryAsync()
        {
            if (_siml != null)
            {
                return;
            }
            await Task.Run(() =>
            {
                _log.Debug("Creating new chatbot...");
                _yandex       = new Yandex();
                _siml         = new SimlBot();
                var simlDir   = Program.FilePathInData("Siml");
                var simlFiles = Directory.GetFiles(simlDir, "*.siml", SearchOption.AllDirectories);
                foreach (var simlFile in simlFiles)
                {
                    var name = Path.GetFileName(simlFile);
                    try
                    {
                        _log.Debug($"Loading {name}");

                        var simlDocument = XDocument.Load(simlFile);
                        _siml.Import(simlDocument);
                    }
                    catch (Exception e)
                    {
                        _log.Error(e.Message);
                    }
                }
            });
        }
Beispiel #2
0
        public async void Translate(string data)
        {
            if (!_message.IsTextMessage() && !_message.IsReplyToMessage() && string.IsNullOrWhiteSpace(data))
            {
                SendUsage();
                return;
            }

            string direction = "id";
            string text;

            if (!_message.IsReplyToMessage())
            {
                var regex = Regex.Match(data, @"([a-z]{2}(?:-[a-z]{2})?) ([\S\s]+)", RegexOptions.IgnoreCase);
                if (!regex.Success)
                {
                    SendUsage();
                    return;
                }

                direction = regex.Groups[1].Value.ToLower();
                text      = regex.Groups[2].Value;
            }
            else
            {
                text = _message.ReplyToMessage.IsTextMessage() ?
                       _message.ReplyToMessage.Text : _message.ReplyToMessage.Caption;

                if (string.IsNullOrWhiteSpace(text))
                {
                    if (_message.ReplyToMessage.IsTextMessage())
                    {
                        SendUsage();
                    }
                    else
                    {
                        await BotClient.SendTextAsync(_message, "Pesan yg dibalas tidak ada keterangannya!");
                    }

                    return;
                }

                if (!string.IsNullOrWhiteSpace(data))
                {
                    var regex = Regex.Match(data, @"^([a-z]{2}(?:-[a-z]{2})?)$", RegexOptions.IgnoreCase);
                    if (!regex.Success)
                    {
                        SendUsage();
                        return;
                    }

                    direction = regex.Groups[1].Value.ToLower();
                }
            }

            _log.Debug("Direction: {0} | Text : {1}", direction, text);

            var yandex = new Yandex();
            var result = await yandex.Translate(direction, text);

            if (result.Success)
            {
                if (_message.IsReplyToMessage())
                {
                    await BotClient.SendTextAsync(_message.Chat.Id, result.Text, _message.ReplyToMessage.MessageId);
                }
                else
                {
                    await BotClient.SendTextAsync(_message, result.Text);
                }
            }
            else
            {
                await BotClient.SendTextAsync(_message, "Paijem bingung, gak bisa jawab");
            }
        }