사용자가 특정 텔레 그램 채널 (Python / PyTelegramBotApi)에 가입되어 있는지 확인하는 방법은 무엇입니까?

보르 초프 보그 단

내가 쓰고 전보 봇 은 Using PyTelegramBotApi 라이브러리를 , 내가 좋아하는 것 특정 전문 채널로 사용자의 가입을 확인하는 기능을 구현 하고, 존재하지 않는 경우는, 제안 구독 할 수 있습니다. 귀하의 답변에 미리 감사드립니다!

티 베베. 나는

getChatMember사용자가 채널의 회원인지 확인 하는 방법을 사용 하십시오.

getChatMember

이 방법을 사용하여 채팅 구성원에 대한 정보를 얻습니다. 성공시 ChatMember 개체를 반환합니다.

import telebot

bot = telebot.TeleBot("TOKEN")

CHAT_ID = -1001...
USER_ID = 700...

result = bot.get_chat_member(CHAT_ID, USER_ID)
print(result)

bot.polling()

샘플 결과 :

사용자가 회원 인 경우 사용자 정보를받습니다.

{'user': {'id': 700..., 'is_bot': False, 'first_name': '', 'username': None, 'last_name': None, ... }

또는 예외

telebot.apihelper.ApiTelegramException: A request to the Telegram API was unsuccessful. Error code: 400 Description: Bad Request: user not found

프로젝트 내에서 사용하는 방법에 대한 예

import telebot
from telebot.apihelper import ApiTelegramException

bot = telebot.TeleBot("BOT_TOKEN")

CHAT_ID = -1001...
USER_ID = 700...

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

if not is_subscribed(CHAT_ID, USER_ID):
    # user is not subscribed. send message to the user
    bot.send_message(CHAT_ID, 'Please subscribe to the channel')
else:
    # user is subscribed. continue with the rest of the logic
    # ...

bot.polling()

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관