Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'telegram/bot'
- require 'mysql2'
- telegram_token = 'PLACE YOUR TELEGRAM BOT TOKEN HERE'
- db_host = 'localhost'
- db_username = 'YOUR DATABASE USERNAME'
- db_password = 'YOUR DATABASE PASSWORD'
- db_name = 'DATABASE NAME'
- command = nil
- table_name = nil
- object = {}
- Telegram::Bot::Client.run(telegram_token) do |bot|
- bot.listen do |message|
- if command.eql?('input')
- if table_name && object[:plot] && object[:ph_first] && object[:ph_last].nil?
- object[:ph_last] = message.text
- puts "ph_last: #{object[:ph_last]}"
- if table_name && object[:plot] && object[:ph_first] && object[:ph_last]
- client = Mysql2::Client.new(host: db_host, username: db_username, password: db_password, database: db_name)
- sql = "INSERT INTO #{table_name} (plot, ph_first, ph_last) VALUES ('#{object[:plot]}', '#{object[:ph_first]}', '#{object[:ph_last]}')"
- puts sql
- result = client.query(sql)
- puts result
- text = 'Data berhasil disimpan.'
- bot.api.send_message(chat_id: message.chat.id, text: text)
- command = nil
- table_name = nil
- object = {}
- end
- end
- if table_name && object[:plot] && object[:ph_first].nil?
- object[:ph_first] = message.text
- puts "ph_first: #{object[:ph_first]}"
- if table_name && object[:plot] && object[:ph_first] && object[:ph_last].nil?
- text = 'Masukan Ph sore:'
- bot.api.send_message(chat_id: message.chat.id, text: text)
- end
- end
- if table_name && object[:plot].nil?
- object[:plot] = message.text
- puts "plot: #{object[:plot]}"
- if table_name && object[:plot] && object[:ph_first].nil?
- text = 'Masukan Ph pagi:'
- bot.api.send_message(chat_id: message.chat.id, text: text)
- end
- end
- if command && table_name.nil?
- table_name = message.text
- puts "table name: #{table_name}"
- if table_name && object[:plot].nil?
- text = 'Masukan nama petak:'
- bot.api.send_message(chat_id: message.chat.id, text: text)
- end
- end
- end
- case message.text
- when '/start'
- bot.api.send_message(chat_id: message.chat.id, text: "Hello, #{message.from.first_name}")
- when '/input'
- text = 'Silahkan pilih tabel:'
- options = Telegram::Bot::Types::ReplyKeyboardMarkup.new(keyboard: [%w(pool)], one_time_keyboard: true)
- bot.api.send_message(chat_id: message.chat.id, text: text, reply_markup: options)
- command = 'input'
- when '/stop'
- bot.api.send_message(chat_id: message.chat.id, text: "Bye, #{message.from.first_name}")
- command = nil
- table_name = nil
- object = {}
- end
- end
- end
Add Comment
Please, Sign In to add comment