예제 #1
0
        public void Setup(IRobot robot)
        {
            var settings = robot.Settings.SettingsFor<ImageAdapter>();

            // for now there's no point in registering this listener if there's no api key
            if (string.IsNullOrWhiteSpace((string) settings.Settings.ApiKey))
            {
                Console.Error.WriteLine("No API key was found for bing image search. If you do not have one you can get one by signing up here http://www.bing.com/developers/");
                return;
            }

            SafeSearch safeSearch;
            if (!Enum.TryParse((string) settings.Settings.SafeSearch, true, out safeSearch))
            {
                safeSearch = SafeSearch.Strict;

                // this will become the default value
                settings.Settings.SafeSearch = safeSearch.ToString();
            }

            robot.AddListener(@"^(bing )?image( me)? (?<query>.*)", (session, message, room, match) =>
            {
                var query = match.ValueFor("query");

                var searchResult = _bingClient.ImageSearch((string) settings.Settings.ApiKey, query, safeSearch);

                // ToDo: unhack this
                if (searchResult.StartsWith("{", StringComparison.OrdinalIgnoreCase))
                {
                    var results = JObject.Parse(searchResult);
                    var urls = results["d"]["results"].SelectList(x => (string) x["MediaUrl"]);

                    string messageResult;
                    if (urls.Count > 0)
                    {
                        messageResult = urls.RandomElement(new Random());
                    }
                    else
                    {
                        messageResult = string.Format("@{0} I couldn't find any images for '{1}'", session.Message.User.Name, query);
                    }

                    robot.SendMessage(room, messageResult);
                }
            });
        }
예제 #2
0
        public void Setup(IRobot robot)
        {
            robot.AddPrivateResponder(@"help\s*(?<command>.*)?$", (session, message, from, match) =>
            {
                var command = match.ValueFor("command");
                if (string.IsNullOrWhiteSpace(command))
                {
                    robot.SendPrivateMessage(@from, "Help\n\n{0}", robot.HelpText());
                    return;
                }

                var helpMessage = "Help for {0}\n\n{1}";
                var helpText = robot.HelpTextFor(command);
                if (string.IsNullOrWhiteSpace(helpText))
                {
                    helpMessage = "No help for '{0}'";
                }

                robot.SendPrivateMessage(@from, helpMessage, command, helpText);
            });

            robot.AddListener(@"jibbe?r jabbe?r", (session, message, room, match) =>
            {
                robot.SendMessage(room, "@{0} I got no time for the jibber-jabber!", session.Message.User.Name);
            });

            robot.AddPrivateResponder(@"note (?<note>.*)$", (session, message, from, match) =>
            {
                // ToDo: add permission check
                var note = match.ValueFor("note");

                robot.SetNote(note);
            });

            robot.AddPrivateResponder(@"flag (?<country>.*)$", (session, message, from, match) =>
            {
                // ToDo: add permission check
                var countryCode = match.ValueFor("country");

                robot.SetFlag(countryCode);
            });

            robot.AddPrivateResponder(@"where (?:are you|is (?<who>.*))\b\?*", (session, message, from, match) =>
            {
                var who = match.ValueFor("who");

                var toSay = "{0} is in #{1}";

                if (string.IsNullOrWhiteSpace(who))
                {
                    who = session.BotName;
                    toSay = "I'm in #{1}";
                }

                var where = string.Empty;

                List<string> rooms;
                if (robot.Settings.KnownUsers.TryGetValue(who, out rooms))
                {
                    where = string.Join(", #", rooms);
                }
                else
                {
                    toSay = "I can't find {0}";
                }

                robot.SendPrivateMessage(from, toSay, who, where);
            });
        }