Class: TelegramBot::TelegramScrumBot
- Inherits:
-
Object
- Object
- TelegramBot::TelegramScrumBot
- Defined in:
- lib/telegram-bot/bot.rb
Instance Attribute Summary collapse
-
#bot ⇒ Object
readonly
Returns the value of attribute bot.
-
#github_repository ⇒ Object
Returns the value of attribute github_repository.
-
#github_username ⇒ Object
Returns the value of attribute github_username.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#user_message ⇒ Object
readonly
Returns the value of attribute user_message.
Class Method Summary collapse
Instance Method Summary collapse
- #get_answer_from_user ⇒ Object
- #get_github_issues ⇒ Object
- #get_github_repository ⇒ Object
- #get_github_user ⇒ Object
- #help ⇒ Object
-
#initialize(username:, repository:) ⇒ TelegramScrumBot
constructor
A new instance of TelegramScrumBot.
- #no_response_for(command) ⇒ Object
- #run ⇒ Object
- #set_github_repository ⇒ Object
- #set_github_user ⇒ Object
- #setup ⇒ Object
- #start ⇒ Object
- #thanks ⇒ Object
- #update_trello ⇒ Object
Constructor Details
#initialize(username:, repository:) ⇒ TelegramScrumBot
Returns a new instance of TelegramScrumBot.
17 18 19 20 21 22 23 24 |
# File 'lib/telegram-bot/bot.rb', line 17 def initialize(username:, repository:) @github_username = username @github_repository = repository @logger = Logger.new(STDOUT, Logger::DEBUG) @bot = TelegramBot.new(token: @@telegram_token, logger: @logger) @logger.debug "starting telegram bot" end |
Instance Attribute Details
#bot ⇒ Object (readonly)
Returns the value of attribute bot.
11 12 13 |
# File 'lib/telegram-bot/bot.rb', line 11 def bot @bot end |
#github_repository ⇒ Object
Returns the value of attribute github_repository.
10 11 12 |
# File 'lib/telegram-bot/bot.rb', line 10 def github_repository @github_repository end |
#github_username ⇒ Object
Returns the value of attribute github_username.
10 11 12 |
# File 'lib/telegram-bot/bot.rb', line 10 def github_username @github_username end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
11 12 13 |
# File 'lib/telegram-bot/bot.rb', line 11 def logger @logger end |
#user_message ⇒ Object (readonly)
Returns the value of attribute user_message.
11 12 13 |
# File 'lib/telegram-bot/bot.rb', line 11 def @user_message end |
Class Method Details
.set_token(token) ⇒ Object
13 14 15 |
# File 'lib/telegram-bot/bot.rb', line 13 def self.set_token(token) @@telegram_token = token end |
Instance Method Details
#get_answer_from_user ⇒ Object
177 178 179 180 181 182 183 184 185 186 |
# File 'lib/telegram-bot/bot.rb', line 177 def get_answer_from_user answer = @bot.get_updates(fail_silently: true).last @logger.info "@#{answer.from.username}: #{answer.text}" result = answer.text answer.reply do |reply| reply.text = "la configuracion ha sido actualizada a: #{result}" reply.send_with(bot) end result end |
#get_github_issues ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/telegram-bot/bot.rb', line 119 def get_github_issues if @github_username.empty? = "Debe indicar el usuario de Github, puede hacerlo con /setgithubuser" elsif @github_repository.empty? = "Debe indicar el repositorio de Github, puede hacerlo con /setgithubrepository" else begin github = GitHubWrapper::Repository.find(username: @github_username, repository: @github_repository) = "Incidentes registrados en: @#{github.username}/#{github.repository}:\n\n" github.get_issues(state: 'open').each do |issue| if issue.instance_of?(GitHubWrapper::Issue) << " issue ##{issue.number}\n #{issue.title}\n" << " -----\n\n" else << issue end end rescue TypeError => e << "User/Repository not found: #{e}" rescue StandardError => e << "Error: #{e}" end end { bot_message: , require_answer: false, answer_variable: nil } end |
#get_github_repository ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/telegram-bot/bot.rb', line 81 def get_github_repository = "La configuración establecida para el repositorio de github es: #{@github_repository}" { bot_message: , require_answer: false, answer_variable: nil } end |
#get_github_user ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/telegram-bot/bot.rb', line 63 def get_github_user = "La configuración establecida para el usuario de github es: #{@github_username}" { bot_message: , require_answer: false, answer_variable: nil } end |
#help ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/telegram-bot/bot.rb', line 90 def help = "Hola soy Scrum Hackathon Bot, puedo ayudarte consultando Issues de un repositorio y a sincronizarlos con un tablero de Trello.\n\n" << "Puedes controlarme con los siguientes comandos:\n\n" << "/start - mensaje de bienvenida\n" << "/setup - asistente de configuración del bot\n\n" << "Configuraciones:\n" << "/setgithubuser - establece la configuración del usuario de github\n" << "/setgithubrepository - establece la configuración del repositorio de github\n\n" << "/getgithubuser - obtiene la configuración del usuario de github\n" << "/getgithubrepository - obtiene la configuración del repositorio de github\n\n" << "Comandos:\n" << "/issues - consultar el listado de Issues en el repositorio de Github\n" << "/trello - sincroniza los issues del repositorio en un tablero de Trello\n" { bot_message: , require_answer: false, answer_variable: nil } end |
#no_response_for(command) ⇒ Object
168 169 170 171 172 173 174 175 |
# File 'lib/telegram-bot/bot.rb', line 168 def no_response_for(command) = "No tengo idea de lo que el comando #{command.inspect} significa." { bot_message: , require_answer: false, answer_variable: nil } end |
#run ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/telegram-bot/bot.rb', line 188 def run @bot.get_updates(fail_silently: true) do || @logger.info "@#{.from.username}: #{.text}" @user_message = command = .get_command_for(@bot) .reply do |reply| case command when /start/i result_hash = start when /setup/i result_hash = setup when /thanks/i result_hash = thanks when /setgithubuser/i result_hash = set_github_user when /getgithubuser/i result_hash = get_github_user when /setgithubrepository/i result_hash = set_github_repository when /getgithubrepository/i result_hash = get_github_repository when /help/i result_hash = help when /issues/i result_hash = get_github_issues when /trello/i result_hash = update_trello else result_hash = no_response_for(command) end reply.text = result_hash[:bot_message] puts "sending #{reply.text.inspect} to @#{.from.username}" reply.send_with(@bot) if result_hash[:require_answer] response = get_answer_from_user instance_variable_set("@#{result_hash[:answer_variable]}", response) end end end end |
#set_github_repository ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/telegram-bot/bot.rb', line 72 def set_github_repository = "Por favor defina el repositorio de github a utilizar:" { bot_message: , require_answer: true, answer_variable: :github_repository } end |
#set_github_user ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/telegram-bot/bot.rb', line 54 def set_github_user = "Por favor defina el usuario de github a utilizar:" { bot_message: , require_answer: true, answer_variable: :github_username } end |
#setup ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/telegram-bot/bot.rb', line 35 def setup = "Bienvenido al asistente de configuración del bot:\n\n" if @github_username.empty? || @github_repository.empty? if @github_username.empty? << "Debe establecer el usuario de Github con /setgithubuser\n" end if @github_repository.empty? << "Debe establecer el repositorio de Github con /setgithubrepository\n" end else << "Todo se encuentra correctamente configurado, consulte /help para mas información\n" end { bot_message: , require_answer: false, answer_variable: nil } end |
#start ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/telegram-bot/bot.rb', line 26 def start = "Hola #{@user_message.from.first_name}, para iniciar puedes utilizar el comando /setup." { bot_message: , require_answer: false, answer_variable: nil } end |
#thanks ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'lib/telegram-bot/bot.rb', line 110 def thanks = "You welcome!" { bot_message: , require_answer: false, answer_variable: nil } end |
#update_trello ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/telegram-bot/bot.rb', line 151 def update_trello if github_username.empty? = "Debe indicar el usuario de Github, puede hacerlo con /setgithubuser" elsif github_repository.empty? = "Debe indicar el repositorio de Github, puede hacerlo con /setgithubrepository" else trello = TelegramBot::TrelloConnector.new(username: @github_username, repository: @github_repository) = trello.show_statistics end { bot_message: , require_answer: false, answer_variable: nil } end |