R
R
rolly002021-07-03 19:45:22
Python
rolly00, 2021-07-03 19:45:22

TypeError when clicking Telebot + MySQL button again?

Why do I get the error "Missing 1 required positional argument: 'prooblast'" when I click on the 'city' button again to show a vacancy. After all, the argument is passed to the Query function.

Actually code:

from logging import exception
import pymysql
import telebot
from telebot import types
bot = telebot.TeleBot("")

db = pymysql.connect(
    host='127.0.0.1',
    user='root', 
    password='12345',
    db='vakansii'
    )
    
cur = db.cursor()
table_value = {}
knowns_ids = []

class Table:
    def __init__(self, first):
        self.first = first
        self.city = None
        self.trebovaniya = None
        self.obyazonosti = None
        self.grafik = None
        self.oklad = None
        self.contacts = None
        self.region = None

@bot.message_handler(commands=['start'])
def region(message):
    markup = types.ReplyKeyboardMarkup(row_width=1, resize_keyboard=True)
    markup.add('HR/Рекрутинг', 'Программист', 'Дизайнер')
    msg = bot.send_message(message.chat.id,"Выберите профессиональную область:", reply_markup=markup)
    bot.register_next_step_handler(msg, gorod)

def gorod(message): # prog
    prooblast = message.text
    markup = types.ReplyKeyboardMarkup(row_width=1, resize_keyboard=True)
    markup.add('Москва', 'Омск','Санкт-Петербург','Назад')      
    msg = bot.send_message(message.chat.id,"Выберите город для поиска вакансий:", reply_markup=markup)
    bot.register_next_step_handler(msg, query, prooblast)

@bot.message_handler(commands=['add'])
def pull(message):
    if message.chat.id in knowns_ids:
        msg = bot.reply_to(message, "Название вакансии:")
        bot.register_next_step_handler(msg, process_first_step)       
    else:
        bot.reply_to(message,"Access denied")

def process_first_step(message):
    chat_id = message.chat.id
    first = message.text
    user = Table(first)
    table_value[chat_id] = user
    msg = bot.reply_to(message, 'Город:')
    bot.register_next_step_handler(msg, process_city_step)

def process_city_step(message):
    chat_id = message.chat.id
    city = message.text
    user = table_value[chat_id]
    user.city = city
    msg = bot.reply_to(message, "Требования:")
    bot.register_next_step_handler(msg, process_trebovaniya_step)

def process_trebovaniya_step(message):
    chat_id = message.chat.id
    trebovaniya = message.text
    user = table_value[chat_id]
    user.trebovaniya = trebovaniya
    msg = bot.reply_to(message, "Обязанности:")
    bot.register_next_step_handler(msg, process_obyazonosti_step)

def process_obyazonosti_step(message):
    chat_id = message.chat.id
    obyazonosti = message.text
    user = table_value[chat_id]
    user.obyazonosti = obyazonosti
    msg = bot.reply_to(message, "График работы:")
    bot.register_next_step_handler(msg, process_grafik_step)

def process_grafik_step(message):
    chat_id = message.chat.id
    grafik = message.text
    user = table_value[chat_id]
    user.grafik = grafik
    msg = bot.reply_to(message, "Оклад:")
    bot.register_next_step_handler(msg, process_oklad_step)

def process_oklad_step(message):
    chat_id = message.chat.id
    oklad = message.text
    user = table_value[chat_id]
    user.oklad = oklad
    msg = bot.reply_to(message, "Контакты:")
    bot.register_next_step_handler(msg, process_contacts_step)

def process_contacts_step(message):
    chat_id = message.chat.id
    contacts = message.text
    user = table_value[chat_id]
    user.contacts = contacts
    msg = bot.reply_to(message, "Область:")
    bot.register_next_step_handler(msg, process_region_step)
        
def process_region_step(message):
    try:
        chat_id = message.chat.id
        region = message.text
        user = table_value[chat_id]
        user.region = region
        sql = "INSERT INTO omsk (Вакансия, Город, Требования, Обязанности, График, Оклад, Контакты, Область) \
                    VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"
        val = (user.first, user.city, user.trebovaniya, user.obyazonosti, user.grafik, user.oklad, user.contacts, user.region)
        cur.execute(sql, val)
        db.commit()
        bot.reply_to(message,"Вакансия успешно добавлена")
    except:
        bot.reply_to(message, "Ошибка, попробуйте ещё раз")


@bot.message_handler(func=lambda m: True)
def query(message, prooblast):
    cur.execute(""" SELECT * FROM omsk WHERE `Город`= "{0}" AND `Область` = "{1}" """.format(message.text, prooblast))
    col = [i[0] for i in cur.description]
    for row in cur.fetchall():
        row_dict = {col[index]:value for (index, value) in enumerate(row)}
        dict = '\n'.join('✅ {0}: {1}'.format(key, val) for key, val in row_dict.items())
        bot.send_message(message.chat.id,'{}'.format(dict))
            
if __name__ == '__main__':
    bot.polling(none_stop=True)

Answer the question

In order to leave comments, you need to log in

5 answer(s)
B
Bl4ckm45k, 2021-07-03
@Bl4ckm45k

You should create keyboards with callback_data.
InlineKeyboardButton
Create a handler for the callbackquery
Where just every time the button is pressed, the handler will receive the city whose button was clicked.
CallbackQuery
In your code, the function accepts a city only once; you exited register_next_step_handler from which you passed prooblast

P
pcdesign, 2013-06-09
@pcdesign

For example, the provider "Akado" - prohibits the use of any DNS other than its own, incl. and 8.8.8.8
Perhaps you are also a "happy owner" of the same or similar provider :)
www.linux.org.ru/forum/general/5984250

I
Ilya Evseev, 2013-06-09
@IlyaEvseev

1) Access to other public DNS forwarders also disappears at these moments?
For example, to 4.2.2.2 and 77.88.8.8?
2) What does "TraceTCP 8.8.8.8 53" show?
Take from here: tracetcp.sf.net
Run with administrator rights.
3) Is there a firewall-antivirus on the computer?
If so, does disabling help?

P
Phillip Gruy, 2013-06-09
@DarkTwin

53 port outside the provider?
Set the DNS that your ISP offers you.

A
anclrej, 2013-06-11
@anclrej

You have Google IPv6 in the DNS server response. Try disabling the IPv6 protocol altogether on the connection to rule out problems with it.
In addition, you have the option to specify multiple DNS servers. Specify the provider's DNS first, and 8.8.8.8 second.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question