0

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?

James C.
  • 71
  • 1
  • 5
  • Probably not related: looks like we're importing `requests` but never using it. Also, it's not clear what's the error. Also, how did you structure the code (filenames, directory structure etc). Did you follow: https://cloud.google.com/functions/docs/writing/specifying-dependencies-python#python38 ? – Nir Alfasi May 27 '21 at 14:04
  • Thanks for asking. requests library was used for a function that I redacted because the program never got past main() the error seems to be some positional argument passed? but I don't know what it is. Yup, I specified the dependencies in requirements.txt and they are downloadable via pip – James C. May 28 '21 at 15:07

1 Answers1

1

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

guillaume blaquiere
  • 66,369
  • 2
  • 47
  • 76
  • --entry-point=main didn't work ERROR: (gcloud.functions.deploy) argument (NAME : --region=REGION): Must be specified. I deployed again without --entry-points but I'm back to the same problem – James C. May 28 '21 at 15:28
  • Take the getting started guide. You need the region, the runtime and the trigger-http param. I just provided a tips if you don't name your function with the same name as your entry point. – guillaume blaquiere May 28 '21 at 16:12
  • Thanks for the tip! The command I used was: gcloud functions deploy main --entry-point main --set-env-vars "SOME SECRETS" --runtime python38 --trigger-http --project=project_id --allow-unauthenticated --verbosity=debug --region=us-central1 – James C. May 30 '21 at 02:20
  • @JamesC. that's a different issue, maybe [this](https://stackoverflow.com/questions/44461164/how-can-i-specify-the-region-of-a-google-cloud-function) could help – Nir Alfasi May 30 '21 at 06:12