-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added feature to comment on last tweet; Added redis to store last twe…
…et data for persistency; Improved logging; Code cleanup and restructuring Signed-off-by: avaakash <[email protected]>
- Loading branch information
Showing
18 changed files
with
272 additions
and
90 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
env | ||
env.json | ||
__pycache__ | ||
__pycache__*/ | ||
run_logs.txt |
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
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
""" | ||
Connect to redis server | ||
""" | ||
import redis | ||
|
||
from settings import get_env | ||
|
||
def connect(): | ||
"""Connect to redis server""" | ||
environ_secrets = get_env() | ||
host = environ_secrets["redis_host"] | ||
port = environ_secrets["redis_port"] | ||
password = environ_secrets["redis_password"] | ||
if password: | ||
return redis.Redis(host=host, port=port, password=password) | ||
return redis.Redis(host=host, port=port) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
""" | ||
Database actions | ||
""" | ||
|
||
from .connect import connect | ||
|
||
redis_connect = connect() | ||
|
||
def set_tweet(tweet_id, tweet_text): | ||
"""Set a tweet in redis""" | ||
redis_connect.set("tweet_id", tweet_id) | ||
redis_connect.set("tweet_text", tweet_text) | ||
|
||
def get_tweet(): | ||
"""Get a tweet from redis""" | ||
return (redis_connect.get("tweet_id").decode('UTF-8'), | ||
redis_connect.get("tweet_text").decode('UTF-8')) | ||
|
||
def delete_tweet(): | ||
"""Delete a tweet from redis""" | ||
redis_connect.delete("tweet_id", "tweet_text") | ||
|
||
def set_tweet_list(tweet_id): | ||
"""Set a list of tweets in redis""" | ||
redis_connect.lpush("tweet_list", tweet_id) | ||
|
||
def get_latest_tweet_list(): | ||
"""Get the latest tweet from redis""" | ||
return redis_connect.lrange("tweet_list", 0, -1) | ||
|
||
def remove_last_tweet(): | ||
"""Remove the latest tweet from redis""" | ||
redis_connect.lpop("tweet_list") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: redis-config | ||
data: | ||
redis-config: | | ||
maxmemory 2mb | ||
maxmemory-policy allkeys-lru |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: redis | ||
labels: | ||
app: redis | ||
spec: | ||
containers: | ||
- name: redis | ||
image: redis:5.0.4 | ||
command: | ||
- redis-server | ||
- "/redis-master/redis.conf" | ||
env: | ||
- name: MASTER | ||
value: "true" | ||
ports: | ||
- containerPort: 6379 | ||
resources: | ||
limits: | ||
cpu: "0.1" | ||
volumeMounts: | ||
- mountPath: /redis-master-data | ||
name: data | ||
- mountPath: /redis-master | ||
name: config | ||
volumes: | ||
- name: data | ||
emptyDir: {} | ||
- name: config | ||
configMap: | ||
name: redis-config | ||
items: | ||
- key: redis-config | ||
path: redis.conf |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
# !/bin/bash | ||
echo $HOME | ||
source "$HOME/tweet-from-message/env/bin/activate" | ||
echo Starting Bot... | ||
python main.py > run_logs.txt & | ||
python main.py > $HOME/tweet-from-message/run_logs.txt & | ||
sleep 3 | ||
echo Bot started. |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
""" | ||
package: telegram_handler | ||
""" | ||
from .basic import * | ||
from .tweet import * |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
""" | ||
Basic handler functions | ||
""" | ||
from telegram import Update | ||
from telegram.ext import CallbackContext | ||
|
||
from .utils import update_message | ||
|
||
def start(update: Update, context: CallbackContext): | ||
"""Start will listen to the /start command and send a message to the user""" | ||
if update.message is not None: | ||
context.bot.send_message( | ||
chat_id=update.effective_chat.id, | ||
text="I am the tweet bot. Send me a message and I will tweet it for you!" | ||
) | ||
else: | ||
update_message(update, context, "start") | ||
|
||
def test(update: Update, context: CallbackContext): | ||
"""Test function to test the bot""" | ||
if update.message is not None: | ||
context.bot.send_message( | ||
chat_id=update.effective_chat.id, | ||
text="This command does nothing, it is for testing purposes only." | ||
) | ||
else: | ||
update_message(update, context, "test") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
""" | ||
Handler function for tweets | ||
""" | ||
import logging | ||
|
||
from telegram import Update | ||
from telegram.ext import CallbackContext | ||
import tweepy | ||
|
||
from validation import restricted, check_tweet_length | ||
|
||
from twitter import twitter | ||
|
||
from redis_py import database | ||
|
||
from .utils import update_message, get_tweet_url | ||
|
||
|
||
@restricted | ||
def send_tweet(update: Update, context: CallbackContext): | ||
"""get_tweet_text will get the text from the message and tweet it""" | ||
if update.message is not None: | ||
logging.info("Message Received: %s", update.message.text) | ||
if not check_tweet_length(update.message.text): | ||
context.bot.send_message(chat_id=update.effective_chat.id, text="Tweet too long!") | ||
return | ||
try: | ||
tweet_id, text = twitter.tweet(update.message.text) | ||
logging.info("Tweet sent: %s", text) | ||
database.set_tweet(tweet_id, text) | ||
message = f"Tweeted: {text}\n{get_tweet_url(tweet_id)}" | ||
context.bot.send_message(chat_id=update.effective_chat.id, text=message) | ||
except tweepy.errors.BadRequest as error: | ||
logging.error(str(error)) | ||
context.bot.send_message( | ||
chat_id=update.effective_chat.id, text="Something went wrong!" + str(error)) | ||
else: | ||
update_message(update, context) | ||
|
||
def delete_tweet(update: Update, context: CallbackContext): | ||
"""Delete a tweet command""" | ||
if update.message is not None: | ||
tweet_id, tweet_text = database.get_tweet() | ||
logging.info("Tweet Data in Redis: %s - %s", tweet_id, tweet_text) | ||
if tweet_id is not None: | ||
twitter.delete_tweet(tweet_id) | ||
database.delete_tweet() | ||
logging.info("Tweet Deleted") | ||
context.bot.send_message(chat_id=update.effective_chat.id, text="Tweet deleted!") | ||
else: | ||
context.bot.send_message(chat_id=update.effective_chat.id, text="No tweet to delete!") | ||
else: | ||
update_message(update, context, "delete") | ||
|
||
def comment_tweet(update: Update, context: CallbackContext): | ||
"""Tweets as a comment to the last tweet""" | ||
if update.message is not None: | ||
tweet_id, tweet_text = database.get_tweet() | ||
logging.info("Tweet Data in Redis: %s - %s", tweet_id, tweet_text) | ||
if tweet_id is not None: | ||
try: | ||
# Send tweet | ||
tweet_message = " ".join(context.args) | ||
reply_tweet_id, reply_text = twitter.reply_to_tweet( | ||
tweet_message, tweet_id=tweet_id) | ||
logging.info("Tweet reply sent: %s", reply_text) | ||
# Setup message for bot | ||
message = f"{reply_text}\n{get_tweet_url(reply_tweet_id)}" | ||
message = message + f"\n\nIn reply to: {get_tweet_url(tweet_id)}\n{tweet_text}" | ||
context.bot.send_message(chat_id=update.effective_chat.id, text=message) | ||
except tweepy.errors.BadRequest as error: | ||
logging.error(str(error)) | ||
context.bot.send_message( | ||
chat_id=update.effective_chat.id, text=str(error)) | ||
else: | ||
context.bot.send_message( | ||
chat_id=update.effective_chat.id, text="No tweet to comment to!") | ||
else: | ||
update_message(update, context, "comment") |
Oops, something went wrong.