Guest User

Untitled

a guest
Apr 21st, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. require 'telegram/bot'
  2. require 'mysql2'
  3.  
  4. telegram_token = 'PLACE YOUR TELEGRAM BOT TOKEN HERE'
  5.  
  6. db_host = 'localhost'
  7. db_username = 'YOUR DATABASE USERNAME'
  8. db_password = 'YOUR DATABASE PASSWORD'
  9. db_name = 'YOUR DATABASE NAME'
  10.  
  11. client = Mysql2::Client.new(host: db_host, username: db_username, password: db_password, database: db_name)
  12.  
  13. command, flag, table_name = nil
  14. object = {}
  15.  
  16. dictionary = {
  17. plot: "Petak",
  18. doc: "Doc",
  19. ph_first: "Ph Pagi",
  20. ph_last: "Ph Sore"
  21. }
  22.  
  23. Telegram::Bot::Client.run(telegram_token) do |bot|
  24. bot.listen do |message|
  25.  
  26. if command.eql?('input')
  27. if table_name && object[:plot] && object[:doc] && object[:ph_first] && object[:ph_last].nil?
  28. object[:ph_last] = message.text
  29. puts "ph_sore: #{object[:ph_last]}"
  30.  
  31. if table_name && object[:plot] && object[:doc] && object[:ph_first] && object[:ph_last]
  32. time_at = Time.now
  33. sql = "INSERT INTO #{table_name} (plot, doc, ph_first, ph_last, created_at, updated_at) VALUES ('#{object[:plot]}','#{object[:doc]}', '#{object[:ph_first]}', '#{object[:ph_last]}', '#{time_at}', '#{time_at}')"
  34. puts sql
  35.  
  36. result = client.query(sql)
  37. puts result
  38.  
  39. text = 'Data berhasil disimpan.'
  40. bot.api.send_message(chat_id: message.chat.id, text: text)
  41.  
  42. # Reset var from previous command
  43. command, flag, table_name = nil
  44. object = {}
  45. end
  46. end
  47.  
  48. if table_name && object[:plot] && object[:doc] && object[:ph_first].nil?
  49. object[:ph_first] = message.text
  50. puts "ph_pagi: #{object[:ph_first]}"
  51.  
  52. if table_name && object[:plot] && object[:doc] && object[:ph_first] && object[:ph_last].nil?
  53. text = 'Masukan Ph Sore:'
  54. bot.api.send_message(chat_id: message.chat.id, text: text)
  55. end
  56. end
  57.  
  58. if table_name && object[:plot] && object[:doc].nil?
  59. object[:doc] = message.text
  60. puts "doc: #{object[:doc]}"
  61.  
  62. if table_name && object[:plot] && object[:doc] && object[:ph_first].nil?
  63. text = 'Masukan Ph pagi:'
  64. bot.api.send_message(chat_id: message.chat.id, text: text)
  65. end
  66. end
  67.  
  68. if table_name && object[:plot].nil?
  69. object[:plot] = message.text
  70. puts "plot: #{object[:plot]}"
  71.  
  72. if table_name && object[:plot] && object[:doc].nil?
  73. text = 'Masukan Doc'
  74. bot.api.send_message(chat_id: message.chat.id, text: text)
  75. end
  76. end
  77.  
  78. if command && table_name.nil?
  79. table_name = message.text
  80. puts "table name: #{table_name}"
  81.  
  82. if table_name && object[:plot].nil?
  83. text = 'Masukan nama petak:'
  84. bot.api.send_message(chat_id: message.chat.id, text: text)
  85. end
  86. end
  87. end
  88.  
  89. if command.eql?('update')
  90. if table_name
  91. case message
  92. when Telegram::Bot::Types::CallbackQuery
  93. # Here you can handle your callbacks from inline buttons
  94. if message.data == 'cancel'
  95. # Reset var from previous command
  96. command, flag, table_name = nil
  97. object = {}
  98.  
  99. bot.api.send_message(chat_id: message.from.id, text: "update selesai")
  100. else
  101. text = "Masukan nilai baru untuk #{dictionary[message.data.to_sym]}:"
  102. bot.api.send_message(chat_id: message.from.id, text: text)
  103.  
  104. flag = message.data
  105. end
  106. when Telegram::Bot::Types::Message
  107. object[:id] = message.text if object[:id].nil?
  108.  
  109. if flag
  110. time_at = Time.now
  111. sql = "UPDATE #{table_name} SET #{flag}='#{message.text}', updated_at='#{time_at}' WHERE id=#{object[:id].to_i}"
  112. client.query(sql)
  113.  
  114. flag = nil
  115.  
  116. text = "Data berhasil dirubah."
  117. bot.api.send_message(chat_id: message.from.id, text: text)
  118. end
  119.  
  120. sql = "SELECT * FROM #{table_name} WHERE id=#{object[:id].to_i}"
  121. puts sql
  122. result = client.query(sql).first
  123.  
  124. if flag.nil? && !result.nil?
  125. text = 'Pilih data yang akan diupdate:'
  126. kb = [
  127. Telegram::Bot::Types::InlineKeyboardButton.new(text: "Petak: #{result['plot']}", callback_data: 'plot'),
  128. Telegram::Bot::Types::InlineKeyboardButton.new(text: "Doc: #{result['doc']}", callback_data: 'doc'),
  129. Telegram::Bot::Types::InlineKeyboardButton.new(text: "Ph Pagi: #{result['ph_first']}", callback_data: 'ph_first'),
  130. Telegram::Bot::Types::InlineKeyboardButton.new(text: "Ph Sore: #{result['ph_last']}", callback_data: 'ph_last'),
  131. Telegram::Bot::Types::InlineKeyboardButton.new(text: "- Selesai -", callback_data: 'cancel')
  132. ]
  133. markup = Telegram::Bot::Types::InlineKeyboardMarkup.new(inline_keyboard: kb)
  134. bot.api.send_message(chat_id: message.chat.id, text: text, reply_markup: markup)
  135. end
  136.  
  137. if result.nil?
  138. text = "Maaf data tidak ditemukan."
  139. bot.api.send_message(chat_id: message.chat.id, text: text)
  140. end
  141. end
  142. end
  143.  
  144. if command && table_name.nil?
  145. table_name = message.text
  146. puts "table name: #{table_name}"
  147.  
  148. if table_name && object[:id].nil?
  149. text = 'Masukan ID:'
  150. bot.api.send_message(chat_id: message.chat.id, text: text)
  151. end
  152. end
  153. end
  154.  
  155. case message
  156. when Telegram::Bot::Types::Message
  157. case message.text
  158. when '/start'
  159. bot.api.send_message(chat_id: message.chat.id, text: "Hello, #{message.from.first_name}")
  160. when '/input'
  161. # Reset var from previous command
  162. command, flag, table_name = nil
  163. object = {}
  164.  
  165. text = 'Silahkan pilih tabel:'
  166. options = Telegram::Bot::Types::ReplyKeyboardMarkup.new(keyboard: [%w(pool)], one_time_keyboard: true)
  167. bot.api.send_message(chat_id: message.chat.id, text: text, reply_markup: options)
  168.  
  169. command = 'input'
  170. when '/update'
  171. # Reset var from previous command
  172. command, flag, table_name = nil
  173. object = {}
  174.  
  175. text = 'Silahkan pilih tabel:'
  176. options = Telegram::Bot::Types::ReplyKeyboardMarkup.new(keyboard: [%w(pool)], one_time_keyboard: true)
  177. bot.api.send_message(chat_id: message.chat.id, text: text, reply_markup: options)
  178.  
  179. command = 'update'
  180. when '/stop'
  181. bot.api.send_message(chat_id: message.chat.id, text: "Bye, #{message.from.first_name}")
  182.  
  183. # Reset var from previous command
  184. command, flag, table_name = nil
  185. object = {}
  186. end
  187. end
  188. end
  189. end
Add Comment
Please, Sign In to add comment