Guest User

Untitled

a guest
Dec 10th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. import sqlite3
  2.  
  3. # Database: chatDB.sqlite
  4. # Table: house_group_chatsimport requests
  5. import telegramimport requests
  6. import telegram
  7. from telegram.ext import (Updater, MessageHandler, Filters, ConversationHandler, CommandHandler, RegexHandler)
  8.  
  9. from settings import token
  10. from commands import *
  11. from stationInfo import all_password_regex
  12.  
  13.  
  14. # Initialisation
  15. bot = telegram.Bot(token)
  16. updater = Updater(token)
  17. dispatcher = updater.dispatcher
  18.  
  19. # Register commands
  20. registerCommand(dispatcher, "my_house_letters", my_house_letters)
  21. registerCommand(dispatcher, "all_house_letters", all_house_letters)
  22. registerCommand(dispatcher, "station_overview", station_overview)
  23. registerCommand(dispatcher, "help", help)
  24. registerCommand(dispatcher, "password", password)
  25.  
  26. # 'Start' conversation handler -- register house with bot
  27. start_conv_handler = ConversationHandler(
  28. entry_points=[CommandHandler('start', start)],
  29. states={
  30. CONFIRMATION_REQUEST: [RegexHandler('^(Ilykoie|Neo|Laventus|Oneiroi|Alerion|Rabanna)$', registerHouse)],
  31. },
  32. fallbacks=[CommandHandler('remove', remove), CommandHandler('cancel', cancel)]
  33. )
  34. dispatcher.add_handler(start_conv_handler)
  35.  
  36.  
  37. registerCommand(dispatcher, "play", play)
  38. play_handler = RegexHandler('^(1|2|3|4|5|6|7|8|9|10|11)$', get_instructions)
  39. dispatcher.add_handler(play_handler)
  40.  
  41.  
  42. # Message handler for station completion
  43. completion_handler = RegexHandler(all_password_regex, station_complete)
  44. dispatcher.add_handler(completion_handler)
  45.  
  46.  
  47. # Polling for updates
  48. updater.start_polling()
  49.  
  50. updater.idle()
  51.  
  52. from telegram.ext import (Updater, MessageHandler, Filters, ConversationHandler, CommandHandler, RegexHandler)
  53.  
  54. from settings import token
  55. from commands import *
  56. from stationInfo import all_password_regex
  57.  
  58.  
  59. # Initialisation
  60. bot = telegram.Bot(token)
  61. updater = Updater(token)
  62. dispatcher = updater.dispatcher
  63.  
  64. # Register commands
  65. registerCommand(dispatcher, "my_house_letters", my_house_letters)
  66. registerCommand(dispatcher, "all_house_letters", all_house_letters)
  67. registerCommand(dispatcher, "station_overview", station_overview)
  68. registerCommand(dispatcher, "help", help)
  69. registerCommand(dispatcher, "password", password)
  70.  
  71. # 'Start' conversation handler -- register house with bot
  72. start_conv_handler = ConversationHandler(
  73. entry_points=[CommandHandler('start', start)],
  74. states={
  75. CONFIRMATION_REQUEST: [RegexHandler('^(Ilykoie|Neo|Laventus|Oneiroi|Alerion|Rabanna)$', registerHouse)],
  76. },
  77. fallbacks=[CommandHandler('remove', remove), CommandHandler('cancel', cancel)]
  78. )
  79. dispatcher.add_handler(start_conv_handler)
  80.  
  81.  
  82. registerCommand(dispatcher, "play", play)
  83. play_handler = RegexHandler('^(1|2|3|4|5|6|7|8|9|10|11)$', get_instructions)
  84. dispatcher.add_handler(play_handler)
  85.  
  86.  
  87. # Message handler for station completion
  88. completion_handler = RegexHandler(all_password_regex, station_complete)
  89. dispatcher.add_handler(completion_handler)
  90.  
  91.  
  92. # Polling for updates
  93. updater.start_polling()
  94.  
  95. updater.idle()
  96.  
  97. # Columns: chat_id, house
  98.  
  99. class ChatDBHelper:
  100. def __init__(self, dbname="chatDB.sqlite"):
  101. self.dbname = dbname
  102. self.conn = sqlite3.connect(dbname, check_same_thread = False)
  103. self.c = self.conn.cursor()
  104.  
  105. def setup(self):
  106. self.conn.execute("DROP TABLE IF EXISTS house_group_chats ") #(for testing purposes)
  107. print("creating chat_id/house table (if it does not exist)")
  108. houses_table = "CREATE TABLE IF NOT EXISTS house_group_chats (chat_id integer, house text)"
  109. self.c.execute(houses_table)
  110. self.conn.commit()
  111.  
  112. def is_house_registered(self, house_name):
  113. stmt = "SELECT house from house_group_chats"
  114. registered = [x[0] for x in self.conn.execute(stmt)]
  115. if house_name in registered:
  116. return True
  117. else:
  118. return False
  119.  
  120. def is_chat_registered(self, chat):
  121. stmt = "SELECT chat_id from house_group_chats"
  122. registered = [x[0] for x in self.conn.execute(stmt)]
  123. for h in registered:
  124. if chat == h:
  125. return True
  126. return False
  127.  
  128. # === Setter ===
  129. def add_house(self, chat, house_name):
  130. stmt = "INSERT INTO house_group_chats (chat_id, house) VALUES (?, ?)"
  131. args = (chat, house_name)
  132. self.c.execute(stmt, args)
  133. self.conn.commit()
  134.  
  135. def remove_chat(self, chat):
  136. stmt = "DELETE FROM house_group_chats WHERE chat_id = (?)"
  137. args = (chat, )
  138. self.c.execute(stmt, args)
  139. self.conn.commit()
  140.  
  141. # === Getters ===
  142. def get_house(self, chat):
  143. stmt = "SELECT house FROM house_group_chats WHERE chat_id = (?)"
  144. args = (chat,)
  145. # return [x for x in self.conn.execute(stmt, args)]
  146. row = self.c.execute(stmt, args).fetchone()
  147. return row[0]
  148.  
  149.  
  150. def print_table(self):
  151. # stmt = "SELECT * FROM house_group_chats"
  152. # table = self.c.execute(stmt)
  153. # print(table)
  154. # self.c.execute("SELECT * FROM house_group_chats")
  155. rows = self.c.fetchall()
  156. for row in rows:
  157. print(row)
Add Comment
Please, Sign In to add comment