Telegram Bot - Python and Google Sheets Integration Issue

I’m attempting to develop a Python-based Telegram bot that connects to Google Sheets to display data from two specific sheets on demand. The bot is designed to have two buttons: one for viewing the “dashboard” and the other for the “KPI”. However, I’m facing an error when I run my code:

Traceback (most recent call last):
  File "C:\Users\ASUS\Downloads\philbot.py", line 51, in <module>
    main()
  File "C:\Users\ASUS\Downloads\philbot.py", line 41, in main
    updater = Updater(TOKEN)
                  ^^^^^^^^^^^^^^
TypeError: Updater.__init__() missing 1 required positional argument: 'update_queue'

Below is my existing code:

import telegram
from telegram import InlineKeyboardMarkup, InlineKeyboardButton
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler
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'
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(bot, update):
    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(bot, update):
    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():
    updater = Updater(TOKEN)
    dp = updater.dispatcher

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

    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()

The intent is for the bot to allow users to click buttons that retrieve and display different datasets from Google Sheets. Can anyone help me resolve this initialization error?

yeah i ran into this issue as well. try downgrading with pip install python-telegram-bot==13.15. if u want the newest features, check the Application class in the documentation. should solve that error!

You’re mixing different versions of python-telegram-bot. That syntax is for version 13.x, but newer versions changed how the Updater class works. I hit this exact issue last month on a similar project. Quick fix: run pip show python-telegram-bot to check your version, then either downgrade to 13.15 or rewrite your code for the newer version. If you stick with 13.x, pin the exact version in your requirements.txt so you don’t run into this again when deploying.

You’re encountering this error due to a mix-up between older and newer syntax in the python-telegram-bot library. In recent versions, specifically v20 and above, the Updater class has been replaced with the Application class. To resolve the problem, update your imports: instead of importing Updater, import Application. Then, modify the line where you instantiate the updater from updater = Updater(TOKEN) to application = Application.builder().token(TOKEN).build(). Additionally, your handler functions need to be declared as asynchronous, so change def start(bot, update) to async def start(update, context). These adjustments should fix the initialization error.