0

我正在制作一个项目,当用户按下按钮时,我必须将数据从 mysql 发送到电报机器人,但是我不能这样做。该程序假设按下“显示数据”按钮将用户名列表发送给用户。

我面临的问题是我不断收到这个错误'KeyError:('csp',)'。csp 是用户名之一。

这是应该在电报中打印的用户名列表

('csp',) ('gss',) ('ccy',) ('ag',) ('rc',)

import mysql.connector as mysql
import json
import logging
import sys
import time
import telepot
from urllib import request
from telepot.loop import MessageLoop
from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton
from pprint import pprint

# store the TOKEN for the Telegram Bot
db = mysql.connect(
host = "localhost",
user = "root",
passwd = "",
database = "database"
)

cursor = db.cursor()

TOKEN = 'token'
bot = telepot.Bot(TOKEN)
def on_chat_message(msg):
    content_type, chat_type, chat_id = telepot.glance(msg)

    keyboard = InlineKeyboardMarkup(inline_keyboard=[
                   [InlineKeyboardButton(text='Show Data', callback_data='show')],
                   [InlineKeyboardButton(text='Delete Data', callback_data='delete')],
                   [InlineKeyboardButton(text='Show latest Data', callback_data='lastest')],
               ])

    bot.sendMessage(chat_id, 'Use the menu to show your WeatherStation values', reply_markup=keyboard)

def on_callback_query(msg):
    
    query_id, from_id, query_data = telepot.glance(msg, flavor='callback_query')

    print('Callback Query:', query_id, from_id, query_data)

    if(query_data == 'show'):
        
        query = "SELECT user_name FROM users"
        usernames = cursor.execute(query)

        bot.sendMessage(from_id, msg[usernames])
        

            
    elif(query_data == 'delete'):
        query = "DELETE FROM users"
        cursor.execute(query)
        db.commit()
 
    elif(query_data == 'lastest'):
        bot.sendMessage(from_id, text='test3')
        

bot = telepot.Bot(TOKEN)
MessageLoop(bot, {'chat': on_chat_message,'callback_query': on_callback_query}).run_as_thread()
print('Listening ...')

while 1:
    time.sleep(100)                 
4

0 回答 0