Skip to content

Forum in maintenance, we will back soon 🙂

A ChatGPT generated...
 
Notifications
Clear all

A ChatGPT generated script for monitoring Twitter (X) accounts and commenting.

5 Posts
3 Users
3 Reactions
409 Views
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
Topic starter
 

I have my AI VA creating scripts for me. Here is one to continuously monitor Twitter accounts and create a comment with OpenAI.

import os
import tweepy
import openai
import threading
import time

# Function to create API object
def create_api():
    consumer_key = os.environ.get('TWITTER_API_KEY')
    consumer_secret = os.environ.get('TWITTER_API_SECRET_KEY')
    access_token = os.environ.get('TWITTER_ACCESS_TOKEN')
    access_token_secret = os.environ.get('TWITTER_ACCESS_TOKEN_SECRET')

    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)

    api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
    return api

# Function to generate an AI-powered comment
def generate_comment(tweet_text):
    openai_api_key = os.environ.get('OPENAI_API_KEY')
    openai.api_key = openai_api_key

    try:
        response = openai.Completion.create(
            engine="text-davinci-003",  # Or the latest available model
            prompt=f"Create a polite and engaging response to this tweet:\n\n'{tweet_text}'\n\nResponse:",
            max_tokens=60
        )
        return response.choices[0].text.strip()
    except Exception as e:
        print("Error in generating AI comment:", e)
        return "Thank you for your tweet!"  # Fallback response

# Function to monitor tweets from a specific user
def monitor_tweets(api, username, sleep_interval):
    while True:
        tweets = api.user_timeline(screen_name=username, count=1, tweet_mode='extended')
        if tweets:
            latest_tweet = tweets[0]
            print(f"Latest tweet from {username}: {latest_tweet.full_text}")
            comment = generate_comment(latest_tweet.full_text)
            try:
                # Reply to the tweet
                api.update_status(status=comment, in_reply_to_status_id=latest_tweet.id, auto_populate_reply_metadata=True)
                print("Replied with comment:", comment)
            except Exception as e:
                print(f"Error during commenting on {username}'s tweet:", e)
        time.sleep(sleep_interval)

def main():
    api = create_api()
    usernames = ["user1", "user2", "user3"]  # Replace with the usernames you want to monitor
    sleep_interval = 60  # Time in seconds between checks

    threads = []
    for username in usernames:
        thread = threading.Thread(target=monitor_tweets, args=(api, username, sleep_interval))
        threads.append(thread)
        thread.start()

    for thread in threads:
        thread.join()

if __name__ == "__main__":
    main()

Notes on Usage

  1. Replace ["user1", "user2", "user3"] with the actual Twitter usernames you want to monitor.
  2. Adjust sleep_interval to control how often the script checks for new tweets. Be mindful of the Twitter API's rate limits.

Important Considerations

  • Rate Limits: Be cautious of Twitter's rate limits. Continuously monitoring multiple accounts may quickly exhaust your rate limit.
  • Concurrency Issues: Python's threading has limitations due to the Global Interpreter Lock (GIL). For more complex concurrent operations, you might need to consider other approaches like multiprocessing or asynchronous programming.
  • Error Handling: Robust error handling is essential, especially in a threaded environment where issues in one thread can impact the overall application.
  • Resource Usage: Monitor the resource usage of your script, as running multiple threads can be resource-intensive.

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 12/21/2023 3:09 pm
Hasan Aboul Hasan
(@admin)
Posts: 1253
Member Admin
 

Nice! This can be a prototype of a SAAS. 

Since you are already monitoring, you can also extract content ideas, brand mentions, and much more! 

 

 
Posted : 12/23/2023 11:32 am
SSAdvisor reacted
SSAdvisor
(@ssadvisor)
Posts: 1139
Noble Member
Topic starter
 

@admin thanks for the comments. I would like to ask you if it would be possible to add a iconized option to copy the code in a code block to the clipboard similar to the way ChatGPT does it's code blocks?

Regards,
Earnie Boyd, CEO
Seasoned Solutions Advisor LLC
Schedule 1-on-1 help
Join me on Slack

 
Posted : 12/23/2023 1:42 pm
Hasan Aboul Hasan
(@admin)
Posts: 1253
Member Admin
 

@ssadvisor thanks, I will try to add this feature

 
Posted : 12/25/2023 1:11 pm
(@Anonymous 27679)
Posts: 1
New Member Guest
 

@admin Thank You

 
Posted : 01/23/2024 7:11 pm
Share: