public BotMessage GetResponse(MargieBot.Models.ResponseContext context)
        {
            BotMessage message = new BotMessage();

            string messageText = "";
            if (m_Bot.Players != null)
            {
                for (int i = 0; i < m_Bot.Players.Count; ++i)
                {
                    messageText += m_Bot.Players[i].Name + " -- Score: " + m_Bot.Players[i].Score + "\n";
                }
                if (m_Bot.Players.Count > ATTACHMENT_THRESHOLD)
                {
                    SlackAttachment playerAttachment = new SlackAttachment();

                    playerAttachment.Fallback = messageText;
                    playerAttachment.PreText = "See who's playing.";
                    playerAttachment.Text = messageText;
                    playerAttachment.Title = "Players in " + m_Bot.Status.Name;
                    message.Attachments.Add(playerAttachment);
                }
                else
                    message.Text = ">Players for " + m_Bot.Status.Name + "\n" + messageText;
            }
            else
                message.Text = "Server players unavailable. Connection settings may be incorrect or server may be down.";
            return message;
        }
        public BotMessage GetResponse(ResponseContext context)
        {
            BotMessage message = new BotMessage();
            if (context.Message.Text == m_RegisterMessage)
            {
                //register
                try
                {
                    m_Bot.AddRegisteredUser(context.Message.User);
                    message.Text = "You've been added.";
                }
                catch(DuplicateUserException ex)
                {
                    message.Text = "You're already registered!";
                }

            }
            else if (context.Message.Text == m_UnregisterMessage)
            {
                //unregister
                try
                {
                    m_Bot.RemoveRegisteredUser(context.Message.User);
                    message.Text = "You've been removed";
                }
                catch(UserNotPresentException ex)
                {
                    message.Text = "Name not found. Do not need to unregister.";
                }
            }
            return message;
        }
        public BotMessage GetResponse(ResponseContext context)
        {
            BotMessage message = new BotMessage();

            if (m_Bot.Status != null)
            {
                string messageText = "Status for " + m_Bot.Status.Name + "\n" +
                    ">Map: " + m_Bot.Status.MapName + "\n" +
                    ">Mission: " + m_Bot.Status.GameMode + "\n" +
                    ">Players: " + m_Bot.Status.CurrentPlayers + " of " + m_Bot.Status.MaximumPlayers;

                message.Text = messageText;
            }
            else
                message.Text = "Server status unavailable. Connection settings may be incorrect or server may be down.";

            return message;
        }
Exemple #4
0
        private async Task Say(BotMessage message, ResponseContext context)
        {
            string chatHubID = null;
            if (message == null)
            {
                return;
            }

            if(message.ChatHub != null) {
                chatHubID = message.ChatHub.ID;
            }
            else if(context != null && context.Message.ChatHub != null) {
                chatHubID = context.Message.ChatHub.ID;
            }

            if(chatHubID != null) {
                NoobWebClient client = new NoobWebClient();

                List<string> values = new List<string>() {
                    "token", this.SlackKey,
                    "channel", chatHubID,
                    "text", message.Text,
                    "as_user", "true"
                };

                if (message.Attachments.Count > 0) {
                    values.Add("attachments");
                    values.Add(JsonConvert.SerializeObject(message.Attachments));
                }

                await client.DownloadString(
                    "https://slack.com/api/chat.postMessage",
                    RequestMethod.Post,
                    values.ToArray()
                );
            }
            else {
                throw new ArgumentException("When calling the Say() method, the message parameter must have its ChatHub property set.");
            }
        }
Exemple #5
0
 public async Task Say(BotMessage message)
 {
     await Say(message, null);
 }
 public override BotMessage GetResponse(ResponseContext context)
 {
     var msg = Message;
     Message = null;
     return msg;
 }
 public void SendMessage(string channel, string message, bool isGroup = false)
 {
     BotMessage botMessage = new BotMessage();
     botMessage.ChatHub = new SlackChatHub();
     botMessage.ChatHub.ID = channel;
     botMessage.ChatHub.Name = channel;
     botMessage.ChatHub.Type = isGroup ? SlackChatHubType.Group : SlackChatHubType.Channel;
     m_Bot.Say(botMessage);
 }
        public void SendDirectMessage(SlackUser user, string message)
        {
            BotMessage botMessage = new BotMessage();
            botMessage.ChatHub = new SlackChatHub();
            botMessage.ChatHub.Type = SlackChatHubType.DM;
            botMessage.ChatHub.ID = user.ID;
            botMessage.ChatHub.Name = user.ID;
            botMessage.Text = message;

            m_Bot.Say(botMessage);
        }
