def linkChannel(update, context): text = update.message.text link = text.split()[-1] try: data = BOT.sendTo(f'@{link}', INTRO_MESSAGE) channel_id = data['chat']['id'] if channel_id: BOT.setChannelID(channel_id) with open('resource/channel_id.txt', 'w') as file: file.write(str(channel_id)) update.message.reply_text(f"{BOT.name()} is linked 't.me/{link}'") except Exception as e: errorMessage = f"CANNOT link 't.me/{link}'. ({e})\n" \ f"1. Is the channel PUBLIC?\n" \ f"2. Is the channel NAME CORRECT?\n" \ f"3. Is {BOT.name()} a MEMBER of the channel?" logger.info(errorMessage) update.message.reply_text(errorMessage)
CHAT_ID = load('resource/user_chat_id.txt') if CHAT_ID: BOT.setChatID(CHAT_ID) else: logger.info('No resource/user_chat_id.txt') BOT.hello() id = BOT.chat_id() save('resource/user_chat_id.txt', id) # channel_id 획득, 없으면 무시 CHANNEL_ID = load('resource/channel_id.txt') if (CHANNEL_ID is not None) or (not CHANNEL_ID == ''): BOT.setChannelID(CHANNEL_ID) # channel_id 유효한지 확인 try: BOT.sendTo(CHANNEL_ID, INTRO_MESSAGE) except Exception as e: logger.info(f'Invaild Channel id: {e}') file = 'resource/channel_id.txt' if os.path.isfile(file): logger.info(f'remove file: \'{file}\'') os.remove(file) else: logger.info(f'No channel id') from CommandFunction import * BOT.add_handler('help', help) BOT.add_handler('h', help) BOT.add_handler('link', linkChannel) BOT.add_handler('unlink', unlinkChannel) BOT.add_handler('rename', setName)