Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import telebot
- import cfg
- import sqlite3
- from telebot import types
- __connection = None
- bot = telebot.TeleBot(cfg.TOKEN)
- #Подключение БД
- def get_connection():
- global __connection
- if __connection is None:
- __connection = sqlite3.connect('data_base.db')
- return __connection
- #Инициализация ДБ
- def init_db(force:bool = False):
- connection = get_connection() #Подключение
- c = connection.cursor()
- if force == False:
- c.execute('DROP TABLE IF EXISTS user_message') #Удаление ДБ в некоторых случаях
- c.execute('''
- CREATE TABLE IF NOT EXISTS user_message(
- id INTEGER PRIMARY KEY,
- user_id INTEGER NOT NULL,
- text TEXT NOT NULL
- )
- ''') #Указание как заполнять ДБ
- #Сохранение изменений
- connection.commit()
- #Добавление сообщения в БД
- def add_message(user_id,text):
- conn = get_connection()
- c = conn.cursor()
- c.execute('INSERT INTO user_message (user_id,text) VALUES (?,?)',(user_id,text))
- conn.commit()
- #Добавление сообщение в БД
- @bot.message_handler(content_types = ['text'])
- def msg_counter(message):
- user = message.from_user
- user_id = user.id
- add_message(user_id,message)
- #Приветствие
- @bot.message_handler(commands = ['start'])
- def Welcome(message):
- stick = open('sticks/Hi_pikachu.tgs','rb')
- bot.send_message(message.chat.id,'Приветствую тебя путник! Я - <b>Любимчик</b>, я призван для помощи <b>Никите</b> - моему лорду в обучении программированию телеграм ботов 😋',parse_mode = 'html')
- bot.send_sticker(message.chat.id,stick)
- #Болталка
- @bot.message_handler(content_types = ['text'])
- def speaking(message):
- author = message.from_user
- ctx = message.text
- try:
- if ctx.startswith('Как дела')or ctx.rindex('как дела'):
- bot.send_message(message.chat.id,f'Спасибо, что спросил <b>{author.first_name}</b>, вроде пока не отключили меня - уже хорошо!',parse_mode = 'html')
- bot.send_sticker(message.chat.id,open('sticks/ur_breathtaking.webp','rb'))
- bot.send_message(message.chat.id,f'<b>{author.first_name}</b>, а у тебя как?',parse_mode='html')
- except ValueError:
- pass
- #Старт бота
- init_db()
- bot.polling(none_stop = True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement