My bot shows a TypeError when initializing with Google Sheets. How can I resolve this? Revised code:
import telegram
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler
import gspread
from google.oauth2 import service_account
tg_key = 'YOUR_TOKEN'
sheet_id = 'NEW_SHEET_ID'
creds = service_account.Credentials.from_service_account_file('path/to/creds.json', scopes=['https://www.googleapis.com/auth/spreadsheets'])
def fetch_records(sheet_name):
client = gspread.authorize(creds)
ws = client.open_by_key(sheet_id).worksheet(sheet_name)
return ws.get_all_records()
def begin(update, context):
markup = telegram.InlineKeyboardMarkup([
[telegram.InlineKeyboardButton('Dashboard', callback_data='dashboard')],
[telegram.InlineKeyboardButton('KPI', callback_data='kpi')]
])
update.message.reply_text('Choose an option:', reply_markup=markup)
def handle_button(update, context):
query = update.callback_query
records = fetch_records(query.data)
query.edit_message_text(text=str(records))
def run_bot():
updater = Updater(tg_key, use_context=True)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('start', begin))
dispatcher.add_handler(CallbackQueryHandler(handle_button))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
run_bot()