Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

add random insults #35

Merged
merged 3 commits into from
Dec 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -119,6 +120,8 @@ time - get time in a given timezone
unix2iso <unix_timestamp> - Converts given unix timestamp to ISO format (Auto-replies for values that look like timestamps)
b64encode <content> - base64 encoding of given text content
b64decode <content> - base64 decoding of given text content
isitup <domain> - checks if given domain is up or not
insult me|<username> - insults given username with random insults
```

### About Mustang
Expand Down
3 changes: 2 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion lib/ex_mustang/responders/isup.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule ExMustang.Responders.Isup do
checks against https://isitup.org/<domain>.json if the given site is down or not
"""
use Hedwig.Responder
import ExMustang.Utils, only: [useragent: 0]

@usage """
isitup <domain> - checks if given domain is up or not
Expand All @@ -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
Expand Down
39 changes: 39 additions & 0 deletions lib/ex_mustang/responders/random_insults.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 me|<username> - 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?+<@(?<user>\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
2 changes: 2 additions & 0 deletions lib/ex_mustang/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@ defmodule ExMustang.Utils do
id
end

def useragent, do: {"User-Agent", "ExMustang"}

defp pid, do: :global.whereis_name("mustang")
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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]}]},
Expand All @@ -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], []},
Expand Down