Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix twitter (x) posts for v2 api #827

Merged
merged 8 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
elixir 1.14.1-otp-25
erlang 25.1.2
elixir 1.17.2-otp-27
erlang 27.0.1
nodejs 19.0.0
39 changes: 37 additions & 2 deletions lib/tilex/notifications/notifiers/twitter.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
defmodule Tilex.Notifications.Notifiers.Twitter do
alias OAuther
alias Tilex.Blog.Developer

use Tilex.Notifications.Notifier
require Logger
avogel3 marked this conversation as resolved.
Show resolved Hide resolved

def handle_post_created(post, developer, channel, url) do
"#{post.title} #{url} via @#{Developer.twitter_handle(developer)} #til ##{channel.twitter_hashtag}"
Expand All @@ -16,7 +18,40 @@ defmodule Tilex.Notifications.Notifiers.Twitter do
:ok
end

def send_tweet(text) do
ExTwitter.update(text)
def send_tweet(message) do
url = "https://api.x.com/2/tweets"
jackrosa183 marked this conversation as resolved.
Show resolved Hide resolved

params = %{
"text" => message
}

headers =
oauth_headers("post", url) ++
[{"Content-Type", "application/json"}]
avogel3 marked this conversation as resolved.
Show resolved Hide resolved

Req.post!(url, headers: headers, json: params)
end

defp oauth_headers(method, url) do
{auth_header, _params} =
OAuther.sign(method, url, [], oauth_creds())
|> OAuther.header()

[auth_header]
end

def oauth_creds do
consumer_key = System.get_env("twitter_consumer_key")
consumer_secret = System.get_env("twitter_consumer_secret")
access_token = System.get_env("twitter_access_token")
access_token_secret = System.get_env("twitter_access_token_secret")

OAuther.credentials(
method: :hmac_sha1,
consumer_key: consumer_key,
consumer_secret: consumer_secret,
token: access_token,
token_secret: access_token_secret
)
end
end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ defmodule Tilex.Mixfile do
{:plug_cowboy, "~> 2.5"},
{:postgrex, ">= 0.0.0"},
{:swoosh, "~> 1.3"},
{:req, "~> 0.5.6"},
{:telemetry_metrics, "~> 0.6"},
{:telemetry_poller, "~> 1.0"},
{:timex, "~> 3.1"},
Expand Down
Loading
Loading