Advertisement
DesEX

Untitled

Apr 21st, 2020
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. import telebot
  2. import cfg
  3. import sqlite3
  4. from telebot import types
  5.  
  6. __connection = None
  7. bot = telebot.TeleBot(cfg.TOKEN)
  8.  
  9. #Подключение БД
  10. def get_connection():
  11.     global __connection
  12.     if __connection is None:
  13.         __connection = sqlite3.connect('data_base.db')
  14.     return __connection
  15.  
  16. #Инициализация ДБ
  17. def init_db(force:bool = False):
  18.  
  19.     connection = get_connection()   #Подключение
  20.     c = connection.cursor()
  21.  
  22.     if force == False:
  23.         c.execute('DROP TABLE IF EXISTS user_message') #Удаление ДБ в некоторых случаях
  24.    
  25.     c.execute('''
  26.         CREATE TABLE IF NOT EXISTS user_message(
  27.             id          INTEGER PRIMARY KEY,
  28.             user_id     INTEGER NOT NULL,
  29.             text        TEXT NOT NULL
  30.         )
  31.     ''') #Указание как заполнять ДБ
  32.  
  33.     #Сохранение изменений
  34.     connection.commit()
  35.  
  36. #Добавление сообщения в БД
  37. def add_message(user_id,text):
  38.     conn = get_connection()
  39.     c = conn.cursor()
  40.     c.execute('INSERT INTO user_message (user_id,text) VALUES (?,?)',(user_id,text))
  41.     conn.commit()
  42.  
  43. #Добавление сообщение в БД
  44. @bot.message_handler(content_types = ['text'])
  45. def msg_counter(message):
  46.     user = message.from_user
  47.     user_id = user.id
  48.     add_message(user_id,message)
  49.    
  50.  
  51.  
  52. #Приветствие
  53. @bot.message_handler(commands = ['start'])
  54. def Welcome(message):
  55.     stick = open('sticks/Hi_pikachu.tgs','rb')
  56.     bot.send_message(message.chat.id,'Приветствую тебя путник! Я - <b>Любимчик</b>, я призван для помощи <b>Никите</b> - моему лорду в обучении программированию телеграм ботов 😋',parse_mode = 'html')
  57.     bot.send_sticker(message.chat.id,stick)
  58.  
  59. #Болталка
  60. @bot.message_handler(content_types = ['text'])
  61. def speaking(message):
  62.     author = message.from_user
  63.     ctx = message.text
  64.     try:
  65.         if ctx.startswith('Как дела')or ctx.rindex('как дела'):
  66.             bot.send_message(message.chat.id,f'Спасибо, что спросил <b>{author.first_name}</b>, вроде пока не отключили меня - уже хорошо!',parse_mode = 'html')
  67.             bot.send_sticker(message.chat.id,open('sticks/ur_breathtaking.webp','rb'))
  68.             bot.send_message(message.chat.id,f'<b>{author.first_name}</b>, а у тебя как?',parse_mode='html')
  69.     except ValueError:
  70.         pass
  71.    
  72. #Старт бота
  73. init_db()
  74. bot.polling(none_stop = True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement