Esempio n. 1
0
        public IChatMessage GetResponse(long interactionId, IChatMessage message)
        {
            using (_trace.scope())
            {
                try
                {
                    Console.WriteLine("[{0}] - GetResponse ({1})", BotName, interactionId);

                    var textMessage = message as TextChatMessage;
                    if (textMessage == null)
                    {
                        return(null);
                    }

                    var page = "http://www.google.com/search?q=" + HttpUtility.UrlEncode(textMessage.Text) + "&btnI=1";
                    return(new UrlChatMessage {
                        Uri = new Uri(GetResponseUrl(page))
                    });
                }
                catch (Exception ex)
                {
                    _trace.exception(ex);
                }
                return(null);
            }
        }
Esempio n. 2
0
        public IChatMessage GetResponse(long interactionId, IChatMessage message)
        {
            using (_trace.scope())
            {
                try
                {
                    Console.WriteLine("[{0}] - GetResponse ({1})", BotName, interactionId);

                    var textMessage = message as TextChatMessage;
                    if (textMessage == null)
                    {
                        return(null);
                    }

                    // Set language
                    if (textMessage.Text.StartsWith("/setlang", StringComparison.InvariantCultureIgnoreCase))
                    {
                        var parts = textMessage.Text.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        if (parts.Length == 2)
                        {
                            // This would be really cool if the google translate api was implemented here. But it costs money. Going to have to settle for pig latin for now... https://cloud.google.com/translate/
                            if (parts[1].Equals("piglatin", StringComparison.InvariantCultureIgnoreCase))
                            {
                                _language = Languages.PigLatin;
                                return(new TextChatMessage
                                {
                                    Text = "Language set to Pig Latin. Say something to have it translated."
                                });
                            }
                            if (parts[1].Equals("binary", StringComparison.InvariantCultureIgnoreCase))
                            {
                                _language = Languages.Binary;
                                return(new TextChatMessage
                                {
                                    Text = "Language set to Binary. Say something to have it translated."
                                });
                            }
                            else
                            {
                                return(new TextChatMessage
                                {
                                    Text = "Sorry, I can't translate to " + parts[1]
                                });
                            }
                        }
                    }

                    // Translate
                    switch (_language)
                    {
                    case Languages.PigLatin:
                        return(new TextChatMessage {
                            Text = ToPigLatin(textMessage.Text)
                        });

                        break;

                    case Languages.Binary:
                        return(new TextChatMessage {
                            Text = ToBinaryString(textMessage.Text)
                        });

                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
                catch (Exception ex)
                {
                    _trace.exception(ex);
                    return(new TextChatMessage {
                        Text = "A failure was encountered while translating the message"
                    });
                }
            }
        }
Esempio n. 3
0
        public IChatMessage GetResponse(long interactionId, IChatMessage message)
        {
            using (_trace.scope())
            {
                try
                {
                    /* FAIR WARNING
                     * This bot is a fairly bad idea to actually expose to users. It's meant as a testing
                     * bot to test the get/set attribute functionality. Set system attributes at your own risk.
                     */
                    Console.WriteLine("[{0}] - GetResponse ({1})", BotName, interactionId);

                    var textMessage = message as TextChatMessage;
                    if (textMessage == null)
                    {
                        return(null);
                    }

                    var pieces = textMessage.Text.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                    if (pieces.Length == 0)
                    {
                        return(null);
                    }

                    // Check for set
                    if (pieces.Length == 1)
                    {
                        var setData = pieces[0].Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                        if (setData.Length == 2)
                        {
                            _attributeService.SetAttribute(interactionId, setData[0], setData[1]);
                            return(new TextChatMessage {
                                Text = "Attribute set!"
                            });
                        }

                        // Get attribute
                        var attr = _attributeService.GetAttribute(interactionId, pieces[0]);
                        return(new TextChatMessage {
                            Text = pieces[0] + "=" + attr
                        });
                    }

                    // Get attributes
                    var attrs = _attributeService.GetAttributes(interactionId, pieces);
                    return(new TextChatMessage
                    {
                        Text =
                            attrs.Select(a => a.Key + "=" + a.Value)
                            .Aggregate((a, b) => a + Environment.NewLine + b)
                    });
                }
                catch (Exception ex)
                {
                    _trace.exception(ex);
                    return(new TextChatMessage {
                        Text = "I'm sorry. I failed. Will you give me another chance?"
                    });
                }
            }
        }