-3

I have no experience coding with Python but I wanted to experiment with twitter so I asked Google's Bard to set up a twitter bot that publishes AI news every hour. I have the API keys but I will not include them for obvious reasons. When I try to run it on my terminal, it says that there is incorrect syntax on line 17. If anyone could help me fix this, as I have no idea how to do it myself. This is the code:

import tweepy
import requests
from newsapi import NewsAPIClient

def get_news():
  """Gets a list of news articles about Artificial Intelligence."""
  newsapi = NewsAPIClient(api_key="YOUR_API_KEY")
  top_headlines = newsapi.get_top_headlines(sources="techcrunch,theverge",
                                         q="artificial intelligence")
  return top_headlines["articles"]

def generate_tweet(article):
  """Generates a tweet for a news article."""
  title = article["title"]
  description = article["description"]
  url = article["url"]
  tweet = f"#AINews: {title.text} - {description.text} - {url}"
  return tweet

def post_tweet(tweet):
  """Posts a tweet to Twitter."""
  api = tweepy.API(auth=auth)
  api.update_status(tweet)

def main():
  # Get the Twitter API keys and tokens from the environment.
  consumer_key = os.environ['CONSUMER_KEY']
  consumer_secret = os.environ['CONSUMER_SECRET']
  access_token = os.environ['ACCESS_TOKEN']
  access_token_secret = os.environ['ACCESS_TOKEN_SECRET']

  # Create an auth object with the API keys and tokens.
  auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  auth.set_access_token(access_token, access_token_secret)

  # Get a list of news articles about Artificial Intelligence.
  news_articles = get_news()

  # Generate tweets for each news article.
  tweets = [generate_tweet(article) for article in news_articles]

  # Post the tweets to Twitter.
  for tweet in tweets:
    post_tweet(tweet)

if __name__ == "__main__":
  main()

I tried to ask ChatGPT and Bard to fix it. They changed description in line 17 into a string and then into a dictionary but neither of those worked.

  • What line is line 17? – tadman Aug 09 '23 at 15:46
  • “*I tried to ask ChatGPT and Bard to fix it.*” LLMs are not serious programming tools and shouldn’t be relied on as such. – esqew Aug 09 '23 at 15:47
  • *"I have no experience coding with Python but I wanted to experiment with twitter so I asked Google's Bard to set up a twitter bot that publishes AI news every hour."* - then you are at the wrong place. SO is no code writing and not "fix my AI code" place. – luk2302 Aug 09 '23 at 15:47
  • @esqew They do produce code-shaped outputs, though! – tadman Aug 09 '23 at 15:47
  • Does even have an API any more? – tadman Aug 09 '23 at 15:48

0 Answers0