import telebot
import threading
from tele_bot_users import *
# Constants to indicate steps while user is entering password
TEAM_USER_LOGGING = 0
TEAM_USER_ACCEPTED = 1
# Data structure for list of bot`s users
team_users = TeamUserList()
user_step = {}
user_active_dialog = {}
reply_data_db = {}
token = ""
bot = telebot.TeleBot(token)
print(bot.get_me())
def log(message, answer):
print("\n ----------")
from datetime import datetime
print(datetime.now())
print("Сообщения от {0} {1}. (id = {2}) \n Текст - {3}".format(message.from_user.first_name,
message.from_user.last_name,
str(message.from_user.id),
message.text))
@bot.message_handler(commands=['start'])
def handle_start(message):
user_markup = telebot.types.ReplyKeyboardMarkup(True, False)
user_markup.row('Есть вопрос')
bot.send_message(message.from_user.id, 'Доброго времени суток, уважаемый пользователь!', reply_markup=user_markup)
@bot.message_handler(commands=['help'])
def handle_text(message):
answer = """Опишите свою проблему"""
bot.send_message(message.chat.id, answer)
log(message, answer)
# Custom command to add user to an operator`s team
@bot.message_handler(commands=['on'])
def subscribe_chat(message):
answer = "Введите кодовую фразу:"
if message.chat.id in team_users:
bot.reply_to(message, "Вы уже оператор!")
log(message, answer)
else:
user_step[message.chat.id] = TEAM_USER_LOGGING
bot.reply_to(message, answer)
log(message, answer)
# Custom command to remove user from an operator`s team
@bot.message_handler(commands=['off'])
def team_user_logout(message):
answer = "Больше вы не получите ни одного сообщения"
if message.chat.id not in team_users:
bot.reply_to(message, "Вы итак не оператор")
log(message, answer)
else:
team_users.remove_by_chat_id(message.chat.id)
bot.reply_to(message, answer)
log(message, answer)
# Here we catch user message after '/on' command and interpret it as a password
@bot.message_handler(func=lambda message: user_step.get(message.chat.id) == TEAM_USER_LOGGING)
def team_user_login(message):
answer = "Неправильная кодовая фраза! Попробуйте еще раз!"
if message.text == '321':
team_users.add(TeamUser(message.chat.id))
user_step[message.chat.id] = TEAM_USER_ACCEPTED
bot.reply_to(message, "Вы стали оператором!")
log(message, answer)
else:
bot.reply_to(message, answer)
log(message, answer)
@bot.message_handler(content_types=["text"])
def handle_text(message):
answer = "Опишите свою проблему."
if message.text == "Есть вопрос":
answer = "Пожалуйста, опишите свою проблему"
log (message, answer)
bot.send_message(message.chat.id, "Пожалуйста, опишите свою проблему")
else:
bot.send_message(message.chat.id, answer)
log(message, answer)
# Use this function when you need to send something to an operators team
def process(message):
text = "%s\n%s пишет to %s\nОтвет: %s" %\
(message, 'Уася', 'Команда техподдержки', '*reply_url*')
for user in team_users:
bot.send_message(user.chat_id, text, disable_web_page_preview=True)
threading.Thread(target=bot.polling).start()
while True:
msg = input("Messages: ")
process(msg)