S
S
swrvdm2022-01-14 12:38:22
Python
swrvdm, 2022-01-14 12:38:22

I can not import user verification for a subscription. How to decide?

I can’t make it so that when you click the “I subscribed” button, the subscription is checked, it automatically considers everyone that they are subscribed. And if you remove the bot from the admins of the channel to which the subscription is being made, then he considers everyone unsubscribed.

import telebot
from telebot.apihelper import ApiTelegramException
from telebot import types
import time

bot = telebot.TeleBot("Token")

CHAT_ID = '@channel_name'

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


@bot.message_handler(commands=['start'])
def start_message_handler(message):
    try:
        keyboard = types.InlineKeyboardMarkup(row_width=1)
        url_button = types.InlineKeyboardButton(text="ПОДПИСАТЬСЯ", url="Sylka")
        check_button = types.InlineKeyboardButton(text="Я ПОДПИСАЛСЯ", callback_data="check_subscribe")
        keyboard.add(url_button, check_button)
        bot.send_message(message.chat.id, "Для того чтобы посмотреть фильм, подпишься на канал!", reply_markup=keyboard)
    except:
        pass


@bot.callback_query_handler(func=lambda call: True)
def callback_query(call):
    try:
        if not is_subscribed(CHAT_ID, call.from_user.id):
            keyboard = types.InlineKeyboardMarkup(row_width=1)
            url_button = types.InlineKeyboardButton(text="ПОДПИСАТЬСЯ", url="Sylkaa")
            check_button = types.InlineKeyboardButton(text="Я ПОДПИСАЛСЯ", callback_data="check_subscribe")
            keyboard.add(url_button, check_button)
            bot.send_message(call.from_user.id, 'Не подписан, попробуй снова', reply_markup=keyboard)
        else:
            keyboard = types.InlineKeyboardMarkup()
            url_button_add = types.InlineKeyboardButton(text="СМОТРЕТЬ ФИЛЬМ", url="sylka")
            keyboard.add(url_button_add)
            bot.send_message(call.from_user.id, '', reply_markup=keyboard)
    except:
        pass



if __name__ == '__main__':
    try:
        bot.polling()
    except Exception as e:
        print(e)
        time.sleep(10)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2022-01-14
@SoreMix

Even if the user is not currently in the channel, the method get_chat_memberwill not throw an exception. This is true for those who left the channel (they were kicked or left themselves). So you still need to check the status for this case. In fact, we only need two - left and kicked. All statuses and types are described here - https://core.telegram.org/bots/api#chatmember

def is_subscribed(chat_id, user_id):
    try:
        user = bot.get_chat_member(chat_id, user_id)
        if user.status in ('left', 'kicked'):
            return False
        return True
    except ApiTelegramException as e:
        if e.result_json['description'] == 'Bad Request: user not found':
            return False

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question