V
V
Vitaliy4452021-08-10 16:09:00
Python
Vitaliy445, 2021-08-10 16:09:00

What other ways can you update user variables in Telegram(telebot)?

I have a problem, in what other way can I update the variables about the user
The problem is that the variables are only assigned when you press / start

@bot.message_handler(commands=['start'])
def welcome(message):
    global chat_user_id, name_user, user_id
    chat_user_id = message.chat.id
    user_id = message.from_user.id
    name_user = message.from_user.first_name

That's how everything in the bot
611277cf3a8fc489323126.jpeg

Well, the buttons are processed by the function
@bot.callback_query_handler(func=lambda call: True)
def check(call):
    global chat_user_id, name_user, user_id
    if call.message:
        if call.data == "edit":
            answer1()   
        elif call.data =="show"
           answer2()


As you already understood, when the bot will be used, it will be kranty. All answers will go to the one who last pressed /start

And here is the question
How do I update user variables, is it possible in these code conditions?

I know that the dictionary \ list helps in this situation, can you tell us more about this method (How to assign \ update a variable and how to access it). I will be very grateful for the answer

The code

# -*- coding: utf-8 -*-
import telebot
import config
from telebot.apihelper import ApiTelegramException

from telebot import types
from array import *

bot = telebot.TeleBot(config.TOKEN)
chat_user_id = 0  # ID чата пользователя
user_id = 0
name_user = ""
chat_id_channel1 = -100.....
chat_id_channel2 = -100....
chat_id_channel3 = -100....
chat_id_channel4 = -100...
chat_id_channel5 = -100....


@bot.message_handler(commands=['start'])
def welcome(message):
    global chat_user_id, name_user, user_id
    chat_user_id = message.chat.id
    user_id = message.from_user.id
    name_user = message.from_user.first_name
    # keyboard
    markup = types.InlineKeyboardMarkup(row_width=1)
    item1P = types.InlineKeyboardButton("Кнопка под сообщением", callback_data='want')
    markup.add(item1P)

    bot.send_message(message.chat.id,
                     "Текст".format(
                         message.from_user, bot.get_me()),
                     parse_mode='html')

    bot.send_message(message.chat.id,
                     "Текст".format(
                         message.from_user, bot.get_me()),
                     parse_mode='html', reply_markup=markup)


def answer1():
    img = open('Money.jpg', 'rb')
    bot.send_photo(Chat_user_id, img,
                   caption="Текст", parse_mode="Markdown")
    markup = types.InlineKeyboardMarkup(row_width=1)
    item1P = types.InlineKeyboardButton("Кнопка под сообщением", callback_data='nextquest')
    markup.add(item1P)
    bot.send_message(Chat_user_id,
                     Name_user + "Текст",
                     parse_mode='html', reply_markup=markup)


def answer2():
    bot.send_message(Chat_user_id, "Текст")
    markup = types.InlineKeyboardMarkup(row_width=1)
    item1P = types.InlineKeyboardButton("Кнопка под сообщением", callback_data='watchsub')
    markup.add(item1P)
    bot.send_message(Chat_user_id, "Текст " ,parse_mode='html', reply_markup=markup)


def answer3():
    bot.send_message(Chat_user_id, 'Тескт')
    bot.send_message(Chat_user_id, 'Тескт')
    markup = types.InlineKeyboardMarkup(row_width=1)
    item1P = types.InlineKeyboardButton("Кнопка под сообщением", callback_data='nextquest2')
    markup.add(item1P)
    img = open('Description.jpg', 'rb')
    bot.send_photo(Chat_user_id, img,
                   caption="Тескт", parse_mode="Markdown", reply_markup=markup)


def answer4():
    bot.send_message(Chat_user_id, 'Текст')
    markup = types.InlineKeyboardMarkup(row_width=1)
    item1P = types.InlineKeyboardButton("Кнопка под сообщением", callback_data='nextquest3')
    markup.add(item1P)
    img = open('adPhoto.jpg', 'rb')
    bot.send_photo(Chat_user_id, img,
                   caption="Текст ", parse_mode="Markdown")

    bot.send_message(chat_user_id, 'Текст»', reply_markup=markup)


def proverka(..., ...):
    if ...
    ...
    else:
        answer3()


def is_subscribed(Chat_id_channel, User_id):
    try:
        bot.get_chat_member(Chat_id_channel, User_id)
        return True
    except ApiTelegramException as e:
        if e.result_json['description'] == 'Bad Request: user not found':
            return False


@bot.callback_query_handler(func=lambda call: True)
def check(call):
    global chat_user_id, name_user, user_id
    if call.message:
        if call.data == "want":
            answer1()
        elif call.data == "nextquest":
            answer2()
        elif call.data == "watchsub":
            proverka(chat_user_id, user_id)
        elif call.data == "nextquest2":
            answer4()
        elif call.data == "nextquest3":
            bot.send_message(chat_user_id, 'Текст')
        else:
            bot.send_message(chat_user_id, "Error".format(bot.get_me()))


# RUN
bot.polling(none_stop=True)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-08-10
@Vitaliy445

Delete all global variables, replacing them with at least the simplest database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question