diff --git a/2018/YuliyaBondareva/2/.gitignore b/2018/YuliyaBondareva/2/.gitignore new file mode 100644 index 000000000..1bc8dbe97 --- /dev/null +++ b/2018/YuliyaBondareva/2/.gitignore @@ -0,0 +1,4 @@ +settings.yml +.bundle/ +chat_ids/* +1/* \ No newline at end of file diff --git a/2018/YuliyaBondareva/2/Gemfile b/2018/YuliyaBondareva/2/Gemfile new file mode 100644 index 000000000..b268c68f6 --- /dev/null +++ b/2018/YuliyaBondareva/2/Gemfile @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +source "https://rubygems.org" +gem 'telegram-bot-ruby' +gem 'github_api' +gem 'redis' +gem 'octokit', :git => 'https://github.com/octokit/octokit.rb' diff --git a/2018/YuliyaBondareva/2/Gemfile.lock b/2018/YuliyaBondareva/2/Gemfile.lock new file mode 100644 index 000000000..034f01598 --- /dev/null +++ b/2018/YuliyaBondareva/2/Gemfile.lock @@ -0,0 +1,70 @@ +GIT + remote: https://github.com/octokit/octokit.rb + revision: f2a5ff176447afc61caa0e0d95b0cd02c031512f + specs: + octokit (4.8.0) + sawyer (~> 0.8.0, >= 0.5.3) + +GEM + remote: https://rubygems.org/ + specs: + addressable (2.5.2) + public_suffix (>= 2.0.2, < 4.0) + axiom-types (0.1.1) + descendants_tracker (~> 0.0.4) + ice_nine (~> 0.11.0) + thread_safe (~> 0.3, >= 0.3.1) + coercible (1.0.0) + descendants_tracker (~> 0.0.1) + descendants_tracker (0.0.4) + thread_safe (~> 0.3, >= 0.3.1) + equalizer (0.0.11) + faraday (0.12.2) + multipart-post (>= 1.2, < 3) + github_api (0.18.2) + addressable (~> 2.4) + descendants_tracker (~> 0.0.4) + faraday (~> 0.8) + hashie (~> 3.5, >= 3.5.2) + oauth2 (~> 1.0) + hashie (3.5.7) + ice_nine (0.11.2) + inflecto (0.0.2) + jwt (1.5.6) + multi_json (1.13.1) + multi_xml (0.6.0) + multipart-post (2.0.0) + oauth2 (1.4.0) + faraday (>= 0.8, < 0.13) + jwt (~> 1.0) + multi_json (~> 1.3) + multi_xml (~> 0.5) + rack (>= 1.2, < 3) + public_suffix (3.0.2) + rack (2.0.4) + redis (4.0.1) + sawyer (0.8.1) + addressable (>= 2.3.5, < 2.6) + faraday (~> 0.8, < 1.0) + telegram-bot-ruby (0.8.6.1) + faraday + inflecto + virtus + thread_safe (0.3.6) + virtus (1.0.5) + axiom-types (~> 0.1) + coercible (~> 1.0) + descendants_tracker (~> 0.0, >= 0.0.3) + equalizer (~> 0.0, >= 0.0.9) + +PLATFORMS + ruby + +DEPENDENCIES + github_api + octokit! + redis + telegram-bot-ruby + +BUNDLED WITH + 1.16.1 diff --git a/2018/YuliyaBondareva/2/lib/base.rb b/2018/YuliyaBondareva/2/lib/base.rb new file mode 100644 index 000000000..f3560ad09 --- /dev/null +++ b/2018/YuliyaBondareva/2/lib/base.rb @@ -0,0 +1,17 @@ +require "redis" +class Base + attr_accessor :bot, :user_id, :messages_array, :last_message + + def initialize(bot, message_chat_id) + @bot = bot + @user_id = message_chat_id + end + + def telegram_send_message(text, answers = nil) + if answers.nil? + @bot.api.send_message(chat_id: @user_id, text: text, parse_mode: 'Markdown') + else + @bot.api.send_message(chat_id: @user_id, text: text, parse_mode: 'Markdown', reply_markup: answers) + end + end +end diff --git a/2018/YuliyaBondareva/2/lib/help.rb b/2018/YuliyaBondareva/2/lib/help.rb new file mode 100644 index 000000000..956e13c02 --- /dev/null +++ b/2018/YuliyaBondareva/2/lib/help.rb @@ -0,0 +1,12 @@ +require_relative 'base' + +class Help < Base + def send_messages + telegram_send_message('Hello. See what I\'m doing + /help + /set\_repo + /show\_repo + /search + /reset') + end +end diff --git a/2018/YuliyaBondareva/2/lib/history.rb b/2018/YuliyaBondareva/2/lib/history.rb new file mode 100644 index 000000000..b2d896a6f --- /dev/null +++ b/2018/YuliyaBondareva/2/lib/history.rb @@ -0,0 +1,9 @@ +require_relative 'base' + +class History < Base + def send_messages + lines = IO.readlines("./chat_ids/#{@user_id}_history") + last = lines.last(10) + telegram_send_message(last.join('')) + end +end diff --git a/2018/YuliyaBondareva/2/lib/search.rb b/2018/YuliyaBondareva/2/lib/search.rb new file mode 100644 index 000000000..d60fd11bf --- /dev/null +++ b/2018/YuliyaBondareva/2/lib/search.rb @@ -0,0 +1,38 @@ +require_relative 'base' +require 'octokit' +require 'yaml' + +SETTINGS = YAML.load(File.open('settings.yml')) + + +class Search < Base + def get_repo + url_repo = File.open("./chat_ids/#{@user_id}") {|f| f.readline} + url_repo.gsub(/http(s)?:\/\/github.com\//, '') + end + + def add_history(query) + File.open("./chat_ids/#{@user_id}_history", 'a') do |f| + f.puts query + end + end + + def commits(query) + client = Octokit::Client.new(:login => SETTINGS['login'], :password => SETTINGS['password']) + repo = get_repo + if repo.nil? + telegram_send_message("Your don't set repo!") + else + commits = client.search_commits("repo:#{repo} #{query}", {page: 1, per_page: 10}) + add_history(query) + total_count = commits[:total_count] + commits_all = [] + commits[:items].each_with_index do |commit, index| + commits_all << "#{index}. #{commit[:commit][:message].gsub("\n", ' ')} -> [Link to commit](#{commit[:commit][:url]})" + end + + telegram_send_message(commits_all.join("\n")) + telegram_send_message("*Results #{commits_all.size} from #{total_count}*") + end + end +end diff --git a/2018/YuliyaBondareva/2/lib/set_repo.rb b/2018/YuliyaBondareva/2/lib/set_repo.rb new file mode 100644 index 000000000..c3c0058f2 --- /dev/null +++ b/2018/YuliyaBondareva/2/lib/set_repo.rb @@ -0,0 +1,7 @@ +require_relative 'base' +class SetRepo < Base + def save_repo(repo) + File.write("./chat_ids/#{@user_id}", repo) + telegram_send_message("We saved your repo: #{repo}") + end +end diff --git a/2018/YuliyaBondareva/2/lib/show_repo.rb b/2018/YuliyaBondareva/2/lib/show_repo.rb new file mode 100644 index 000000000..063452790 --- /dev/null +++ b/2018/YuliyaBondareva/2/lib/show_repo.rb @@ -0,0 +1,7 @@ +require_relative 'base' +class ShowRepo < Base + def get_repo + repo = File.open("./chat_ids/#{@user_id}") {|f| f.readline} + telegram_send_message("Your repo: #{repo}") + end +end diff --git a/2018/YuliyaBondareva/2/lib/start.rb b/2018/YuliyaBondareva/2/lib/start.rb new file mode 100644 index 000000000..b88045f16 --- /dev/null +++ b/2018/YuliyaBondareva/2/lib/start.rb @@ -0,0 +1,6 @@ +require_relative 'base' +class Start < Base + def send_messages + telegram_send_message('Hello! Have a nice day! if you don\'t know what to do enter /help' ) + end +end diff --git a/2018/YuliyaBondareva/2/octokit.rb b/2018/YuliyaBondareva/2/octokit.rb new file mode 160000 index 000000000..f2a5ff176 --- /dev/null +++ b/2018/YuliyaBondareva/2/octokit.rb @@ -0,0 +1 @@ +Subproject commit f2a5ff176447afc61caa0e0d95b0cd02c031512f diff --git a/2018/YuliyaBondareva/2/telegram_bot.rb b/2018/YuliyaBondareva/2/telegram_bot.rb new file mode 100644 index 000000000..80c2842b5 --- /dev/null +++ b/2018/YuliyaBondareva/2/telegram_bot.rb @@ -0,0 +1,57 @@ +require 'telegram/bot' +require_relative 'lib/start' +require_relative 'lib/base' +require_relative 'lib/help' +require_relative 'lib/set_repo' +require_relative 'lib/show_repo' +require_relative 'lib/search' +require_relative 'lib/history' +require 'yaml' + +SETTINGS = YAML.load(File.open('settings.yml')) + +token = SETTINGS['token'] + +Telegram::Bot::Client.run(token) do |bot| + bot.listen do |message| + Thread.start(message) do |message| + chat_id = message.chat.id + + begin + case message.text + when "/start" + bot.api.send_message(chat_id: chat_id, text: 'Use /help what see all command') + when /^\/set_repo http(s)?:\/\/github.com(.)*$/i + set_repo = SetRepo.new(bot, chat_id) + repository = message.text.gsub("/set_repo ", '') + set_repo.save_repo(repository) + when "/show_repo" + show_repo = ShowRepo.new(bot, chat_id) + show_repo.get_repo + when "/reset" + set_repo = SetRepo.new(bot, chat_id) + set_repo.save_repo('') + when /^\/search (.)+/ + search = Search.new(bot, chat_id) + query = message.text.gsub("/search ", '') + if query.empty? + bot.api.send_message(chat_id: chat_id, text: 'You don\'t input search query') + else + search.commits(query) + end + when "/history" + History.new(bot, chat_id).send_messages + when "/help" + help = Help.new(bot, chat_id) + help.send_messages + else + bot.api.send_message(chat_id: chat_id, text: 'I don\'t understand you :(') + end + rescue Exception => e + bot.api.send_message(chat_id: chat_id, text: "Something doing wrong!!! #{e.message}") + puts e.backtrace.inspect + end + + end + end +end