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 = 'YOUR DATABASE NAME'
- client = Mysql2::Client.new(host: db_host, username: db_username, password: db_password, database: db_name)
- command, flag, table_name = nil
- object = {}
- dictionary = {
- plot: "Petak",
- doc: "Doc",
- ph_first: "Ph Pagi",
- ph_last: "Ph Sore"
- }
- Telegram::Bot::Client.run(telegram_token) do |bot|
- bot.listen do |message|
- if command.eql?('input')
- if table_name && object[:plot] && object[:doc] && object[:ph_first] && object[:ph_last].nil?
- object[:ph_last] = message.text
- puts "ph_sore: #{object[:ph_last]}"
- if table_name && object[:plot] && object[:doc] && object[:ph_first] && object[:ph_last]
- time_at = Time.now
- 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}')"
- puts sql
- result = client.query(sql)
- puts result
- text = 'Data berhasil disimpan.'
- bot.api.send_message(chat_id: message.chat.id, text: text)
- # Reset var from previous command
- command, flag, table_name = nil
- object = {}
- end
- end
- if table_name && object[:plot] && object[:doc] && object[:ph_first].nil?
- object[:ph_first] = message.text
- puts "ph_pagi: #{object[:ph_first]}"
- if table_name && object[:plot] && object[:doc] && 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] && object[:doc].nil?
- object[:doc] = message.text
- puts "doc: #{object[:doc]}"
- if table_name && object[:plot] && object[:doc] && object[:ph_first].nil?
- text = 'Masukan Ph pagi:'
- 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[:doc].nil?
- text = 'Masukan Doc'
- 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
- if command.eql?('update')
- if table_name
- case message
- when Telegram::Bot::Types::CallbackQuery
- # Here you can handle your callbacks from inline buttons
- if message.data == 'cancel'
- # Reset var from previous command
- command, flag, table_name = nil
- object = {}
- bot.api.send_message(chat_id: message.from.id, text: "update selesai")
- else
- text = "Masukan nilai baru untuk #{dictionary[message.data.to_sym]}:"
- bot.api.send_message(chat_id: message.from.id, text: text)
- flag = message.data
- end
- when Telegram::Bot::Types::Message
- object[:id] = message.text if object[:id].nil?
- if flag
- time_at = Time.now
- sql = "UPDATE #{table_name} SET #{flag}='#{message.text}', updated_at='#{time_at}' WHERE id=#{object[:id].to_i}"
- client.query(sql)
- flag = nil
- text = "Data berhasil dirubah."
- bot.api.send_message(chat_id: message.from.id, text: text)
- end
- sql = "SELECT * FROM #{table_name} WHERE id=#{object[:id].to_i}"
- puts sql
- result = client.query(sql).first
- if flag.nil? && !result.nil?
- text = 'Pilih data yang akan diupdate:'
- kb = [
- Telegram::Bot::Types::InlineKeyboardButton.new(text: "Petak: #{result['plot']}", callback_data: 'plot'),
- Telegram::Bot::Types::InlineKeyboardButton.new(text: "Doc: #{result['doc']}", callback_data: 'doc'),
- Telegram::Bot::Types::InlineKeyboardButton.new(text: "Ph Pagi: #{result['ph_first']}", callback_data: 'ph_first'),
- Telegram::Bot::Types::InlineKeyboardButton.new(text: "Ph Sore: #{result['ph_last']}", callback_data: 'ph_last'),
- Telegram::Bot::Types::InlineKeyboardButton.new(text: "- Selesai -", callback_data: 'cancel')
- ]
- markup = Telegram::Bot::Types::InlineKeyboardMarkup.new(inline_keyboard: kb)
- bot.api.send_message(chat_id: message.chat.id, text: text, reply_markup: markup)
- end
- if result.nil?
- text = "Maaf data tidak ditemukan."
- bot.api.send_message(chat_id: message.chat.id, text: text)
- end
- end
- end
- if command && table_name.nil?
- table_name = message.text
- puts "table name: #{table_name}"
- if table_name && object[:id].nil?
- text = 'Masukan ID:'
- bot.api.send_message(chat_id: message.chat.id, text: text)
- end
- end
- end
- case message
- when Telegram::Bot::Types::Message
- case message.text
- when '/start'
- bot.api.send_message(chat_id: message.chat.id, text: "Hello, #{message.from.first_name}")
- when '/input'
- # Reset var from previous command
- command, flag, table_name = nil
- object = {}
- 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 '/update'
- # Reset var from previous command
- command, flag, table_name = nil
- object = {}
- 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 = 'update'
- when '/stop'
- bot.api.send_message(chat_id: message.chat.id, text: "Bye, #{message.from.first_name}")
- # Reset var from previous command
- command, flag, table_name = nil
- object = {}
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment