Я сделал связку. Вот небольшой код для восприятия
TELEGRAM_API_TOKEN = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" #ваш API получать у @BotFather
TELEGRAM_CHAT_ID = 0 # временно, пока не авторизуется по дополнительному паролю
TELEGRAM_CHAT_PASSWORD_COUNT = 0
TELEGRAM_CHAT_PASSWORD = "xxxxxxxxxxxxx" #дополнительная защита
# Обработка команд
def startCommand(bot, update):
bot.send_message(chat_id=update.message.chat_id, text='Привет! Это приватный бот. Для работы необходимо авторизовываться')
def authCommand(bot, update):
global TELEGRAM_CHAT_PASSWORD_COUNT
bot.send_message(chat_id=update.message.chat_id, text='Для авторизации введите пароль.')
TELEGRAM_CHAT_PASSWORD_COUNT = 3
def startbotCommand(bot, update):
if TELEGRAM_CHAT_ID == update.message.chat_id:
bot.send_message(chat_id=update.message.chat_id, text='Команда запуска бота принята.')
start_bot()
else:
bot.send_message(chat_id=update.message.chat_id, text='Вы не авторизованы.')
def stopbotCommand(bot, update):
if TELEGRAM_CHAT_ID == update.message.chat_id:
bot.send_message(chat_id=update.message.chat_id, text='Команда остановки бота принята.')
stop_bot()
else:
bot.send_message(chat_id=update.message.chat_id, text='Вы не авторизованы.')
def restartbotCommand(bot, update):
if TELEGRAM_CHAT_ID == update.message.chat_id:
bot.send_message(chat_id=update.message.chat_id, text='Команда перезапуска бота принята.')
restart_bot()
else:
bot.send_message(chat_id=update.message.chat_id, text='Вы не авторизованы.')
def mywalletCommand(bot, update):
if TELEGRAM_CHAT_ID == update.message.chat_id:
bot.send_message(chat_id=update.message.chat_id, text='Команда по информаии моего кошелька принята.')
update_balance()
wallet_info()
else:
bot.send_message(chat_id=update.message.chat_id, text='Вы не авторизованы.')
def textMessage(bot, update):
global TELEGRAM_CHAT_PASSWORD_COUNT, TELEGRAM_CHAT_ID
if TELEGRAM_CHAT_PASSWORD_COUNT>0:
TELEGRAM_CHAT_PASSWORD_COUNT = TELEGRAM_CHAT_PASSWORD_COUNT - 1
if TELEGRAM_CHAT_PASSWORD==update.message.text:
TELEGRAM_CHAT_ID = update.message.chat_id
response = 'Вы успешно авторизовались!'
else:
response = 'Ошибка авторизации'
else:
response = 'Попытки исчерпаны'
bot.send_message(chat_id=update.message.chat_id, text=response)
def telegrambot():
global bot, update, updater, str_for_bot
# Хендлеры
start_command_handler = CommandHandler('start', startCommand)
auth_command_handler = CommandHandler('auth', authCommand)
startbot_command_handler = CommandHandler('startbot', startbotCommand)
stopbot_command_handler = CommandHandler('stopbot', stopbotCommand)
restartbot_command_handler = CommandHandler('restartbot', restartbotCommand)
mywallet_command_handler = CommandHandler('mywallet', mywalletCommand)
text_message_handler = MessageHandler(Filters.text, textMessage)
# Добавляем хендлеры в диспетчер
dispatcher.add_handler(start_command_handler)
dispatcher.add_handler(auth_command_handler)
dispatcher.add_handler(startbot_command_handler)
dispatcher.add_handler(stopbot_command_handler)
dispatcher.add_handler(restartbot_command_handler)
dispatcher.add_handler(mywallet_command_handler)
dispatcher.add_handler(text_message_handler)
# Начинаем поиск обновлений
updater.start_polling(clean=True)
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
updater = Updater(token=TELEGRAM_API_TOKEN) # Токен API к Telegram
dispatcher = updater.dispatcher
# init threads
t1 = threading.Thread(target=telegrambot)
# start threads
t1.start()
# join threads to the main thread
t1.join()
str_for_bot = "ТЕКСТ для отправки"
updater.bot.send_message(chat_id=TELEGRAM_CHAT_ID, text=str_for_bot)