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

Send notification new post to Twitter #1126

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 4 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ jobs:
secret_PATREON_CLIENT_SECRET: ${{ secrets.PATREON_CLIENT_SECRET }}
secret_JWT_PRIVATE_KEY: ${{ secrets.JWT_PRIVATE_KEY }}
secret_WEBHOOK_SECRETS: ${{ secrets.WEBHOOK_SECRETS }}
secret_TWITTER_CONSUMER_KEY: ${{ secrets.TWITTER_CONSUMER_KEY }}
secret_TWITTER_SECRET_KEY: ${{ secrets.TWITTER_SECRET_KEY }}
secret_TWITTER_ACCESS_TOKEN : ${{ secrets.TWITTER_ACCESS_TOKEN }}
secret_TWITTER_ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }}
- run: echo "GITHUB_SHA=$GITHUB_SHA" >> .env
- run: echo "${{ secrets.PRODUCTION_SSH_KEY }}" > ${{ env.SSH_KEY_PATH }} && chmod 600 ${{ env.SSH_KEY_PATH }}
- run: scp -o StrictHostKeyChecking=no -i ${{ env.SSH_KEY_PATH }} .env ${{ secrets.PRODUCTION_SSH_USERNAME }}@${{ secrets.PRODUCTION_SSH_HOST }}:/home/vas3k/vas3k.club/.env
Expand Down
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ icalendar = "*"
httptools = "*"
croniter = "*"
authlib = "==1.2"
tweepy = "==4.14.0"

[requires]
python_version = "3.8"
2 changes: 2 additions & 0 deletions bot/handlers/moderation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from notifications.telegram.posts import notify_post_approved, announce_in_club_chats, \
notify_post_rejected, notify_post_collectible_tag_owners
from notifications.telegram.users import notify_user_profile_approved, notify_user_profile_rejected
from notifications.twitter.posts import send_to_twitter
from posts.models.post import Post
from posts.models.subscriptions import PostSubscription
from search.models import SearchIndex
Expand Down Expand Up @@ -51,6 +52,7 @@ def approve_post(update: Update, context: CallbackContext) -> None:
# send notifications
notify_post_approved(post)
announce_in_club_chats(post)
send_to_twitter(post)
if post.collectible_tag_code:
notify_post_collectible_tag_owners(post)

Expand Down
6 changes: 6 additions & 0 deletions club/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ PATREON_CLIENT_SECRET=""

JWT_PRIVATE_KEY=""
WEBHOOK_SECRETS=""

TWITTER_CONSUMER_KEY=""
TWITTER_SECRET_KEY=""
TWITTER_ACCESS_TOKEN=""
TWITTER_ACCESS_TOKEN_SECRET=""

5 changes: 5 additions & 0 deletions club/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@
TELEGRAM_BOT_WEBHOOK_HOST = "0.0.0.0"
TELEGRAM_BOT_WEBHOOK_PORT = 8816

TWITTER_CONSUMER_KEY=os.getenv("TWITTER_CONSUMER_KEY")
TWITTER_SECRET_KEY=os.getenv("TWITTER_SECRET_KEY")
TWITTER_ACCESS_TOKEN=os.getenv("TWITTER_ACCESS_TOKEN")
TWITTER_ACCESS_TOKEN_SECRET=os.getenv("TWITTER_ACCESS_TOKEN_SECRET")

STRIPE_API_KEY = os.getenv("STRIPE_API_KEY") or ""
STRIPE_PUBLIC_KEY = os.getenv("STRIPE_PUBLIC_KEY") or ""
STRIPE_WEBHOOK_SECRET = os.getenv("STRIPE_WEBHOOK_SECRET") or ""
Expand Down
Empty file.
29 changes: 29 additions & 0 deletions notifications/twitter/posts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import logging

import tweepy

from club import settings
from posts.models.post import Post

log = logging.getLogger()

auth = tweepy.OAuth1UserHandler(
consumer_key=settings.TWITTER_CONSUMER_KEY,
consumer_secret=settings.TWITTER_SECRET_KEY,
access_token=settings.TWITTER_ACCESS_TOKEN,
access_token_secret=settings.TWITTER_ACCESS_TOKEN_SECRET
)
twitter = tweepy.API(auth)


def send_to_twitter(post: Post):
if post.type == Post.TYPE_INTRO:
log.info(f"Twitter skipping post with type: {post.type}")
return

url = settings.APP_HOST + post.get_absolute_url()
title = post.title
text = f"{title} {url}"

log.info(f"Twitter sending tweet with text: {text}")
twitter.update_status(status=text)
Loading