Live Twitter Mentions

The Live Twitter Mentions feature in Bragabot is designed to monitor twitter for specific hashtags and account mentions in real-time and forward relevant tweets to your Telegram groups. This feature ensures that your community stays updated with the latest conversations related to your project. Similar to the Tweets Forwarding feature, Live Twitter Mentions includes a backup search mechanism to ensure no tweet is missed, and it incorporates additional logic for managing different client tiers (Braga Premium or Braga Pay-as-You-Go).

Architecture Overview

  1. Custom Twitter Endpoint: A dedicated service running in a Kubernetes pod that handles real-time tweet streaming for mentions and periodic searches.

  2. Bragabot Backend: The core platform where groups and their settings are managed, and where the forwarded mentions are processed and stored.

  3. Database: Stores configuration settings for each group, including enabled hashtags, account mentions, client tiers, and history of forwarded mentions.

Workflow

Live Twitter Mentions Workflow

Components and Implementation Details

1. Custom Twitter Endpoint

The Custom Twitter Endpoint for Live Twitter Mentions operates similarly to the Tweets Forwarding feature, with additional logic to handle client tiers (Braga Premium and Braga Pay-as-You-Go).

1.1. Real-Time Twitter Mention Streaming

The real-time streaming service listens for tweets mentioning specific hashtags or project accounts based on group settings. It filters these tweets and forwards them to the relevant Telegram groups, applying additional checks based on the client tier.

Steps:

  1. Fetching Active Groups:

    • The service fetches all groups from the database where Live Twitter Mentions is active.

    • For each group, it retrieves settings such as enabled hashtags, account mentions, and the client tier (Braga Premium or Braga Pay-as-You-Go) along with which tweet types are enabled (e.g., Tweets, Retweets, Replies).

  2. Building Mention Stream Rules:

    • The service dynamically constructs rules for the Twitter Streaming API based on the hashtags and account mentions configured for each group.

    • Rules include filters for specific tweet types, such as excluding replies or including only retweets or quote tweets.

Code Example: Constructing mention stream rules based on group settings.

def build_mention_stream_rules(groups):
    mention_rules = []
    for group in groups:
        if group.live_mentions_active:
            base_rule = ""

            # Add hashtags to the rules
            for hashtag in group.hashtags:
                base_rule += f" #{hashtag}"

            # Add account mentions to the rules
            base_rule += f" @{group.twitter_username}"

            # Add tweet type filters
            if not group.include_replies:
                base_rule += " -is:reply"
            if not group.include_retweets:
                base_rule += " -is:retweet"
            if not group.include_quotes:
                base_rule += " -is:quote"

            base_rule = base_rule.strip()
            mention_rules.append({"value": base_rule, "tag": f"group:{group.id}"})

    return mention_rules

# Usage
groups = fetch_active_groups_from_db()
mention_stream_rules = build_mention_stream_rules(groups)
  1. Starting the Stream:

    • The Twitter Streaming API is initiated using the constructed rules for mentions.

    • As tweets are captured, they are processed and forwarded to the corresponding Telegram groups, with additional checks based on the client tier.

Code Example: Starting the mention stream.

def start_mention_stream(rules):
    # Initialize the Twitter Streaming Client
    stream = TwitterStreamingClient(bearer_token=os.getenv('TWITTER_BEARER_TOKEN'))
    
    # Apply the rules
    stream.set_rules(rules)
    
    # Start the stream
    stream.filter(track=[rule['value'] for rule in rules])

# Usage
start_mention_stream(mention_stream_rules)
  1. Processing and Forwarding Mentions:

    • For each tweet received:

      • Check Client Tier: Determine whether the group is using Braga Premium or Braga Pay-as-You-Go.

      • Braga Premium: The mention is forwarded directly to the group.

      • Braga Pay-as-You-Go: Additional checks are performed to ensure sufficient funds are available. If funds are sufficient, the system deducts the appropriate amount before forwarding the mention.

      • Forwarding: The mention is then forwarded to the relevant Telegram group.

Code Example: Processing a tweet mention based on client tier.

def process_mention(tweet, group):
    if group.client_tier == "premium":
        # Forward the mention directly
        forward_to_telegram_group(tweet, group.group_id)
    elif group.client_tier == "pay_as_you_go":
        # Check and deduct funds
        if check_and_deduct_funds(group):
            forward_to_telegram_group(tweet, group.group_id)
        else:
            notify_group_admins_insufficient_funds(group)
            disable_live_twitter_mention(group)

1.2. Recent Search Mechanism

Overview: The Recent Search Mechanism serves as a backup to the real-time stream, ensuring that any mentions missed due to stream failures are captured and forwarded.

Steps:

  1. Periodic Execution:

    • The Recent Search runs every 5 minutes as a scheduled job within the Kubernetes pod.

  2. Fetching Recent Mentions:

    • The service queries Twitter’s API for mentions (hashtags and account mentions) posted in the last 15 minutes.

Code Example: Fetching recent mentions.

def fetch_recent_mentions(groups):
    recent_mentions = []
    for group in groups:
        query = build_recent_search_query(group)
        tweets = twitter_api.search_recent_tweets(query=query)
        
        for tweet in tweets:
            if not is_tweet_already_notified(tweet.id, group.id):
                recent_mentions.append((tweet, group))
    
    return recent_mentions
  1. Processing and Forwarding:

    • Similar to the real-time stream, the fetched mentions are processed and forwarded to the Telegram groups if they haven’t been notified yet, respecting the client tier checks.

  2. Database Update:

    • Each processed mention is logged in the database to ensure it isn’t forwarded again by either the stream or future searches.

2. Bragabot Backend Integration

The Bragabot Backend handles the management of group settings, such as enabling/disabling Live Twitter Mentions, configuring hashtags, and managing client tiers (Braga Premium or Braga Pay-as-Go). It also interacts with the Custom Twitter Endpoint by providing the necessary group settings.

Key Functions:

  • Group Settings Management: Admins can configure which hashtags should be monitored and whether the group uses Braga Premium or Braga Pay-as-Go.

  • Integration with Custom Twitter Endpoint: The backend periodically updates the Custom Twitter Endpoint with group settings.

When processing tweets, the system checks whether screenshots and social links should be included. If enabled, the system interacts with the Bragabot Screenshots API and the database to retrieve the necessary data.

Steps:

  1. Requesting Screenshots:

    • If screenshots are enabled, a request is sent to the Bragabot Screenshots API to capture the tweet’s appearance.

Code Example: Capturing a screenshot.

def capture_screenshot(tweet):
    response = requests.post(
        url="https://api.bragabot.com/bragabot-screenshot/screenshot/",
        json={"tweet_url": f"https://twitter.com/{tweet['author']}/status/{tweet['id']}"}
    )
    return response.json().get('screenshot_url')

# Usage
screenshot_url = capture_screenshot(tweet)
  1. Fetching Social Links:

    • If social links are enabled, they are fetched from the database and formatted for inclusion in the forwarded message.

The Live Twitter Mentions feature in Bragabot is a comprehensive implementation that ensures real-time and reliable forwarding of Twitter mentions (hashtags and account mentions) to Telegram groups. By utilizing a Custom Twitter Endpoint, real-time streaming, and a backup search mechanism, Bragabot guarantees that no mention is missed, and each mention is processed according to the group’s specific settings and client tier. The modular design, running independently in a Kubernetes pod, ensures scalability and resilience, making this feature an essential tool for maintaining active and informed communities.

Last updated