Skip to content

Commit

Permalink
added caching to newsletter feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredtanzd committed Apr 15, 2024
1 parent 0feb4ef commit 6c32923
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
22 changes: 18 additions & 4 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import json
import logging
import json
import time
from cachetools import TTLCache
from scamscraper import scrape_scam_stories
from langchain_openai import ChatOpenAI
# from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
Expand All @@ -16,6 +18,8 @@
from langchain.agents.agent import AgentExecutor
import agenttools

cache = TTLCache(maxsize=1, ttl=6 * 60 * 60) # maxsize=1 because we only need to cache one newsletter

# Enable logging
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.DEBUG
Expand Down Expand Up @@ -183,19 +187,29 @@ def callback_query(call):
def scam_newsletter(message):
logger.debug('scam_newsletter function called')
print("Received /scam_newsletter command.")


# Check if the newsletter is cached and still valid
cached_newsletter = cache.get('newsletter')
if cached_newsletter:
print("Sending cached newsletter.")
bot.send_message(message.chat.id, cached_newsletter)
return

try:
# Scrape recent scams from https://www.scamalert.sg/stories
print("Scraping recent scams...")
scam_stories, first_read_more_link = scrape_scam_stories()
print("Scam stories scraped.")

# Prompt GPT for counter measures
counter_measures = generate_counter_measures(scam_stories)

# Generate newsletter message
newsletter = generate_newsletter(scam_stories, counter_measures, first_read_more_link)


# Cache the newsletter
cache['newsletter'] = newsletter

# Send newsletter message to user
print("Sending newsletter to user.")
bot.send_message(message.chat.id, newsletter)
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ typing_extensions==4.10.0
urllib3==2.2.1
yarl==1.9.4
beautifulsoup4 == 4.12.3
regex == 2023.12.25
regex == 2023.12.25
cachetools == 5.3.3

0 comments on commit 6c32923

Please sign in to comment.