class Bot < ActiveRecord::Base belongs_to :user belongs_to :profile, optional: true # def send_text # if self.telegram_api_token # Telegram::Bot::Client.run(telegram_api_token) do |bot| # bot.api.send_message(chat_id: message.chat.id, text: response_text, parse_mode: "Markdown") # end # end # end def fortune `fortune -a` end def telegram_listen # `echo "#{Time.now} hi from telegram listen" > tglisten.txt` return unless self.telegram_api_token bot = Telegram::Bot::Client.new(telegram_api_token) # Signal.trap('INT') do # bot.stop # end bot.listen do |message| # TODO Logging # File.open("./logs/#{@botname}.log", 'a') { |f| f.write(["\n", Time.now, message.chat.id, message.from.id, message.from.first_name, message].join("\t")) } case message when Telegram::Bot::Types::Message # Debugging use next to skip/drop the queue # next case message.text when /^\/fortune/i response_text = `fortune -a` begin bot.api.send_message(chat_id: message.chat.id, reply_to_message_id: message.message_id, text: response_text, parse_mode: "Markdown") rescue puts "Error from fortune" end end end end end end