Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homework 2 #775

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions 2018/YuliyaBondareva/2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
settings.yml
.bundle/
chat_ids/*
1/*
7 changes: 7 additions & 0 deletions 2018/YuliyaBondareva/2/Gemfile
Original file line number Diff line number Diff line change
@@ -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'
70 changes: 70 additions & 0 deletions 2018/YuliyaBondareva/2/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions 2018/YuliyaBondareva/2/lib/base.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions 2018/YuliyaBondareva/2/lib/help.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions 2018/YuliyaBondareva/2/lib/history.rb
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions 2018/YuliyaBondareva/2/lib/search.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions 2018/YuliyaBondareva/2/lib/set_repo.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions 2018/YuliyaBondareva/2/lib/show_repo.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions 2018/YuliyaBondareva/2/lib/start.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions 2018/YuliyaBondareva/2/octokit.rb
Submodule octokit.rb added at f2a5ff
57 changes: 57 additions & 0 deletions 2018/YuliyaBondareva/2/telegram_bot.rb
Original file line number Diff line number Diff line change
@@ -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