bot.py:# -*- coding: utf-8 -*-
import sqlite3
import config
import telebot
import requests
import secrets
import json
import re
import time
from telebot import types
from datetime import datetime
import uuid
bot = telebot.TeleBot(config.token_bot)
@bot.message_handler(commands=['start'])
def get_document_query(message):
# CREATE REQUES
body = {"UserId": message.from_user.id, "UserName": message.from_user.username, "UserFirstName": message.from_user.first_name, "UserLastName": message.from_user.last_name, "ProjectKey": config.project_key}
us_r = requests.post('https://mindscan.ru/Outer/GetToken', data=body)
text = 'Введите поисковый запрос'
data = json.loads(us_r.text)
token = data.get('Jwt')
# SAVE TOKEN
with sqlite3.connect(config.db_name) as conn:
stmt = "INSERT INTO users (id, username , token) VALUES (Null, ?, ?)"
args = (message.from_user.username, token)
conn.execute(stmt, args)
conn.commit()
# SEND MESSAGE
msg = bot.send_message(message.chat.id, text)
# CREATE NEXT STEP
bot.register_next_step_handler(msg, query_confirm)
@bot.message_handler(regexp='подтвердить отчёт')
def query_confirm(message):
# CREATE TEXT
text = 'Ваш поисковый запрос: *' + message.text + '*'
keyboard = types.InlineKeyboardMarkup()
callback_button = types.InlineKeyboardButton(text="Подтвердить", callback_data="confirm")
keyboard.add(callback_button)
# SEND MESSAGE
bot.send_message(message.chat.id, text, parse_mode='Markdown', reply_markup=keyboard)
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
if call.message:
if call.data == "confirm":
# HEADERS
token = ''
with sqlite3.connect(config.db_name) as conn:
stmt = "SELECT token FROM users WHERE username=:login"
args = {'login': call.message.from_user.username}
conn.execute(stmt, args)
row = conn.fetchone()
token = row[0]['token']
bot.send(call.message.chat.id, token)
while True:
try:
if __name__ == '__main__':
print('Bot running..')
bot.polling(none_stop=True)
except Exception as ex:
print('Error - {}'.format(str(ex)))
print('Restarting..')
bot.stop_polling()
time.sleep(15)
print('Running again!')
bot.polling(none_stop=True)