title: Twitter reply-to-mentions bot programmed in Python works once and then crashes with error 400: what is the problem?
body: <p>I´ve been building a bot and it works exactly as intended, but only for one Tweet. Then, it waits 60 seconds, and, if it doesn´t find a new Tweet to reply to (since it´s configured to reply to the most recent Tweet), it throws an error (it´s 400 as in &quot;400: Bad Authentication Data&quot;, but I think the issue is not that, since the bot posts on Twitter once without any issues. However, I do think it´s possibly some kind of Bad Request error).
  Whenever it crashes, I can just run in my command &quot;python (botname).py&quot; and it works once if there is now a new Tweet, but then, it crashes again.
  I want the bot to run properly by itself, so I would really appreciate some help!
  This is the code in my file:</p>
  <pre><code>
  #!/usr/bin/env python
  # tweepy-bots/bots/autoreply.py
  
  
  import tweepy
  import logging
  from config import create_api
  import time
  import re
  from googlesearch import search
  import sys
  import io
  
  
  logging.basicConfig(level=logging.INFO)
  logger = logging.getLogger()
  
  
  def check_mentions(api, since_id):
  
      logger.info(&quot;Collecting info&quot;)
  
      new_since_id = since_id
  
      for tweet in tweepy.Cursor(api.mentions_timeline,
          since_id=since_id).items():
  
          new_since_id = max(tweet.id, new_since_id)
  
  
          if tweet.in_reply_to_status_id is not None:
              in_reply_to_status_id = tweet.id
              status_id = tweet.in_reply_to_status_id
              tweet_u = api.get_status(status_id,tweet_mode='extended')
  
          logger.info(f&quot;Answering to {tweet.user.name}&quot;)
          
  
          # remove words between 1 and 3
          shortword = re.compile(r'\W*\b\w{1,3}\b')
  
          keywords_search = str(shortword.sub('', tweet_u.full_text))
  
          print(keywords_search)
  
          if keywords_search is not None:
  
                  mystring = search(keywords_search, num_results=500)
          else:
                  mystring = search(&quot;error&quot;, num_results=1)
  
          print(mystring)
  
          output_info=[]
          for word in mystring:
                  if &quot;harvard&quot; in word or &quot;cornell&quot; in word or &quot;researchgate&quot; in word or &quot;yale&quot; in word or &quot;rutgers&quot; in word or &quot;caltech&quot; in word or &quot;upenn&quot; in word or &quot;princeton&quot; in word or &quot;columbia&quot; in word or &quot;journal&quot; in word or &quot;mit&quot; in word or &quot;stanford&quot; in word or &quot;gov&quot; in word or &quot;pubmed&quot; in word or &quot;theguardian&quot; in word or &quot;aaas&quot; in word or &quot;bbc&quot; in word or &quot;rice&quot; in word or &quot;ams&quot; in word or &quot;sciencemag&quot; in word or &quot;research&quot; in word or &quot;article&quot; in word or &quot;publication&quot; in word or &quot;nationalgeographic&quot; in word or &quot;ngenes&quot; in word:
                                  output_info.append(word)
                                  infostringa = ' '.join(output_info)
  
  
          if output_info:
                  output_info4 = output_info[:5]
                  infostring = ' '.join(output_info4)
                  print(infostring)
                  status = &quot;Hi there! This may be what you're looking for &quot; + infostring
                  len(status) &lt;= 280
                  api.update_status(status, in_reply_to_status_id=tweet.id, auto_populate_reply_metadata=True)
  
          else:
                  status = &quot;Sorry, I cannot help you with that :(. You might want to try again with a distinctly sourced Tweet&quot;
                  api.update_status(status, in_reply_to_status_id=tweet.id, auto_populate_reply_metadata=True)
  
          print(status)
  
          return new_since_id
      return check_mentions
  
  
  
  
  
  def main():
      api = create_api()
      since_id = 1 #the last mention you have.
      while True:
          since_id = check_mentions(api, since_id)
          logger.info(&quot;Waiting...&quot;)
          time.sleep(60)
  
  main()
  </code></pre>
  <p>My config module:</p>
  <pre><code># tweepy-bots/bots/config.py
  import tweepy
  import logging
  import os
  
  logger = logging.getLogger()
  
  def create_api():
      consumer_key = os.getenv(&quot;CONSUMER_KEY&quot;)
      consumer_secret = os.getenv(&quot;CONSUMER_SECRET&quot;)
      access_token = os.getenv(&quot;ACCESS_TOKEN&quot;)
      access_token_secret = os.getenv(&quot;ACCESS_TOKEN_SECRET&quot;)
  
      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)
      try:
          api.verify_credentials()
      except Exception as e:
          logger.error(&quot;Error creating API&quot;, exc_info=True)
          raise e
      logger.info(&quot;API created&quot;)
      return api
  </code></pre>
  <p>The error raised:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;C:\Users\maria\OneDrive\Documentos\Lara\Python\Factualbot\botstring32.py&quot;, line 92, in &lt;module&gt;
      main()
    File &quot;C:\Users\maria\OneDrive\Documentos\Lara\Python\Factualbot\botstring32.py&quot;, line 87, in main
      since_id = check_mentions(api, since_id)
    File &quot;C:\Users\maria\OneDrive\Documentos\Lara\Python\Factualbot\botstring32.py&quot;, line 26, in check_mentions
      for tweet in tweepy.Cursor(api.mentions_timeline, since_id=since_id).items():
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\cursor.py&quot;, line 51, in __next__
      return self.next()
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\cursor.py&quot;, line 243, in next
      self.current_page = self.page_iterator.next()
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\cursor.py&quot;, line 132, in next
      data = self.method(max_id=self.max_id, parser=RawParser(), *self.args, **self.kwargs)
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\binder.py&quot;, line 253, in _call
      return method.execute()
    File &quot;C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\binder.py&quot;, line 234, in execute
      raise TweepError(error_msg, resp, api_code=api_error_code)
  tweepy.error.TweepError: Twitter error response: status code = 400
  </code></pre>
  <p>Thank you really much!</p>