Created
May 23, 2020 01:59
-
-
Save allefgomes/f1335e64827b0eb3870eedcb19ec507a to your computer and use it in GitHub Desktop.
Twitter Client in elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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