Exemple #1
0
        private bool ParseVariableTrigger(CommsLayerMessage msg)
        {
            var    dv = JsonConvert.DeserializeObject <VariableDataDBO>(data.ToString());
            string variableName, value;

            switch (modifier)
            {
            case (int)SingleVariableTriggerModifiers.MatchPresetToValue:
                variableName = dv.variable;
                value        = dv.value;

                return(UserVariablesSingleton.DoesSingleVariableMatch(variableName, value));

            case (int)SingleVariableTriggerModifiers.MatchMessageToValue:
                if (msg.type != CommsLayerMessage.Type.Text)
                {
                    return(false);
                }

                variableName = dv.variable;
                value        = msg.message.ToString();

                return(UserVariablesSingleton.DoesSingleVariableMatch(variableName, value));

            default:
                return(false);
            }
        }
Exemple #2
0
        /// <summary>
        /// If any <TriggerSubgroup> is false, then the <TriggerGroup> evaluates as false
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public TriggerOutput Eval(CommsLayerMessage msg)
        {
            if (subgroups == null)
            {
                AssembleSubgroups();
            }

            if (msg.isPrivateChat != privateTrigger)
            {
                return(new TriggerOutput(false, null));
            }

            var groupOutput = new TriggerOutput(true, null);

            foreach (var keypair in subgroups)
            {
                var subgroup       = keypair.Value;
                var subgroupOutput = subgroup.Eval(msg);

                if (subgroupOutput.result == false)
                {
                    return(subgroupOutput);
                }
            }
            return(groupOutput);
        }
Exemple #3
0
        private bool ParseImageTrigger(CommsLayerMessage msg)
        {
            if (msg.type != CommsLayerMessage.Type.Image)
            {
                return(false);
            }

            switch (modifier)
            {
            case 0:
                var triggerData  = JsonConvert.DeserializeObject <ImageTriggerMetadataDBO>(data.ToString());
                var fileMetadata = (Telegram.Bot.Types.File)msg.message;
                var fileSize     = fileMetadata.FileSize / (1024.0f * 1024.0f);

                bool result = true;

                if (triggerData.minSize.HasValue && fileSize < triggerData.minSize.Value)
                {
                    result = false;
                }

                if (triggerData.maxSize.HasValue && fileSize > triggerData.maxSize.Value)
                {
                    result = false;
                }

                return(result);

            default:
                return(false);
            }
        }
Exemple #4
0
        private void ParseDictionaryReaction(CommsLayerMessage clm)
        {
            var key = clm.senderId.ToString();
            var dv  = JsonConvert.DeserializeObject <VariableDataDBO>(data.ToString());

            switch (modifier)
            {
            case (int)DictionaryVariableReactionModifiers.SavePresetValue:
                UserVariablesSingleton.SetDictionaryVariableItem(dv.variable, key, dv.value);
                break;

            case (int)DictionaryVariableReactionModifiers.SaveUserMessage:

                if (clm.type != CommsLayerMessage.Type.Text)
                {
                    return;
                }

                UserVariablesSingleton.SetDictionaryVariableItem(dv.variable, key, (string)clm.message);
                break;

            case (int)DictionaryVariableReactionModifiers.DeleteEntry:
                UserVariablesSingleton.DeleteDictionaryVariable(dv.variable, key);
                break;
            }
        }
Exemple #5
0
        private void ParseSpecialReaction(CommsLayerMessage msg)
        {
            switch (modifier)
            {
            case (int)IOReactionModifiers.CreateCSV:
                var csvMetadata = JsonConvert.DeserializeObject <CsvMetadataDBO>(data.ToString());
                UserVariablesSingleton.DumpToCSV(csvMetadata);
                break;

            case (int)IOReactionModifiers.SaveImage:
                var fileMetadata = (Telegram.Bot.Types.File)msg.message;

                var imageMetadata = JsonConvert.DeserializeObject <ImageMetadataDBO>(data.ToString());

                FileManager.DownloadAndSaveFile(fileMetadata.FilePath, $"{msg.senderId}{imageMetadata.suffix}.jpg", imageMetadata.subfolderName);
                break;
            }
        }
Exemple #6
0
        /// <summary>
        /// If any <Trigger> is <true>, then the <TriggerSubgroup> evaluates as <true>
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public TriggerOutput Eval(CommsLayerMessage msg)
        {
            var subgroupOutput = new TriggerOutput(true, null);

            foreach (var trigger in triggers)
            {
                var triggerOutput = trigger.Eval(msg);

                if (triggerOutput.result)
                {
                    return(triggerOutput);
                }
                else
                {
                    subgroupOutput = triggerOutput;
                }
            }
            return(subgroupOutput);
        }
Exemple #7
0
        private bool ParseTriggerInput(CommsLayerMessage msg)
        {
            switch (triggerType)
            {
            case TriggerType.Text:
                return(ParseTextTrigger(msg));

            case TriggerType.DictionaryVariable:
                return(ParseDictionaryTrigger(msg));

            case TriggerType.SingleVariable:
                return(ParseVariableTrigger(msg));

            case TriggerType.Image:
                return(ParseImageTrigger(msg));

            default:
                return(false);
            }
        }
Exemple #8
0
        public ReactionOutputMessage React(CommsLayerMessage msg)
        {
            ReactionOutputMessage output = null;

            switch (reactionType)
            {
            case ReactionType.Text:
                output = new ReactionOutputMessage(this);
                break;

            case ReactionType.DictionaryVariable:
                ParseDictionaryReaction(msg);
                break;

            case ReactionType.IO:
                ParseSpecialReaction(msg);
                break;
            }

            return(output);
        }
