NameError: name 'startCommand' is not defined - PYTHON

NameError: name 'startCommand' is not defined - PYTHON

from coinbase.wallet.client import Client
from telegram import ParseMode
from telegram.ext import CommandHandler, Defaults, Updater

COINBASE_KEY = 'xxxxxxxxxxxx'
COINBASE_SECRET = 'xxxxxxxxxxxx' 
TELEGRAM_TOKEN = 'xxxxxxxxxxxx'

coinbase_client = Client(COINBASE_KEY, COINBASE_SECRET)

#if __name__ == '__main__':
updater = Updater(token=TELEGRAM_TOKEN, defaults=Defaults(parse_mode=ParseMode.HTML))
dispatcher = updater.dispatcher
dispatcher.add_handler('start', startCommand) # Accessed via /start
dispatcher.add_handler('alert', priceAlert) # Accessed via /alert

updater.start_polling() # Start the bot

updater.idle() # Wait for the script to be stopped, this will stop the bot


def startCommand(update, context):
    context.bot.send_message(chat_id=update.effective_chat.id, text='Hello there!')

def priceAlert(update, context):
    if len(context.args) > 2:
        crypto = context.args[0].upper()
        sign = context.args[1]
        price = context.args[2]

        context.job_queue.run_repeating(priceAlertCallback, interval=15, first=15, context=[crypto, sign, price, update.message.chat_id])

        response = f"⏳ I will send you a message when the price of {crypto} reaches £{price}, \n"
        response += f"the current price of {crypto} is £{coinbase_client.get_spot_price(currency_pair=crypto + '-GBP')['amount']}"
    else:
        response = '⚠️ Please provide a crypto code and a price value: \n<i>/price_alert {crypto code} {> / &lt;} {price}</i>'

    context.bot.send_message(chat_id=update.effective_chat.id, text=response)


def priceAlertCallback(context):
    crypto = context.job.context[0]
    sign = context.job.context[1]
    price = context.job.context[2]
    chat_id = context.job.context[3]

    send = False
    spot_price = coinbase_client.get_spot_price(currency_pair=crypto + '-GBP')['amount']

    if sign == '<':
        if float(price) >= float(spot_price):
            send = True
    else:
        if float(price) <= float(spot_price):
            send = True

    if send:
        response = f' {crypto} has surpassed £{price} and has just reached <b>£{spot_price}</b>!'

        context.job.schedule_removal()

        context.bot.send_message(chat_id=chat_id, text=response)

enter image description here

我在上面的代码中遇到了这个错误,而且我已经尝试更改 def 的位置但是,它也显示错误,如何解决这个问题? 这是 telegram bot 的代码,而且一直显示 NameError,我已经添加了 python3 和 pip,但仍然没有解决

尝试

dispatcher.add_handler('start', startCommand()) # Accessed via /start
dispatcher.add_handler('alert', priceAlert()) # Accessed via /alert

您还需要添加两个函数所需的两个参数。

dispatcher.add_handler('start', startCommand(update, context))
dispatcher.add_handler('alert', startCommand(update, context))

我不太确定这两个函数接收了哪些数据,但我猜这就是机器人返回的数据。

Python 从上到下读取文件。所以当你调用dispatcher.add_handler('start', startCommand)时,函数startCommand还不知道。移动部分

updater = Updater(token=TELEGRAM_TOKEN, defaults=Defaults(parse_mode=ParseMode.HTML))
dispatcher = updater.dispatcher
dispatcher.add_handler('start', startCommand) # Accessed via /start
dispatcher.add_handler('alert', priceAlert) # Accessed via /alert

updater.start_polling() # Start the bot

updater.idle() # Wait for the script to be stopped, this will stop the bot

回调定义下方。

除此之外,add_handler 需要一个 Handler 作为参数,在你的例子中是 add_handler(CommandHanlder('start', startCommand)。请参阅 PTB tutorial as well as the examples


免责声明:我是 python-telegram-bot 库的当前维护者。