def call_back(call: telebot.types.CallbackQuery): if call.data == 'invoice': PRICE = telebot.types.LabeledPrice( label='Подписка на iMove: Ukraine на 1 месяц', amount=int(config.SUBS_COST + '99')) bot.send_invoice( chat_id=call.message.chat.id, title='Subscribe [1 month]', description=msg['invoice'], provider_token=config.PAY_TOKEN, currency='EUR', is_flexible= False, # True если конечная цена зависит от способа доставки prices=[PRICE], start_parameter='time-machine-example', invoice_payload='some-invoice-payload-for-our-internal-use') if call.data == 'answer': connection = Db().connect() chat_id = str(call.message.text).split( 'Сообщение от пользователя')[1].split('\n')[0].split('(')[0] question = str(call.message.text).split('\n-----------------\n') msg_id = connection.cursor().execute( 'SELECT id FROM messages WHERE chat_id=? AND message=?', (str(chat_id), str(question[1]))).fetchall()[0][0] manager = connection.cursor().execute( 'SELECT who_answer FROM messages WHERE id=?', (msg_id, )).fetchall() connection.close() if manager.__len__() != 1: connection = Db().connect() md_name = connection.cursor().execute( 'SELECT name FROM users WHERE chat_id=?', (manager[0][0], )).fetchall() connection.commit() print(manager.__len__(), manager) bot.send_message( chat_id=call.from_user.id, text=f'Модератор {md_name} уже отвечает на этот вопрос.') else: connection = Db().connect() connection.cursor().execute( 'UPDATE messages SET who_answer=? WHERE id=?', (str(call.from_user.id), str(msg_id))) connection.commit() connection.close() bot.send_message(chat_id=call.from_user.id, text='Теперь вы отвечаете на это сообщение') if call.data == 'history': connection = Db().connect() chat_id = str(call.message.text).split( 'Сообщение от пользователя')[1].split('\n')[0].split('(')[0] history = connection.cursor().execute( 'SELECT message, answer FROM messages WHERE chat_id=? AND status=?', (chat_id, 'Resolved')).fetchall() for pare in history: bot.send_message( chat_id=call.from_user.id, text=f'*Пользователь*:\n{pare[0]}\n*Модератор*:\n{pare[1]}', parse_mode='markdown')
def init(db_path): if (not utils.is_database_exists(db_path)): print('Database was not found. Do you want to create a new one?') a = input('Yes/no: ') if (a == 'no'): sys.exit(1) else: db = Db(db_path) db.init() db.close()
def save_name(message): connection = Db().connect() connection.cursor().execute( 'UPDATE users SET name=? WHERE chat_id=?', (message.text, message.chat.id)) connection.commit() connection.close() get_company = bot.send_message(chat_id=message.chat.id, text=msg['company']) bot.register_next_step_handler(get_company, save_company)
def save_company(message): connection = Db().connect() connection.cursor().execute( 'UPDATE users SET company=? WHERE chat_id=?', (message.text, message.chat.id)) connection.commit() connection.close() markup1 = telebot.types.InlineKeyboardMarkup(row_width=1) btn1 = telebot.types.InlineKeyboardButton(text='Получить доступ', callback_data='invoice') btn2 = telebot.types.InlineKeyboardButton(text='Договор оферты', callback_data='dogovor', url='https://google.com') btn3 = telebot.types.InlineKeyboardButton(text='О компании', callback_data='about', url='https://google.com') markup1.add(btn1, btn2, btn3) bot.send_message(chat_id=message.chat.id, text=msg['start'], reply_markup=markup1)
def contact_handler(message): try: connection = Db().connect() connection.cursor().execute( 'INSERT INTO users(chat_id, username, phone, date) VALUES (?,?,?,?)', (message.chat.id, message.from_user.username, message.contact.phone_number, datetime.now().strftime('%d.%m.%y'))) connection.commit() connection.close() markup = telebot.types.ReplyKeyboardRemove() get_name = bot.send_message(chat_id=message.chat.id, text=msg['name'], reply_markup=markup) bot.register_next_step_handler(get_name, save_name) except: bot.send_message( chat_id=message.chat.id, text= 'Вы не можете зарегистрироваться без username. Для того, чтобы его указать, перейдите в настройки вашего Telegram и укажите username' ) pass
def dialog(message: telebot.types.Message): connection = Db().connect() subs_end = connection.cursor().execute( 'SELECT subs_end FROM users WHERE chat_id=?', (message.chat.id, )).fetchall() connection.close() if subs_end.__len__() != 0: date = datetime.strptime(subs_end[0][0], '%d.%m.%y') if date < datetime.now(): markup = telebot.types.InlineKeyboardMarkup(row_width=1) btn1 = telebot.types.InlineKeyboardButton(text='Получить доступ', callback_data='invoice') markup.add(btn1) bot.send_message(chat_id=message.chat.id, text=msg['pay_invoice_back'], reply_markup=markup) else: for manager in config.MODER_ID: if message.chat.id != manager: markup = telebot.types.InlineKeyboardMarkup(row_width=1) btn1 = telebot.types.InlineKeyboardButton( text='Ответить', callback_data='answer') btn2 = telebot.types.InlineKeyboardButton( text='Показать историю', callback_data='history') markup.add(btn1, btn2) connection = Db().connect() username = connection.cursor().execute( 'SELECT name FROM users WHERE chat_id=?', (message.chat.id, )).fetchall()[0][0] connection.cursor().execute( 'INSERT INTO messages(chat_id, message, status) VALUES (?,?,?)', (message.chat.id, message.text, 'No answer')) connection.commit() connection.close() bot.send_message( chat_id=manager, text= f'Сообщение от пользователя {message.chat.id}({username})\n-----------------\n' + message.text, reply_markup=markup) else: try: connection = Db().connect() msg_id = connection.cursor().execute( 'SELECT id FROM messages WHERE who_answer=? AND status=?', (message.chat.id, 'No answer')).fetchall()[0][0] connection.cursor().execute( 'UPDATE messages SET answer=?, status=? WHERE id=?', (message.text, 'Resolved', msg_id)) chat_id = \ connection.cursor().execute('SELECT chat_id FROM messages WHERE id=?', (msg_id,)).fetchall()[0][ 0] connection.commit() connection.close() bot.send_message( chat_id=chat_id, text= f'Вы получили ответ от оператора:\n\n{message.text}' ) except: bot.send_message( chat_id=message.chat.id, text= 'Вы ни на что не отвечаете, для того, чтобы ответить на сообщение, нужно нажать на кнопку "Ответить под сообщением"' ) else: bot.send_message(chat_id=message, text=msg['subs_needed'])