Guest User

Untitled

a guest
Apr 20th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 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 = 'DATABASE NAME'
  10.  
  11. command = nil
  12. table_name = nil
  13. object = {}
  14.  
  15. Telegram::Bot::Client.run(telegram_token) do |bot|
  16. bot.listen do |message|
  17.  
  18. if command.eql?('input')
  19. if table_name && object[:plot] && object[:ph_first] && object[:ph_last].nil?
  20. object[:ph_last] = message.text
  21. puts "ph_last: #{object[:ph_last]}"
  22.  
  23. if table_name && object[:plot] && object[:ph_first] && object[:ph_last]
  24. client = Mysql2::Client.new(host: db_host, username: db_username, password: db_password, database: db_name)
  25. sql = "INSERT INTO #{table_name} (plot, ph_first, ph_last) VALUES ('#{object[:plot]}', '#{object[:ph_first]}', '#{object[:ph_last]}')"
  26. puts sql
  27. result = client.query(sql)
  28.  
  29. puts result
  30. text = 'Data berhasil disimpan.'
  31. bot.api.send_message(chat_id: message.chat.id, text: text)
  32.  
  33. command = nil
  34. table_name = nil
  35. object = {}
  36. end
  37. end
  38.  
  39. if table_name && object[:plot] && object[:ph_first].nil?
  40. object[:ph_first] = message.text
  41. puts "ph_first: #{object[:ph_first]}"
  42.  
  43. if table_name && object[:plot] && object[:ph_first] && object[:ph_last].nil?
  44. text = 'Masukan Ph sore:'
  45. bot.api.send_message(chat_id: message.chat.id, text: text)
  46. end
  47. end
  48.  
  49. if table_name && object[:plot].nil?
  50. object[:plot] = message.text
  51. puts "plot: #{object[:plot]}"
  52.  
  53. if table_name && object[:plot] && object[:ph_first].nil?
  54. text = 'Masukan Ph pagi:'
  55. bot.api.send_message(chat_id: message.chat.id, text: text)
  56. end
  57. end
  58.  
  59. if command && table_name.nil?
  60. table_name = message.text
  61. puts "table name: #{table_name}"
  62.  
  63. if table_name && object[:plot].nil?
  64. text = 'Masukan nama petak:'
  65. bot.api.send_message(chat_id: message.chat.id, text: text)
  66. end
  67. end
  68. end
  69.  
  70. case message.text
  71. when '/start'
  72. bot.api.send_message(chat_id: message.chat.id, text: "Hello, #{message.from.first_name}")
  73. when '/input'
  74. text = 'Silahkan pilih tabel:'
  75. options = Telegram::Bot::Types::ReplyKeyboardMarkup.new(keyboard: [%w(pool)], one_time_keyboard: true)
  76. bot.api.send_message(chat_id: message.chat.id, text: text, reply_markup: options)
  77.  
  78. command = 'input'
  79. when '/stop'
  80. bot.api.send_message(chat_id: message.chat.id, text: "Bye, #{message.from.first_name}")
  81.  
  82. command = nil
  83. table_name = nil
  84. object = {}
  85. end
  86. end
  87. end
Add Comment
Please, Sign In to add comment