Search code examples
pythonpython-3.xtwitterpipgoogle-cloud-functions

How to resolve: Google cloud function TypeError: main() takes 0 positional arguments but 1 was given?


I have made a small Twitter bot that I can successfully run locally on my computer and I have then decided to host the bot on a server. I created a google cloud function whereby I am able to paste my python code onto their editor but as I launch the function and run a test i get the following error message:

Error: function terminated. Recommended action: inspect logs for termination reason. Additional troubleshooting documentation can be found at https://cloud.google.com/functions/docs/troubleshooting#logging Details: 500 Internal Server Error: The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

And error messages in the log is as follows:

Traceback (most recent call last): File “/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py”, line 2073, in wsgi_app response = self.full_dispatch_request() File “/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py”, line 1518, in full_dispatch_request rv = self.handle_user_exception(e) File “/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py”, line 1516, in full_dispatch_request rv = self.dispatch_request() File “/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py”, line 1502, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args) File “/layers/google.python.pip/pip/lib/python3.8/site-packages/functions_framework/init.py”, line 99, in view_func return function(request._get_current_object()) TypeError: main() takes 0 positional arguments but 1 was given

I have added an image from the Google platform but i have also added the python code from Google functions inline editor:

Setting up the python code for google cloud function

def main():
  import tweepy
  import time
  import config


  client = tweepy.Client(bearer_token=config.BEARER_TOKEN, consumer_key=config.CONSUMER_KEY,
                       consumer_secret=config.CONSUMER_SECRET, access_token=config.ACCESS_KEY, access_token_secret=config.ACCESS_SECRET)

  auth = tweepy.OAuth1UserHandler(
            config.CONSUMER_KEY, config.CONSUMER_SECRET, config.ACCESS_KEY, config.ACCESS_SECRET)
  api = tweepy.API(auth)

  search_terms = ["#MUFC"]



  class MyStream(tweepy.StreamingClient):

    def on_connect(self):

      print("Connected")


    def on_tweet(self, tweet):

      if tweet.referenced_tweets == None:
        print(tweet.text)
        client.retweet(tweet.id)
        client.like(tweet.id)
        time.sleep(0.5)


  stream = MyStream(bearer_token=config.BEARER_TOKEN)


  rules = tweepy.StreamRule("(from:FabrizioRomano) (#MUFC) (Manchester United) (-is:reply)")

  stream.add_rules(rules)

  print(stream.get_rules())

  stream.filter()

Solution

  • When Cloud Functions activate your code - they automatically pass through some arguments so that you can do things like change code behaviour based on the parameters of the URL you activate.

    In short - the last line of the error message is pointing you in the right direction;

    TypeError: main() takes 0 positional arguments but 1 was given

    This means your function wasn't expecting any arguments and it has been surprised to receive one. Easiest fix is likely: change main() to be main(request)