返回

连接 Telegram 机器人到 Google 表格并显示仪表盘,解决 TypeError: Updater.__init__() 问题

python

使用 Telegram 机器人连接到 Google 表格并显示仪表盘

在构建自动化和数据驱动的任务时,连接 Telegram 机器人和 Google 表格是一种强大的组合。本文将指导你如何解决一个常见的错误,并在你的仪表盘上显示关键绩效指标 (KPI)。

错误:缺少 update_queue 参数

当编写一个 Telegram 机器人时,试图连接到 Google 表格并显示 KPI 表时,可能会遇到一个错误:

TypeError: Updater.__init__() missing 1 required positional argument: 'update_queue'

这个错误表明 Updater 类缺少了一个必需的参数,即 update_queueupdate_queue 负责管理从 Telegram API 接收的更新。

解决方案:提供 update_queue

要解决此问题,你需要在实例化 Updater 时提供 update_queue

修改后的代码:

from telegram import Update, Bot
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, CallbackContext
from google.oauth2 import service_account
import gspread

# Telegram bot token
TOKEN = 'xxxxxyyyyyy'

# Google Sheets credentials
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
SERVICE_ACCOUNT_FILE = 'C:/Users/ASUS/Downloads/credentials.json'  # Specify the path to your credentials JSON file
spreadsheet_id = 'yyyyxxxxxxx'

# Authenticate with Google Sheets API
creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)

# Function to fetch data from Google Sheets
def get_sheet_data(sheet_name):
    gc = gspread.authorize(creds)
    sheet = gc.open_by_key(spreadsheet_id)
    worksheet = sheet.worksheet(sheet_name)
    return worksheet.get_all_values()

# Command handler for starting the bot
def start(update: Update, context: CallbackContext):
    keyboard = [[InlineKeyboardButton("Dashboard", callback_data='dashboard')],
                [InlineKeyboardButton("KPI", callback_data='kpi')]]
    reply_markup = InlineKeyboardMarkup(keyboard)
    update.message.reply_text('Please choose:', reply_markup=reply_markup)

# Callback handler for button presses
def button(update: Update, context: CallbackContext):
    query = update.callback_query
    sheet_name = query.data
    data = get_sheet_data(sheet_name)
    message = '\n'.join(['\t'.join(row) for row in data])
    query.edit_message_text(text=message)

def main():
    # Initialize the Updater with a custom Updater object
    updater = Updater(TOKEN, update_queue=None)
    # Assign the custom Updater object to a variable
    bot = updater.bot

    # Register handlers with the custom Updater object
    dp = updater.dispatcher

    dp.add_handler(CommandHandler("start", start))
    dp.add_handler(CallbackQueryHandler(button))

    # Start the custom Updater object
    updater.start_polling()
    # Idle the custom Updater object
    updater.idle()

if __name__ == '__main__':
    main()

其他提示

  • 确保在 SERVICE_ACCOUNT_FILE 中指定正确的凭据文件路径。
  • 确保 spreadsheet_id 变量包含正确的 Google 表格 ID。
  • 考虑使用异常处理来处理可能出现的错误。

结论

通过提供必要的 update_queue 参数,我们已经解决了 Telegram 机器人连接 Google 表格并显示 KPI 的问题。这使我们能够构建更强大和有用的应用程序。

常见问题解答

  1. 如何为 Telegram 机器人获取凭证?
    通过 Telegram BotFather 创建一个机器人并获取其密钥。

  2. 如何获取 Google 表格的 ID?
    打开 Google 表格,并在 URL 中查找 spreadsheet_id。

  3. 如何使用其他 Google 表格凭证类型?
    修改 service_account.Credentials 构造函数的参数以使用 OAuth2 凭证或服务帐户文件。

  4. 如何向 Telegram 机器人发送数据?
    使用 update.message.reply_textupdate.message.send_photo 等方法。

  5. 如何进一步自定义 Telegram 机器人?
    探索 Telegram Bot API 和使用高级功能,如菜单、位置共享和文件上传。