From 43351c7ba96c7c66802e3d356c31cb71bc8d2cd2 Mon Sep 17 00:00:00 2001 From: techgaun Date: Fri, 16 Dec 2016 12:31:27 -0600 Subject: [PATCH 1/3] do a random insult and add default ua --- README.md | 1 + lib/ex_mustang/responders/isup.ex | 3 +- lib/ex_mustang/responders/random_insults.ex | 35 +++++++++++++++++++++ lib/ex_mustang/utils.ex | 2 ++ mix.exs | 3 +- mix.lock | 2 ++ 6 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 lib/ex_mustang/responders/random_insults.ex diff --git a/README.md b/README.md index f530b73..698bb03 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,7 @@ Currently, there are following responders and these should ideally work with any - `ExMustang.Responders.TimeConvert` - converts given unix timestamp to ISO8601 format - `ExMustang.Responders.EncodeDecode` - encoding/decoding tools - `ExMustang.Responders.Isup` - checks if a site is up or not via isitup.org API +- `ExMustang.Responders.RandomInsult` - insults a user with random insults from randominsults.net For Google Maps search, you have to set `GOOGLE_API_KEY` which has access to call google places api. diff --git a/lib/ex_mustang/responders/isup.ex b/lib/ex_mustang/responders/isup.ex index b6385c0..386dab0 100644 --- a/lib/ex_mustang/responders/isup.ex +++ b/lib/ex_mustang/responders/isup.ex @@ -3,6 +3,7 @@ defmodule ExMustang.Responders.Isup do checks against https://isitup.org/.json if the given site is down or not """ use Hedwig.Responder + import ExMustang.Utils, only: [useragent: 0] @usage """ isitup - checks if given domain is up or not @@ -14,7 +15,7 @@ defmodule ExMustang.Responders.Isup do def isitup(domain) do domain = parse_domain(domain) - case HTTPoison.get("https://isitup.org/#{domain}.json") do + case HTTPoison.get("https://isitup.org/#{domain}.json", [useragent]) do {:ok, %HTTPoison.Response{status_code: 200, body: body}} -> body = Poison.decode!(body) case body["status_code"] do diff --git a/lib/ex_mustang/responders/random_insults.ex b/lib/ex_mustang/responders/random_insults.ex new file mode 100644 index 0000000..7561603 --- /dev/null +++ b/lib/ex_mustang/responders/random_insults.ex @@ -0,0 +1,35 @@ +defmodule ExMustang.Responders.RandomInsult do + @moduledoc """ + Sends random insult to a user + """ + use Hedwig.Responder + import ExMustang.Utils, only: [useragent: 0] + + @base_url "http://www.randominsults.net/" + + @usage """ + insult - insults given username with random insults + """ + + hear ~r/^insult\s?+<@(?\w+)>.*$/i, msg do + emote msg, insult(msg.matches["user"]) + end + + defp insult(user) do + case HTTPoison.get(@base_url, [useragent]) do + {:ok, %HTTPoison.Response{status_code: 200, body: body}} -> + parse_body(body, user) + _ -> + "I can not insult #{user} right now" + end + end + + defp parse_body(body, user) do + case Floki.find(body, "i") do + [{"i", _, [msg]} | _] -> + "<@#{user}> : #{msg}" + _ -> + "I am unable to parse insult data. Please let the author know his shit is broken!" + end + end +end diff --git a/lib/ex_mustang/utils.ex b/lib/ex_mustang/utils.ex index 225520d..e17add6 100644 --- a/lib/ex_mustang/utils.ex +++ b/lib/ex_mustang/utils.ex @@ -36,5 +36,7 @@ defmodule ExMustang.Utils do id end + def useragent, do: {"User-Agent", "ExMustang"} + defp pid, do: :global.whereis_name("mustang") end diff --git a/mix.exs b/mix.exs index 2e66dc0..5285750 100644 --- a/mix.exs +++ b/mix.exs @@ -19,7 +19,7 @@ defmodule ExMustang.Mixfile do # Type "mix help compile.app" for more information def application do [applications: [:logger, :hedwig_slack, :quantum, :tentacat, :ex_google, :timex, - :ex_pwned], + :ex_pwned, :floki], mod: {ExMustang, []}] end @@ -40,6 +40,7 @@ defmodule ExMustang.Mixfile do {:timex, "~> 3.1"}, {:ex_google, "~> 0.1"}, {:ex_pwned, "~> 0.1"}, + {:floki, "~> 0.11.0"}, {:ex_doc, "~> 0.14.5", only: [:dev]} ] end diff --git a/mix.lock b/mix.lock index 7a6e0c0..c955250 100644 --- a/mix.lock +++ b/mix.lock @@ -7,6 +7,7 @@ "ex_google": {:hex, :ex_google, "0.1.2", "2e6053ad002a0eae1c299977fa3f8d16a47504f36a9cac6f61667204b2abdc82", [:mix], [{:httpoison, "~> 0.9.0", [hex: :httpoison, optional: false]}]}, "ex_pwned": {:hex, :ex_pwned, "0.1.0", "35bf58986d76e18ffddf267e9727958e1f18948fb5f9b7713fbb64636402a952", [:mix], [{:httpoison, "~> 0.9 or ~> 0.10", [hex: :httpoison, optional: false]}, {:poison, "~> 2.0 or ~> 3.0", [hex: :poison, optional: false]}]}, "exjsx": {:hex, :exjsx, "3.2.1", "1bc5bf1e4fd249104178f0885030bcd75a4526f4d2a1e976f4b428d347614f0f", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, optional: false]}]}, + "floki": {:hex, :floki, "0.11.0", "b4532ab64d67225f13f5626e4ba1b8cf3ee9d5bd48017075bb975e1522efd32d", [:mix], [{:mochiweb, "~> 2.15", [hex: :mochiweb, optional: false]}]}, "gettext": {:hex, :gettext, "0.12.2", "4f71a0df7b0bf5693419da593b4ddde558cc28e8f56a991fbfc2b51be1d5c848", [:mix], []}, "gproc": {:hex, :gproc, "0.5.0", "2df2d886f8f8a7b81a4b04aa17972b5965bbc5bf0100ea6d8e8ac6a0e7389afe", [:rebar], []}, "gun": {:hex, :gun, "1.0.0-pre.1", "28514327a7572234633e127c1c14c5d91991bc26ec16d3707f05962a31f0c1b5", [:rebar, :make], [{:cowlib, "1.3.0", [hex: :cowlib, optional: false]}, {:ranch, "1.1.0", [hex: :ranch, optional: false]}]}, @@ -18,6 +19,7 @@ "jsx": {:hex, :jsx, "2.8.0", "749bec6d205c694ae1786d62cea6cc45a390437e24835fd16d12d74f07097727", [:mix, :rebar], []}, "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [], []}, "mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [], []}, + "mochiweb": {:hex, :mochiweb, "2.15.0", "e1daac474df07651e5d17cc1e642c4069c7850dc4508d3db7263a0651330aacc", [:rebar3], []}, "poison": {:hex, :poison, "3.0.0", "625ebd64d33ae2e65201c2c14d6c85c27cc8b68f2d0dd37828fde9c6920dd131", [:mix], []}, "quantum": {:hex, :quantum, "1.8.1", "37c9ad0307cf47bd578507ce1ddda98746199b281e8afe91cbe44c21d56af983", [:mix], [{:calendar, "~> 0.16", [hex: :calendar, optional: false]}]}, "ranch": {:hex, :ranch, "1.1.0", "f7ed6d97db8c2a27cca85cacbd543558001fc5a355e93a7bff1e9a9065a8545b", [:make], []}, From 2f87bb2d175c6ab199557e00d3c2bc70da8b6d3c Mon Sep 17 00:00:00 2001 From: techgaun Date: Fri, 16 Dec 2016 12:33:10 -0600 Subject: [PATCH 2/3] update config --- config/config.exs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/config.exs b/config/config.exs index aa7ad31..5b8c4d1 100644 --- a/config/config.exs +++ b/config/config.exs @@ -55,7 +55,8 @@ config :ex_mustang, ExMustang.Robot, {ExMustang.Responders.Time, []}, {ExMustang.Responders.TimeConvert, []}, {ExMustang.Responders.EncodeDecode, []}, - {ExMustang.Responders.Isup, []} + {ExMustang.Responders.Isup, []}, + {ExMustang.Responders.RandomInsult, []} ] config :quantum, timezone: System.get_env("SYSTEM_TIME") || "America/Chicago" From 8d5a2e3cabcd987be6128191d228cd9c95bd8ab1 Mon Sep 17 00:00:00 2001 From: techgaun Date: Fri, 16 Dec 2016 12:35:47 -0600 Subject: [PATCH 3/3] update readme and add insult me --- README.md | 2 ++ lib/ex_mustang/responders/random_insults.ex | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 698bb03..6ad3269 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,8 @@ time - get time in a given timezone unix2iso - Converts given unix timestamp to ISO format (Auto-replies for values that look like timestamps) b64encode - base64 encoding of given text content b64decode - base64 decoding of given text content +isitup - checks if given domain is up or not +insult me| - insults given username with random insults ``` ### About Mustang diff --git a/lib/ex_mustang/responders/random_insults.ex b/lib/ex_mustang/responders/random_insults.ex index 7561603..72ac276 100644 --- a/lib/ex_mustang/responders/random_insults.ex +++ b/lib/ex_mustang/responders/random_insults.ex @@ -8,9 +8,13 @@ defmodule ExMustang.Responders.RandomInsult do @base_url "http://www.randominsults.net/" @usage """ - insult - insults given username with random insults + insult me| - insults given username with random insults """ + hear ~r/^insult\s*me/i, msg do + emote msg, insult(msg.user.id) + end + hear ~r/^insult\s?+<@(?\w+)>.*$/i, msg do emote msg, insult(msg.matches["user"]) end