import sqlite3
from sqlite3 import Error
from time import sleep, ctime
def post_sql_query(sql_query):
with sqlite3.connect('my.db') as connection:
cursor = connection.cursor()
try:
cursor.execute(sql_query)
except Error:
pass
result = cursor.fetchall()
return result
def create_tables():
users_query = '''CREATE TABLE IF NOT EXISTS USERS
(user_id INTEGER PRIMARY KEY NOT NULL,
username TEXT,
first_name TEXT,
last_name TEXT,
reg_date TEXT);'''
post_sql_query(users_query)
def register_user(user, username, first_name, last_name):
user_check_query = f'SELECT * FROM USERS WHERE user_id = {user};'
user_check_data = post_sql_query(user_check_query)
if not user_check_data:
insert_to_db_query = f'INSERT INTO USERS (user_id, username, first_name, last_name, reg_date) VALUES ({user}, "{username}", "{first_name}", "{last_name}", "{ctime()}");'
post_sql_query(insert_to_db_query )
create_tables() # вызываем функцию создания таблицы users
@bot.message_handler(commands=['start'])
def start(message):
register_user(message.from_user.id, message.from_user.username,
message.from_user.first_name, message.from_user.last_name)
bot.send_message(message.from_user.id, f'Welcome {message.from_user.first_name}' )
import telebot
from telebot import types
token = ""
bot = telebot.TeleBot(token)
contacs = {'sales': '8 (888) 888888, доб. 300',
'support': '8 (888) 888888, доб. 301',
'accounting': '8 (888) 888888, доб. 302',
'address': 'Наш адрес находится по адресу - адрес'}
@bot.message_handler(commands=['start'])
def handle_start(message):
keyboardmain = types.InlineKeyboardMarkup()
help = types.InlineKeyboardButton(text="Помощь", callback_data="help")
contacts = types.InlineKeyboardButton(text="Адрес офиса", callback_data="address")
site = types.InlineKeyboardButton(text="Перейти на сайт", callback_data="site", url='https://qna.habr.com/q/889613')
keyboardmain.add(help, contacts, site)
text = 'Добро пожаловать в Telegram бот компании "Лайт".'
bot.send_message(chat_id=message.chat.id, text=text, reply_markup=keyboardmain)
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
keyboardmain = types.InlineKeyboardMarkup()
help = types.InlineKeyboardButton(text="Помощь", callback_data="help")
contacts = types.InlineKeyboardButton(text="Адрес офиса", callback_data="address")
site = types.InlineKeyboardButton(text="Перейти на сайт", callback_data="site", url='https://qna.habr.com/q/889613')
buttons = [help, contacts, site]
text = contacs.get(call.data, None)
if call.data == 'help':
accounting = types.InlineKeyboardButton(text="Бухгалтерия", callback_data="accounting")
support = types.InlineKeyboardButton(text="Техподдержка", callback_data="support")
sales = types.InlineKeyboardButton(text="Отдел продаж", callback_data="sales")
back = types.InlineKeyboardButton(text="Назад", callback_data="back")
buttons = [accounting, support, sales, back]
text = 'Выберите пункт меню: '
if call.data == 'accounting':
back = types.InlineKeyboardButton(text="Назад", callback_data="back")
buttons.append(back)
if call.data == 'support':
back = types.InlineKeyboardButton(text="Назад", callback_data="back")
buttons.append(back)
if call.data == 'sales':
back = types.InlineKeyboardButton(text="Назад", callback_data="back")
buttons.append(back)
if call.data == 'address':
back = types.InlineKeyboardButton(text="Назад", callback_data="back")
buttons.append(back)
keyboardmain.add(*buttons)
bot.send_message(chat_id=call.message.chat.id, text=text, reply_markup=keyboardmain)
if __name__ == "__main__":
try:
bot.polling(none_stop=True)
except Exception as Error:
print(Error)