Exemple #9
0
        private bool ParseTextTrigger(CommsLayerMessage msg)
        {
            if (msg.type != CommsLayerMessage.Type.Text)
            {
                return(false);
            }

            switch (modifier)
            {
            case (int)TextTriggerModifiers.Contains:

                string text  = (string)msg.message;
                string query = (string)data;

                Match match = Regex.Match(text, query);

                return(match.Success);

            default:
                return(false);
            }
        }
Exemple #10
0
        public List <ReactionOutputMessage> React(CommsLayerMessage msg)
        {
            if (subgroups == null)
            {
                AssembleSubgroups();
            }

            var reactionOutputs = new List <ReactionOutputMessage>();

            foreach (var keypair in subgroups)
            {
                var subgroup = keypair.Value;

                var output = subgroup.SelectAndApplyReaction(msg);

                if (output != null)
                {
                    reactionOutputs.Add(output);
                }
            }

            return(reactionOutputs);
        }
Exemple #11
0
        private bool ParseDictionaryTrigger(CommsLayerMessage msg)
        {
            var    dv = JsonConvert.DeserializeObject <VariableDataDBO>(data.ToString());
            string dictionaryName, userId, value;

            switch (modifier)
            {
            case (int)DictionaryVariableTriggerModifiers.MatchMessageToValue:
                if (msg.type != CommsLayerMessage.Type.Text)
                {
                    return(false);
                }

                dictionaryName = dv.variable;
                userId         = msg.senderId.ToString();
                value          = msg.message.ToString();

                return(UserVariablesSingleton.DoesDictionaryVariableMatch(dictionaryName, userId, value));

            case (int)DictionaryVariableTriggerModifiers.MatchPresetToValue:
                dictionaryName = dv.variable;
                userId         = msg.senderId.ToString();
                value          = dv.value;

                return(UserVariablesSingleton.DoesDictionaryVariableMatch(dictionaryName, userId, value));

            case (int)DictionaryVariableTriggerModifiers.KeyExists:
                dictionaryName = dv.variable;
                userId         = msg.senderId.ToString();

                return(UserVariablesSingleton.DoesDictionaryVariableUserExist(dictionaryName, userId));

            default:
                return(false);
            }
        }
Exemple #12
0
 public TriggerOutput Eval(CommsLayerMessage msg)
 {
     return(new TriggerOutput(ParseTriggerInput(msg), onFailMsg));
 }
Exemple #13
0
/* */
        private async void BotOnMessageReceived(object sender, MessageEventArgs args)
        {
            try
            {
                var telegramMessage = args.Message;
                var userId          = telegramMessage.From.Id;
                var chatId          = telegramMessage.Chat.Id;

                object message = null;
                CommsLayerMessage.Type messageType = 0;

                string fileId = "";

                if (telegramMessage == null)
                {
                    return;
                }

                switch (telegramMessage.Type)
                {
                /*case MessageType.ChatMembersAdded:
                 *  {
                 *      if (EAT_JOIN_MSGS)
                 *      {
                 *          await ReactNewMember(message);
                 *      }
                 *      break;
                 *  }*/

                case MessageType.Text:
                    message     = telegramMessage.Text;
                    messageType = CommsLayerMessage.Type.Text;

                    break;

                case MessageType.Photo:
                    var i = telegramMessage.Photo.Length - 1;
                    fileId = telegramMessage.Photo[i].FileId;

                    message = (object)(await Bot.GetFileAsync(fileId));
                    ((Telegram.Bot.Types.File)message).FilePath = $"https://api.telegram.org/file/bot{botToken}/{((Telegram.Bot.Types.File)message).FilePath}";
                    messageType = CommsLayerMessage.Type.Image;

                    break;

                case MessageType.Document:
                    switch (telegramMessage.Document.MimeType)
                    {
                    case "image/png":
                    case "image/jpg":
                    case "image/jpeg":
                        fileId = telegramMessage.Document.FileId;

                        message = (object)await Bot.GetFileAsync(fileId);

                        ((Telegram.Bot.Types.File)message).FilePath = $"https://api.telegram.org/file/bot{botToken}/{((Telegram.Bot.Types.File)message).FilePath}";
                        messageType = CommsLayerMessage.Type.Image;
                        break;
                    }
                    break;
                }

                if (message == null)
                {
                    return;
                }

                bool isPrivateChat = telegramMessage.Chat.Type == ChatType.Private;

                var commsMsg      = new CommsLayerMessage(message, messageType, userId, chatId, isPrivateChat);
                var triggerOutput = behaviourManager.EvaluateTriggers(commsMsg);

                if (triggerOutput.result)
                {
                    Console.WriteLine($"{telegramMessage.From.Username}: {triggerOutput.triggerName}");

                    var reactionOutputMessages = behaviourManager.ApplyReactions(triggerOutput.triggerName, commsMsg);

                    if (reactionOutputMessages != null && reactionOutputMessages.Count > 0)
                    {
                        TypeMessagesAsync(chatId, reactionOutputMessages);
                    }
                }
                else
                {
                    if (triggerOutput.onFailMsg != null)
                    {
                        TypeMessageAsync(chatId, triggerOutput.onFailMsg);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"{args.Message.From.Username}: {e.Message}");
            }
        }
Exemple #14
0
        public ReactionOutputMessage SelectAndApplyReaction(CommsLayerMessage msg)
        {
            var reaction = SelectReaction();

            return(reaction.React(msg));
        }