Exemple #9
0
        public BotMessage GetResponse(ResponseContext context)
        {
            string request = context.Message.Text.Length > quoteStart.Length ? context.Message.Text.Substring(quoteStart.Length+1) : "";
            Debug.WriteLine("request: '" + request + "'");
            BotMessage reply = new BotMessage() { ChatHub = context.Message.ChatHub };

            string command = request.Contains(" ") ? request.Substring(0, request.IndexOf(" ")) : request;
            Debug.WriteLine("command: '" + command + "'");

            switch (command)
            {
                case quoteAdd:
                    {
                        string quoteText = request.Substring(request.IndexOf(" ") + 1);

                        if (quoteText.Length > 0)
                        {
                            Quote quote = new Quote
                            {
                                channel = context.Message.ChatHub.Name,
                                user = context.UserNameCache[context.Message.User.ID],
                                timestamp = context.Message.TimeStamp,
                                text = quoteText
                            };

                            if (addQuoteToDb(ref quote))
                            {
                                reply.Text = "Success! Added " + quote.ToString(quoteTimeZone);
                            }
                            else
                            {
                                reply.Text = "Failed to add " + quote.ToString(quoteTimeZone);
                            }
                        }
                        else
                        {
                            reply.Text = "Nope! Not adding empty quote";
                        }

                        break;
                    }

                case quoteDel:
                    {
                        long id = GetRequestQuoteId(request, reply);

                        if (id != -1)
                        {
                            Quote deletedQuote = delQuoteFromDb(id);
                            if (deletedQuote != null)
                            {
                                reply.Text = "Succesfully deleted " + deletedQuote.ToString(quoteTimeZone);
                            }
                            else
                            {
                                reply.Text = "Could not delete quote #" + id;
                            }
                        }

                        break;
                    }

                case quoteGet:
                    {
                        long id = GetRequestQuoteId(request, reply);
                        Quote quote = getQuoteById(id);
                        if (quote != null)
                        {
                            reply.Text = quote.ToString(quoteTimeZone);
                        }
                        else
                        {
                            reply.Text = "Could not find quote #" + id;
                        }
                        break;
                    }

                case quoteRandom:
                    {
                        Quote quote = getRandomQuote();
                        if (quote != null)
                        {
                            reply.Text = quote.ToString(quoteTimeZone);
                        }
                        else
                        {
                            reply.Text = "Could not get random quote";
                        }
                        break;
                    }

                default:
                    Debug.WriteLine("Unsupported quote command '" + command + "' (request '" + request + "')");
                    reply.Text = "Unsupported quote command '" + command + "' (request '" + request + "')";
                    break;
            }

            return reply;
        }
Exemple #10
0
        private long GetRequestQuoteId(string request, BotMessage reply)
        {
            string idStr = request.Substring(request.IndexOf(" ") + 1);
            long id = -1;

            try
            {
                id = Convert.ToInt64(idStr);
            }
            catch (FormatException)
            {
                reply.Text = "Badly formatted id: '" + idStr + "'";
                return -1;
            }

            return id;
        }
Exemple #11
0
        private static string GetChatHubId(BotMessage message, ResponseContext context)
        {
            string chatHubID = null;
            if (message == null)
            {
                return chatHubID;
            }

            if (message.ChatHub != null)
            {
                chatHubID = message.ChatHub.ID;
            }
            else if (context != null && context.Message.ChatHub != null)
            {
                chatHubID = context.Message.ChatHub.ID;
            }
            return chatHubID;
        }
Exemple #12
0
        public async Task React(BotMessage message, ResponseContext context)
        {
            if (message == null)
            {
                return;
            }

            if (context == null || context.Message == null || context.Message.Timestamp.IsNullOrEmpty())
            {
                throw new ArgumentException("When calling the React() method, the context must have a Message and Timestamp.");
            }

            var chatHubID = GetChatHubId(message, context);
            if (chatHubID != null)
            {
                NoobWebClient client = new NoobWebClient();

                List<string> values = new List<string>() {
                    "token", this.SlackKey,
                    "channel", chatHubID,
                    "timestamp", context.Message.Timestamp,
                    "name", message.Text
                };

                await client.GetResponse(
                    "https://slack.com/api/reactions.add",
                    RequestMethod.Post,
                    values.ToArray()
                );
            }
            else
            {
                throw new ArgumentException("When calling the React() method, the message parameter must have its ChatHub property set.");
            }
        }