Пример #1
0
        public async Task SaveBevan(Bevan bevan)
        {
            using (var client = new AmazonDynamoDBClient())
            {
                var table = Table.LoadTable(client, "hey-bevan-table-new-dev");

                var book = new Document
                {
                    ["bevanId"]    = bevan.BevanId,
                    ["receiverId"] = bevan.ReceiverId,
                    ["count"]      = bevan.Count,
                    ["message"]    = bevan.Message,
                    ["giverId"]    = bevan.GiverId,
                    ["channel"]    = bevan.Channel,
                    ["timestamp"]  = bevan.Timestamp
                };

                await table.PutItemAsync(book);

                Console.WriteLine("Bevan saved");
                await SaveChannel(bevan.Channel);
                await SaveUser(bevan.ReceiverId, bevan.GiverId);
            }
        }
Пример #2
0
        internal async Task <Bevan> ProcessMessage(Event @event)
        {
            var theMessage = @event.Text;
            var bevan      = new Bevan();

            Console.WriteLine(" theMessage ", theMessage + " ~~~~~~~~~~ " + @event.Text);
            if (string.IsNullOrEmpty(theMessage))
            {
                Console.WriteLine("Message is empty , exiting");
                return(bevan);
            }
            //TODO allow multiple users
            var regexValue = Regex.Match(theMessage, @"<@(.+?)>");

            Console.WriteLine("regexValue ", regexValue);
            var whoReceived = regexValue.Groups[1].Value;

            Console.WriteLine(" whoReceived ", whoReceived);

            theMessage = theMessage.Replace("<@" + whoReceived + ">", "");
            Console.WriteLine("Event message received from slack " + theMessage + " " + @event.Channel + " " + @event.User);

            if (theMessage.Contains(emoji) && !string.IsNullOrEmpty(whoReceived))
            {
                string whoSent = @event.User; //who posted the message

                var sentToday = await getNoSentToday(whoSent);

                var noOfEmojis = theMessage.Split(emoji).Length - 1;

                //Sorry, you can only give tacos to other people on your team.
                if (whoReceived == whoSent)
                {
                    var selfMessage = string.Format("Sorry, you can only give {0}'s to other people on your team.", emoji);
                    await sendDM(whoSent, selfMessage);

                    return(bevan);
                }
                Console.WriteLine("No of emojis received " + noOfEmojis.ToString());
                Console.WriteLine("Total emojis sent today " + sentToday.ToString());
                Console.WriteLine("Daily limit " + dailyLimit.ToString());

                //check how many they've sent today
                //5 >= 5 || (5 - 1) >= 5)
                if (sentToday >= dailyLimit || (sentToday + noOfEmojis) > dailyLimit)
                {
                    //send daily limit message
                    Console.WriteLine("{0} >= {1} || ({0} - {2}) >= {1}) ", sentToday, dailyLimit, noOfEmojis);

                    var dailyLimitMessage = string.Format("Whoops! You tried to give {0} {2}'s. You have {1} {2}'s left to give today.", noOfEmojis, (dailyLimit - sentToday), emoji);
                    await sendDM(whoSent, dailyLimitMessage);

                    return(bevan);
                }
                Console.WriteLine("{0} gave \"{1}\" emojis to {2}", whoSent, noOfEmojis, whoReceived);

                // do dynamo db inserts
                bevan = new Bevan
                {
                    BevanId    = Guid.NewGuid().ToString(),
                    ReceiverId = whoReceived,
                    Count      = noOfEmojis,
                    Message    = theMessage,
                    Channel    = @event.Channel,
                    GiverId    = whoSent,
                    Timestamp  = DateTime.UtcNow
                };

                await _dynamoRepository.SaveBevan(bevan);

                //You received 1 taco from @ivan in #cr-hyperion.
                //>@bevan :taco: for the awesome pitch!
                var receiverDM = string.Format("You received {0} {1}'s from <@{2}>.\n>{3}", noOfEmojis, emoji, whoSent, theMessage);
                await sendDM(whoReceived, receiverDM);

                Console.WriteLine(receiverDM);

                //@jp received 3 tacos from you. You have 2 tacos left to give out today.
                var giverDM = string.Format("<@{0}> received {1} {2}'s from you. You have {3} {2}'s left to give out today.", whoReceived, noOfEmojis, emoji, dailyLimit - (noOfEmojis + sentToday));
                await sendDM(whoSent, giverDM);

                Console.WriteLine(giverDM);
            }

            //do nothing
            return(bevan);
        }