James C.
James C.

Reputation: 81

Python script working on local machine but not on Google Cloud Functions

My function has successfully deployed on GCF but is not working:


import requests
from telegram.ext import CommandHandler, CallbackQueryHandler, Filters, MessageHandler, Updater, CallbackContext
from telegram import Update
import logging
import os
import re


# Setup Logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)

# Constants
NEWS_ENDPOINT = f"https://newsapi.org/v2/everything"


API_KEY = os.environ.get('NEWS_API_KEY')
TOKEN = os.environ.get('TOKEN')

# Setup Webhook
WEBHOOK = os.environ.get('WEBHOOK')

# Setup PORT
PORT = int(os.environ.get('PORT', '443'))



def main():
    updater = Updater(token=TOKEN, use_context=True)
    dispatcher = updater.dispatcher

    # Adding commands
    dispatcher.add_handler(CommandHandler("about", about_command))
    dispatcher.add_handler(CommandHandler("get", get_articles))
    dispatcher.add_handler(CommandHandler("start", get_articles))
    dispatcher.add_handler(CommandHandler("keywords", keywords))

    dispatcher.add_handler(MessageHandler(Filters.command, unknown_command))
    # Toggle between polling and webhook based on env file
    MODE = os.environ.get("MODE", "WEBHOOK")
    if MODE == "polling":
        logging.info("Polling mode")
        updater.start_polling()
    else:
        logging.info("Webhook mode")
        updater.start_webhook(listen="0.0.0.0",
                              port=PORT,
                              url_path=TOKEN,
                              webhook_url=WEBHOOK)
        # updater.bot.setWebhook(WEBHOOK + TOKEN)

    # Run the bot until you press Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT.
    updater.idle()

 if __name__ == '__main__':
     from dotenv import load_dotenv
     load_dotenv()
     main()

Traceback (most recent call last): File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app response = self.full_dispatch_request() File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1952, 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 1821, in handle_user_exception reraise(exc_type, exc_value, tb) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise raise value File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request rv = self.dispatch_request() File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request return self.view_functionsrule.endpoint File "/layers/google.python.pip/pip/lib/python3.8/site-packages/functions_framework/init.py", line 67, in view_func return function(request._get_current_object()) TypeError: main() takes 0 positional arguments but 1 was given

I'm not sure why 1 positional argument was given My best guess is GCF is referencing the main.py as a class so self is passed to main? But how do I overcome that?

Upvotes: 0

Views: 698

Answers (1)

guillaume blaquiere
guillaume blaquiere

Reputation: 75970

You must comply with the Cloud Functions method Signature, and set the correct entrypoint during the deployment.

#In your script
def hello_http(request):
...
...


# When you deploy
gcloud functions deploy --entry-point=hello_http ........

Follow the Quickstart guide for more details

Upvotes: 1

Related Questions