what is the best way to find out if the bot is the member of a Telegram group using pytelegrambotapi?
bot.send_message(GroupID,"test")
it returns an error if bot is not in the group but i don't want check it this way
what is the best way to find out if the bot is the member of a Telegram group using pytelegrambotapi?
bot.send_message(GroupID,"test")
it returns an error if bot is not in the group but i don't want check it this way
You can handle the error with try | except
,every error in python returns a specific exception, and the name of the exception, you can handle this and use the name of the exception and know specifically the error.
2023-04-29 02:43:06,787 (__init__.py:1083 MainThread) ERROR - TeleBot: "Threaded polling exception: A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: chat not found"
2023-04-29 02:43:06,788 (__init__.py:1085 MainThread) ERROR - TeleBot: "Exception traceback:
Traceback (most recent call last):
File "/home/vinicius/.local/lib/python3.10/site-packages/telebot/__init__.py", line 1074, in __threaded_polling
self.worker_pool.raise_exceptions()
File "/home/vinicius/.local/lib/python3.10/site-packages/telebot/util.py", line 147, in raise_exceptions
raise self.exception_info
File "/home/vinicius/.local/lib/python3.10/site-packages/telebot/util.py", line 90, in run
task(*args, **kwargs)
File "/home/vinicius/.local/lib/python3.10/site-packages/telebot/__init__.py", line 6720, in _run_middlewares_and_handler
result = handler['function'](message)
File "/home/vinicius/Scripts/bot.py", line 11, in Start
bot.send_message(groupid, 'Beep!!!')
File "/home/vinicius/.local/lib/python3.10/site-packages/telebot/__init__.py", line 1549, in send_message
apihelper.send_message(
File "/home/vinicius/.local/lib/python3.10/site-packages/telebot/apihelper.py", line 264, in send_message
return _make_request(token, method_url, params=payload, method='post')
File "/home/vinicius/.local/lib/python3.10/site-packages/telebot/apihelper.py", line 162, in _make_request
json_result = _check_result(method_name, result)
File "/home/vinicius/.local/lib/python3.10/site-packages/telebot/apihelper.py", line 189, in _check_result
raise ApiTelegramException(method_name, result, result_json)
telebot.apihelper.ApiTelegramException: A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: chat not found
in the last line we can see the exception telebot.apihelper.ApiTelegramException
. it's not a specific exception, but it's possible to handle it.
@bot.message_handler(commands=['start'])
def Start(message):
GROUPID = '-123456789'
try:
bot.send_message(GROUPID, 'Beep!!!')
except telebot.apihelper.ApiTelegramException as err:
string = str(err)[91:]
if 'chat not found' in string:
print('Group not found')
bot.infinity_polling()
except
, but in your case, I think it's the only way. I hope it helped you.1: Get the bot ID.
2: Get the chat ID of the group.
3: Check if the bot is a chat member using "getChatMember" or "getDefaultAdmninistratorRights".