Answer the question
In order to leave comments, you need to log in
Telegram bot refuses to work (python)?
After starting through CMD, it gives the following message, while it does not show any signs of life:
main.py:68: TelegramDeprecationWarning: Old Handler API is deprecated - see https://git.io/fxJuV for details
updater = Updater(TOKEN, use_context=False)
TOKEN = '1164328268:AAH_XxrGnbeutPPKAyaxdwIO58xPg4EFIMA' # токен бота
AGREEMENT_TEXT = "SOGLASHENIYE"
AGREE_BUTTON_TEXT = "Соглашаюсь"
GET_CARD_BUTTON = "Получить карточку"
AGREE_KEYBOARD =
GET_CARD_KEYBOARD =
CAPTIONS = [
"Биологическая характеристика",
"Профессия",
"Человеческие качества",
"Состояние здоровья",
"Хобби",
"Доп. информация",
"Фобия",
"Багаж"
]
# 3 2 1 0
COUNT_DATA = [
[10, 8, 9, 3],
[18, 18, 15, 0],
[11, 8, 9, 0],
[6, 5, 7, 4],
[10, 11, 9, 0],
[8, 9, 7, 6],
[10, 8, 10, 2],
[6, 16, 8, 0]
]
DATA_STARTING_ROW = [2, 15, 35, 48, 57, 70, 81, 94]
import sqlite3
__connection = None
def get_connection():
global __connection
if __connection is None:
__connection = sqlite3.connect('main.db', check_same_thread=False)
return __connection
def create_table():
conn = get_connection()
crs = conn.cursor()
crs.execute("CREATE TABLE IF NOT EXISTS data(id INTEGER NOT NULL, agree BOOLEAN NOT NULL)")
conn.commit()
def reset_tables():
conn = get_connection()
crs = conn.cursor()
crs.execute("DROP TABLE data")
conn.commit()
create_table()
def is_registred(user_id):
conn = get_connection()
crs = conn.cursor()
crs.execute("SELECT COUNT(*) FROM data WHERE id = {0}".format(user_id))
return crs.fetchall()[0][0] > 0
def register(user_id):
conn = get_connection()
crs = conn.cursor()
crs.execute("INSERT INTO data(id, agree) VALUES({0}, 0)".format(user_id))
conn.commit()
def is_agree(user_id):
conn = get_connection()
crs = conn.cursor()
crs.execute("SELECT agree FROM data WHERE id = {0}".format(user_id))
return crs.fetchall()[0][0]
def set_agree(user_id):
conn = get_connection()
crs = conn.cursor()
crs.execute("UPDATE data SET agree = 1 WHERE id = {0}".format(user_id))
conn.commit()
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from telegram import ReplyKeyboardMarkup
import telegram
from config import *
import db
import xlrd
workbook = xlrd.open_workbook('main.xlsx')
worksheet = workbook.sheet_by_name('Лист3')
db.get_connection()
db.create_table()
def numbers_with_sum(n, k):
import random
if n == 1:
return [k]
num = random.randint(0, min(3, k))
return [num] + numbers_with_sum(n - 1, k - num)
def is_can_be(array):
if len(array) != 8:
return False
for i in range(8):
if COUNT_DATA[i][3 - array[i]] == 0:
return False
return True
def generate_card():
ans = []
while not is_can_be(ans):
ans = numbers_with_sum(8, 10)
card = "Вот ваша карточка: \n"
import random
for i in range(8):
row = DATA_STARTING_ROW[i] + random.randint(0, COUNT_DATA[i][3 - ans[i]] - 1)
text = str(worksheet.cell(row, 3 - ans[i])).split('\'')[1]
text = text.replace("\\xa0", ' ')
card += CAPTIONS[i] + ": " + text + "\n"
return card
def start(update: telegram.update, context: telegram.ext.CallbackContext):
user_id = context.message.chat.id
if not db.is_registred(user_id):
db.register(user_id)
if db.is_agree(user_id):
return
bot.send_message(user_id, AGREEMENT_TEXT, reply_markup=ReplyKeyboardMarkup(AGREE_KEYBOARD, resize_keyboard=True))
def new_message(update: telegram.update, context: telegram.ext.CallbackContext):
user_id = context.message.chat.id
text = context.message.text
if not db.is_agree(user_id):
if text == AGREE_BUTTON_TEXT:
db.set_agree(user_id)
bot.send_message(user_id, "Успешно!\nТеперь вы можете получить карточку по нажатию кнопки снизу!",
reply_markup=ReplyKeyboardMarkup(GET_CARD_KEYBOARD, resize_keyboard=True))
return
if text == GET_CARD_BUTTON:
bot.send_message(user_id, generate_card())
updater = Updater(TOKEN, use_context=False)
dp = updater.dispatcher
bot = updater.bot
dp.add_handler(CommandHandler("start", start))
dp.add_handler(MessageHandler(Filters.text, new_message))
updater.start_polling()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question