N
N
Nusup2020-07-08 16:18:35
MySQL
Nusup, 2020-07-08 16:18:35

How to save geodata and photos in the MySQL database using the Pytelegrambotapi library (does not give errors, saves the first 2 parameters) but there is no geo and photo?

import telebot
import mysql.connector
from telebot import types, TeleBot
from telebot.types import Message

bot = telebot.TeleBot("941242828:AAHHweF6qU5Zx4tGAmVxi6kABBRFDElIpgk")

db = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="root",
    port="3306",
    database="test"
)

cursor = db.cursor()

cursor.execute("CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, user_id INT, openvisit TEXT , geo BLOB, photo BLOB)")

user_data = { }

class User:
    def __init__(self, openvisit):
        self.openvisit = openvisit
        self.geo = ' '
        self.photo=' '


@bot.message_handler(commands=['start'])
def send_welcome(message):
    visit = types.ReplyKeyboardMarkup(one_time_keyboard=True)
    visit_btn = types.KeyboardButton(text = 'Открыть')
    visit.add(visit_btn)

    msg = bot.send_message(message.chat.id, "Привет", reply_markup=visit)
    bot.register_next_step_handler(msg, process_openvisit_step)

def process_openvisit_step(message):
    user_id = message.from_user.id
    user_data[user_id] = User(message.text)

    geo=types.ReplyKeyboardMarkup(one_time_keyboard=True)
    geo_btn = types.KeyboardButton(text='Местоположение', request_location=True)
    geo.add(geo_btn)

    msg = bot.send_message(message.chat.id, "Отправьте текущее местоположениe:", reply_markup=geo)
    bot.register_next_step_handler(msg, process_geo_step)

def process_geo_step(message):
    user_id = message.from_user.id
    user = user_data[user_id]
    user.geo = message.text

    msg = bot.send_message(message.chat.id, "Теперь вышли фото")
    bot.register_next_step_handler(msg, process_photo_step)

def process_photo_step(message):
    user_id = message.from_user.id
    user = user_data[user_id]
    user.photo = message.text

    sql = "INSERT INTO testblob (user_id , openvisit , geo, photo) \
                                  VALUES (%s, %s, %s, %s)"

    val = (user_id, user.openvisit, user.geo, user.photo)
    cursor.execute(sql, val)
    db.commit()

    msg = bot.send_message(message.chat.id, "Спасибо")

bot.enable_save_next_step_handlers(delay=2)

bot.load_next_step_handlers()

if __name__ == '__main__':
    bot.polling(none_stop=True)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question