B
B
bartmangame2021-01-28 13:13:20
Python
bartmangame, 2021-01-28 13:13:20

How to add message.text to sqlite database after certain button?

I do not understand how it is possible to implement a record in the database after pressing a certain button. In general, something seems to have happened, but the problem is that all messages entered by the user are recorded, and I need it after this button.
Here is the code:

import sqlite3
import telebot
from telebot import types

db = sqlite3.connect('database.db', check_same_thread = False) 
cursor = db.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS database (
  user_id INTEGER PRIMARY KEY NOT NULL,
  name TEXT,
  story TEXT
)''')

db.commit()

token = '1563349957:AAHZeyJrQteHs18UT5hYKU'
bot = telebot.TeleBot(token)

menu = types.ReplyKeyboardMarkup(True, True)
menu.add(telebot.types.InlineKeyboardButton(text='Story'))

@bot.message_handler(commands = ['start'])
def start(message):
  cursor.execute(f'INSERT INTO database (user_id, name) VALUES ({message.from_user.id}, {message.from_user.first_name})')
  db.commit()
  bot.send_message(message.chat.id, text=u'Привет, чтобы рассказать иторию нажми кнопку ниже', reply_markup=menu)
@bot.message_handler(content_types = ['text'])
def messages(message):
  if message.text == 'Story':
    bot.send_message(message.chat.id, text=u'Напиши свою историю сюда')
  if message.text == 'Напиши свою историю сюда?':
    text = message.text
        cursor.execute(f'UPDATE database SET story = "{text}" WHERE user_id = "{message.from_user.id}"')
        db.commit()
bot.polling()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-01-28
@bartmangame

@bot.message_handler(content_types = ['text'])
def messages(message):
  if message.text == 'Story':
    msg = bot.send_message(message.chat.id, text=u'Напиши свою историю сюда')
    bot.register_next_step_handler(msg, update_database)

def update_database(message):
    # В message.text уже будет история пользователя

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question