Skip to content

Instantly share code, notes, and snippets.

@watzon
Created October 22, 2017 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save watzon/ad7776d5ec405e25235f5aa7af0dfbfb to your computer and use it in GitHub Desktop.
Save watzon/ad7776d5ec405e25235f5aa7af0dfbfb to your computer and use it in GitHub Desktop.
require "file"
require "telegram_bot"
require "./definebot/*"
TOKEN = File.read(File.join(__DIR__, "..", ".secret-token"))
module Definebot
class Bot < TelegramBot::Bot
include TelegramBot::CmdHandler
def initialize
super("SampleDefineBot", TOKEN)
cmd "start" { |msg, params| start_command msg, params }
cmd "define" { |msg, params| define_command msg, params }
cmd "listterms" { |msg, params| listterms_command msg, params }
end
def start_command(msg, params)
message = %q{Welcome to DefineBot
I am a test bot. Use the command /define in conjunction with a word. If I know the word I will define it for you.}
send_message msg.from.not_nil!.id, message
end
def define_command(msg, params)
word = params.join(" ").strip
from_id = msg.from.not_nil!.id
query = Repo::Query.where(word: params).limit(1)
terms = Repo.all(Models::Term, query)
if (terms.size > 0)
term = terms.first
response = "*#{word}*\n#{term.definition.not_nil!}"
send_message from_id, response, parse_mode: "markdown"
else
send_message from_id, "Sorry, I don't know what \"#{word}\" means"
end
end
def listterms_command(msg, params)
from_id = msg.from.not_nil!.id
terms = Repo.all(Models::Term)
terms.as(Array(Models::Term)) unless terms.nil?
if terms.nil?
return send_message from_id, "Sorry, but it seems as though no terms have been defined yet."
end
terms_list = terms.map { |t| t.word }.join("\n")
send_message from_id, "Here are all the terms I know:\n#{terms_list}"
end
end
end
bot = Definebot::Bot.new
bot.polling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment