Skip to content

Instantly share code, notes, and snippets.

@allefgomes
Created May 23, 2020 01:59
Show Gist options
  • Save allefgomes/f1335e64827b0eb3870eedcb19ec507a to your computer and use it in GitHub Desktop.
Save allefgomes/f1335e64827b0eb3870eedcb19ec507a to your computer and use it in GitHub Desktop.
Twitter Client in elixir
defmodule TwitterClient do
use Tesla
@token System.get_env("TWITTER_TOKEN")
plug(Tesla.Middleware.BaseUrl, "https://api.twitter.com")
plug(Tesla.Middleware.DecodeJson)
def get_twittes do
{:ok, response} = make_request()
twittes = response.body["statuses"]
twittes
|> create_presentations()
end
defp make_request do
get("/1.1/search/tweets.json",
query: [q: "elixir", count: 10],
headers: [
{"Authorization", "Bearer #{@token}"},
{"Accept", "Application/json; Charset=utf-8"}
]
)
end
defp create_presentations(twittes) do
Enum.map(twittes, fn twitte -> print_twitte(twitte) end)
%{message: "Thank you for watching!"}
end
defp print_twitte(twitte) do
IO.puts("-- Twitted by #{twitte["user"]["name"]} at #{twitte["created_at"]} -------------- \n
#{twitte["text"]}
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><> \n\n")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment