Class: TelegramBot::TelegramScrumBot

Inherits:
Object
  • Object
show all
Defined in:
lib/telegram-bot/bot.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#botObject (readonly)

Returns the value of attribute bot.



11
12
13
# File 'lib/telegram-bot/bot.rb', line 11

def bot
  @bot
end

#github_repositoryObject

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_usernameObject

Returns the value of attribute github_username.



10
11
12
# File 'lib/telegram-bot/bot.rb', line 10

def github_username
  @github_username
end

#loggerObject (readonly)

Returns the value of attribute logger.



11
12
13
# File 'lib/telegram-bot/bot.rb', line 11

def logger
  @logger
end

#user_messageObject (readonly)

Returns the value of attribute user_message.



11
12
13
# File 'lib/telegram-bot/bot.rb', line 11

def user_message
  @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_userObject



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_issuesObject



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?
    message = "Debe indicar el usuario de Github, puede hacerlo con /setgithubuser"
  elsif @github_repository.empty?
    message = "Debe indicar el repositorio de Github, puede hacerlo con /setgithubrepository"
  else
    begin
      github = GitHubWrapper::Repository.find(username:   @github_username, 
                                              repository: @github_repository)

      message = "Incidentes registrados en: @#{github.username}/#{github.repository}:\n\n"
      github.get_issues(state: 'open').each do |issue|
        if issue.instance_of?(GitHubWrapper::Issue)
          message << "  issue ##{issue.number}\n  #{issue.title}\n"
          message << "  -----\n\n"
        else
          message << issue
        end
      end
    rescue TypeError => e
      message << "User/Repository not found: #{e}"
    rescue StandardError => e
      message << "Error: #{e}"
    end
  end
  {
    bot_message:     message,
    require_answer:  false,
    answer_variable: nil
  }
end

#get_github_repositoryObject



81
82
83
84
85
86
87
88
# File 'lib/telegram-bot/bot.rb', line 81

def get_github_repository
  message = "La configuración establecida para el repositorio de github es: #{@github_repository}"
  {
    bot_message:     message,
    require_answer:  false,
    answer_variable: nil
  }
end

#get_github_userObject



63
64
65
66
67
68
69
70
# File 'lib/telegram-bot/bot.rb', line 63

def get_github_user
  message = "La configuración establecida para el usuario de github es: #{@github_username}"
  {
    bot_message:     message,
    require_answer:  false,
    answer_variable: nil
  }
end

#helpObject



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
  message = "Hola soy Scrum Hackathon Bot, puedo ayudarte consultando Issues de un repositorio y a sincronizarlos con un tablero de Trello.\n\n"
  message << "Puedes controlarme con los siguientes comandos:\n\n"
  message << "/start - mensaje de bienvenida\n"
  message << "/setup - asistente de configuración del bot\n\n"
  message << "Configuraciones:\n"
  message << "/setgithubuser - establece la configuración del usuario de github\n"
  message << "/setgithubrepository - establece la configuración del repositorio de github\n\n"
  message << "/getgithubuser - obtiene la configuración del usuario de github\n"    
  message << "/getgithubrepository - obtiene la configuración del repositorio de github\n\n"
  message << "Comandos:\n"
  message << "/issues - consultar el listado de Issues en el repositorio de Github\n"
  message << "/trello - sincroniza los issues del repositorio en un tablero de Trello\n"
  {
    bot_message:     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)
  message = "No tengo idea de lo que el comando #{command.inspect} significa."
  {
    bot_message:     message,
    require_answer:  false,
    answer_variable: nil
  }
end

#runObject



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 |user_message|
    @logger.info "@#{user_message.from.username}: #{user_message.text}"
    @user_message  = user_message
    command        = user_message.get_command_for(@bot)
    
    user_message.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 @#{user_message.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_repositoryObject



72
73
74
75
76
77
78
79
# File 'lib/telegram-bot/bot.rb', line 72

def set_github_repository
  message = "Por favor defina el repositorio de github a utilizar:"
  {
    bot_message:     message,
    require_answer:  true,
    answer_variable: :github_repository
  }
end

#set_github_userObject



54
55
56
57
58
59
60
61
# File 'lib/telegram-bot/bot.rb', line 54

def set_github_user
  message = "Por favor defina el usuario de github a utilizar:"
  {
    bot_message:     message,
    require_answer:  true,
    answer_variable: :github_username
  }
end

#setupObject



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
  message =  "Bienvenido al asistente de configuración del bot:\n\n"
  if @github_username.empty? || @github_repository.empty?
    if @github_username.empty?
      message << "Debe establecer el usuario de Github con /setgithubuser\n"
    end
    if @github_repository.empty?
      message << "Debe establecer el repositorio de Github con /setgithubrepository\n"
    end
  else
    message << "Todo se encuentra correctamente configurado, consulte /help para mas información\n"
  end
  {
    bot_message:     message,
    require_answer:  false,
    answer_variable: nil
  }
end

#startObject



26
27
28
29
30
31
32
33
# File 'lib/telegram-bot/bot.rb', line 26

def start
  message = "Hola #{@user_message.from.first_name}, para iniciar puedes utilizar el comando /setup."
  {
    bot_message:     message,
    require_answer:  false,
    answer_variable: nil
  }
end

#thanksObject



110
111
112
113
114
115
116
117
# File 'lib/telegram-bot/bot.rb', line 110

def thanks
  message = "You welcome!"
  {
    bot_message:     message,
    require_answer:  false,
    answer_variable: nil
  }
end

#update_trelloObject



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?
    message = "Debe indicar el usuario de Github, puede hacerlo con /setgithubuser"
  elsif github_repository.empty?
    message = "Debe indicar el repositorio de Github, puede hacerlo con /setgithubrepository"
  else
    trello = TelegramBot::TrelloConnector.new(username: @github_username, 
                                              repository: @github_repository)
    message = trello.show_statistics
  end
  {
    bot_message:     message,
    require_answer:  false,
    answer_variable: nil
  }
end