diff --git a/Gemfile b/Gemfile index 68ae672..cea4d19 100644 --- a/Gemfile +++ b/Gemfile @@ -1,47 +1,47 @@ source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '2.5.5' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.2.3' # Use pg as the database for Active Record gem 'pg' # Use Puma as the app server gem 'puma', '~> 3.11' gem 'bundler', '2.0.2' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder # gem 'jbuilder', '~> 2.5' # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 4.0' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' - +gem 'crono' # Use ActiveStorage variant # gem 'mini_magick', '~> 4.8' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.1.0', require: false gem 'httparty' # Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible # gem 'rack-cors' group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] end group :development do gem 'listen', '>= 3.0.5', '< 3.2' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] diff --git a/Gemfile.lock b/Gemfile.lock index 11be956..462c2bc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,158 +1,162 @@ GEM remote: https://rubygems.org/ specs: actioncable (5.2.3) actionpack (= 5.2.3) nio4r (~> 2.0) websocket-driver (>= 0.6.1) actionmailer (5.2.3) actionpack (= 5.2.3) actionview (= 5.2.3) activejob (= 5.2.3) mail (~> 2.5, >= 2.5.4) rails-dom-testing (~> 2.0) actionpack (5.2.3) actionview (= 5.2.3) activesupport (= 5.2.3) rack (~> 2.0) rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.2) actionview (5.2.3) activesupport (= 5.2.3) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.3) activejob (5.2.3) activesupport (= 5.2.3) globalid (>= 0.3.6) activemodel (5.2.3) activesupport (= 5.2.3) activerecord (5.2.3) activemodel (= 5.2.3) activesupport (= 5.2.3) arel (>= 9.0) activestorage (5.2.3) actionpack (= 5.2.3) activerecord (= 5.2.3) marcel (~> 0.3.1) activesupport (5.2.3) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 0.7, < 2) minitest (~> 5.1) tzinfo (~> 1.1) arel (9.0.0) bootsnap (1.4.4) msgpack (~> 1.0) builder (3.2.3) byebug (11.0.1) concurrent-ruby (1.1.5) crass (1.0.4) + crono (1.1.2) + activerecord (>= 4.0) + activesupport (>= 4.0) erubi (1.8.0) ffi (1.11.1) globalid (0.4.2) activesupport (>= 4.2.0) httparty (0.17.0) mime-types (~> 3.0) multi_xml (>= 0.5.2) i18n (1.6.0) concurrent-ruby (~> 1.0) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) ruby_dep (~> 1.2) loofah (2.2.3) crass (~> 1.0.2) nokogiri (>= 1.5.9) mail (2.7.1) mini_mime (>= 0.1.1) marcel (0.3.3) mimemagic (~> 0.3.2) method_source (0.9.2) mime-types (3.2.2) mime-types-data (~> 3.2015) mime-types-data (3.2019.0331) mimemagic (0.3.3) mini_mime (1.0.2) mini_portile2 (2.4.0) minitest (5.11.3) msgpack (1.3.1) multi_xml (0.6.0) nio4r (2.4.0) nokogiri (1.10.3) mini_portile2 (~> 2.4.0) pg (1.1.4) puma (3.12.1) rack (2.0.7) rack-test (1.1.0) rack (>= 1.0, < 3) rails (5.2.3) actioncable (= 5.2.3) actionmailer (= 5.2.3) actionpack (= 5.2.3) actionview (= 5.2.3) activejob (= 5.2.3) activemodel (= 5.2.3) activerecord (= 5.2.3) activestorage (= 5.2.3) activesupport (= 5.2.3) bundler (>= 1.3.0) railties (= 5.2.3) sprockets-rails (>= 2.0.0) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) rails-html-sanitizer (1.2.0) loofah (~> 2.2, >= 2.2.2) railties (5.2.3) actionpack (= 5.2.3) activesupport (= 5.2.3) method_source rake (>= 0.8.7) thor (>= 0.19.0, < 2.0) rake (12.3.3) rb-fsevent (0.10.3) rb-inotify (0.10.0) ffi (~> 1.0) ruby_dep (1.5.0) spring (2.1.0) spring-watcher-listen (2.0.1) listen (>= 2.7, < 4.0) spring (>= 1.2, < 3.0) sprockets (3.7.2) concurrent-ruby (~> 1.0) rack (> 1, < 3) sprockets-rails (3.2.1) actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) thor (0.20.3) thread_safe (0.3.6) tzinfo (1.2.5) thread_safe (~> 0.1) websocket-driver (0.7.1) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.4) PLATFORMS ruby DEPENDENCIES bootsnap (>= 1.1.0) bundler (= 2.0.2) byebug + crono httparty listen (>= 3.0.5, < 3.2) pg puma (~> 3.11) rails (~> 5.2.3) spring spring-watcher-listen (~> 2.0.0) tzinfo-data RUBY VERSION ruby 2.5.5p157 BUNDLED WITH 2.0.2 diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4ac8823..09705d1 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,2 +1,2 @@ -class ApplicationController < ActionController::API +class ApplicationController < ActionController::Base end diff --git a/app/controllers/message_controller.rb b/app/controllers/message_controller.rb index b80395e..84ef675 100644 --- a/app/controllers/message_controller.rb +++ b/app/controllers/message_controller.rb @@ -1,99 +1,129 @@ class MessageController < ApplicationController def message_process bot_api_key = '836213850:AAG-aBtJB8khJ53DNRlzORUVcFWOH5SOF9o' - unless params[:message].blank? - message = params[:message].to_unsafe_h + return false if params[:message].blank? + message = params[:message].to_unsafe_h if message[:chat][:type] == 'group' || message[:chat][:type] == 'supergroup' - logger.debug '1ok' if message[:text].downcase =~ /asd/ - logger.debug '2ok' - asdcount = message[:text].downcase.scan(/asd/).count + multiplevalue = message[:text].downcase.scan(/asd/).count unless Group.find_by(chat_id: message[:chat][:id]) @group = Group.create(chat_id: message[:chat][:id], username: message[:chat][:username]) else @group = Group.find_by(chat_id: message[:chat][:id]) end + unless @group.welcomesent + HTTParty.get(CGI.escape("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{@group.chat_id}&text=Bella zio! Sono il bot asdoso creato da Ferdinando Traversa (ferdinando.me) da idea di Valerio Bozzolan, asd! Digita /grafico per ricevere il link ad un grafico, anche in privato per averne uno personale, asd o /classifca per scoprire cose interessanti. L'impostazione automatica è che io invii il conto degli ASD alla fine della serata, così però ti perdi cose belle come la faccina dell'ASD di mezzanotte. Per modificare questa impostazione, basta digitare /nightsend ed invierò il messaggio di conteggio appena invii un asd")) + @group.update_attribute(:welcomesent, true) + end + unless Sender.find_by(chat_id: message[:from][:id]) @sender = Sender.create(chat_id: message[:from][:id], username: message[:from][:username]) else @sender = Sender.find_by(chat_id: message[:from][:id]) end - asdcount.times do - @asd = Asd.new(group: @group, sender: @sender, text: message[:text], update_id: params[:update_id]) - if @asd.save + defmultiplevalue = multiplevalue - 1 + @asd = Asd.new(group: @group, sender: @sender, text: message[:text], update_id: params[:update_id], multiple_value: defmultiplevalue) + defmultiplevalue.each do |multiple| + multiple = Asd.new(group: @group, sender: @sender, text: message[:text]) + end + asdcount = @group.asds.count case asdcount when 1 addtext = 'Il primo asd. Benvenuto nella grande famiglia di asdbot' + SpecialEvent.create(text: addtext, group: @group, asd: @asd) when 10 addtext = 'Il decimo asd! Complimenti, asd.' + SpecialEvent.create(text: addtext, group: @group, asd: @asd) when 100 addtext = 'IL CENTESIMO ASD, ASD! COMPLIMENTS CONGRATULATIONS AUF WIDERSHEN' + SpecialEvent.create(text: addtext, group: @group, asd: @asd) when 1000 addtext = '1000 asd, wow! Questo gruppo, così asdoso, asd' + SpecialEvent.create(text: addtext, group: @group, asd: @asd) when 10000 addtext = '10000 è un record mondiale, asd' - else - noasdface = true - addtext = '' + SpecialEvent.create(text: addtext, group: @group, asd: @asd) + end + + unless @group.nightsend + if SpecialEvent.find_by(asd: @asd) + HTTParty.get(CGI.escape("http://api.telegram.org/bot#{bot_api_key}/sendPhoto?chat_id=#{@group.chat_id}&photo=http://www.lanciano.it/faccine/asdone.gif&caption=Così asdoso, asd. #{SpecialEvent.find_by(asd: @asd).text}")) + SpecialEvent.find_by(asd: @asd).destroy end - unless noasdface - HTTParty.get(CGI.escape("http://api.telegram.org/bot#{bot_api_key}/sendPhoto?chat_id=#{@group.chat_id}&photo=http://www.lanciano.it/faccine/asdone.gif&caption=Così asdoso, asd.")) + position = Group.all.sort_by{|group| group.asds.count}.pluck(:id).reverse.find_index(@group.id) + 1 + HTTParty.get(CGI.escape("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{@group.chat_id}&text=Il contasd conta ben #{asdcount} (+ #{defmultiplevalue}), asd. Sei il #{position}º gruppo per ASD inviati.")) + if @asd.created_at.strftime('%H:%M') == '00:00' + HTTParty.get(CGI.escape("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{@group.chat_id}&text=Asd di mezzanotte 🌚")) end - position = 'primo in assoluto' - position = Group.all.sort_by{|group| group.asds.count}.pluck(:id).reverse.find_index(@group.id) + 1 if Group.count > 0 - HTTParty.get(CGI.escape("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{@group.chat_id}&text=Il contasd conta ben #{asdcount}, asd. Sei il #{position}º gruppo per ASD inviati. #{addtext}")) + end end - end - end end - message = params[:message].to_unsafe_h - if message[:text] == '/start' && (message[:chat][:type] == 'group' || message[:chat][:type] == 'supergroup') unless Group.find_by(chat_id: message[:chat][:id]) @group = Group.create(chat_id: message[:chat][:id], username: message[:chat][:username]) else @group = Group.find_by(chat_id: message[:chat][:id]) end - HTTParty.get(CGI.escape("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{@group.chat_id}&text=Bella zio! Sono il bot asdoso creato da Ferdinando Traversa (ferdinando.me) da idea di Valerio Bozzolan (reyboz.it), asd! Digita /grafico per ricevere il link ad un grafico, anche in privato per averne uno personale, asd o /classifca per scoprire cose interessanti.")) + HTTParty.get(CGI.escape("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{@group.chat_id}&text=Bella zio! Sono il bot asdoso creato da Ferdinando Traversa (ferdinando.me) da idea di Valerio Bozzolan, asd! Digita /grafico per ricevere il link ad un grafico, anche in privato per averne uno personale, asd o /classifca per scoprire cose interessanti. L'impostazione automatica è che io invii il conto degli ASD alla fine della serata, così però ti perdi cose belle come la faccina dell'ASD di mezzanotte. Per modificare questa impostazione, basta digitare /nightsend ed invierò il messaggio di conteggio appena invii un asd.")) + @group.update_attribute(:welcomesent, true) end if message[:text] == '/classifica' - HTTParty.get(CGI.escape("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{message[:chat][:id]}&text=Vai su #{request.domain}/classifica per vedere la classifica.")) + HTTParty.get(CGI.escape("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{message[:chat][:id]}&text=Vai su #{ENV['DOMAIN']}/classifica per vedere la classifica. Ci sono #{Group.count} che usano questo bot, comunque.")) end + if message[:text] == '/start' && message[:chat][:type] == 'private' - HTTParty.get(CGI.escape("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{message[:chat][:id]}&text=Bella zio! Sono il bot asdoso creato da Ferdinando Traversa (ferdinando.me @ferdi2005) da idea di Valerio Bozzolan (reyboz.it), asd! Aggiungimi ad un bel gruppo e conterò gli asd, altrimenti digita /grafico per il tuo grafico personal personal.")) + HTTParty.get(CGI.escape("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{message[:chat][:id]}&text=Bella zio! Sono il bot asdoso creato da Ferdinando Traversa (ferdinando.me @ferdi2005) da idea di Valerio Bozzolan, asd! Aggiungimi ad un bel gruppo e conterò gli asd, altrimenti digita /grafico per il tuo grafico personal personal.")) end if message[:text] == '/grafico' && message[:chat][:type] == 'private' unless Sender.find_by(chat_id: message[:from][:id]) HTTParty.get(CGI.escape("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{message[:chat][:id]}&text=Non ho ancora un grafico per te, sei nuovo per me, non ti conosco. Iscriviti in qualche gruppo con questo bot e manda asd a ripetizione, poi torna da me.")) else @sender = Sender.find_by(chat_id: message[:from][:id]) position = 'primo in assoluto' position = Sender.all.sort_by{|sender| sender.asds.count}.pluck(:id).reverse.find_index(@sender.id) + 1 if Sender.count > 0 HTTParty.get(CGI.escape("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{message[:chat][:id]}&text=Amico caro, la funzionalità grafico sta arrivando, arriva quando questo numero (#{Group.count}) uguale a 20, forse. Nel frattempo ti posso solo dire che hai al tuo attivo ben #{@sender.asds.count} asd e sei il #{position}º inviatore di ASD classifica globale!")) end end - + if message[:text] == '/grafico' && (message[:chat][:type] == 'group' || message[:chat][:type] == 'supergroup') unless Group.find_by(chat_id: message[:chat][:id]) @group = Group.create(chat_id: message[:chat][:id], username: message[:chat][:username]) HTTParty.get(CGI.escape("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{message[:chat][:id]}&text=Non ho ancora un grafico per te, sei nuovo per me, non ti conosco. Invia qualche asd e prova questo comando.")) else @group = Group.find_by(chat_id: message[:chat][:id]) - position = 'primo in assoluto' - position = Group.all.sort_by{|group| group.asds.count}.pluck(:id).reverse.find_index(@group.id) + 1 if Group.count > 0 + position = Group.all.sort_by{|group| group.asds.count}.pluck(:id).reverse.find_index(@group.id) + 1 HTTParty.get(CGI.escape("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{message[:chat][:id]}&text=Amico caro, la funzionalità grafico sta arrivando, arriverà quando questo numero (#{Group.count}) sarà uguale a 20, forse. Nel frattempo ti posso solo dire che hai al tuo attivo ben #{@group.asds.count} asd e sei il #{position}º gruppo!")) end end + + if message[:text] == '/nightsend' && (message[:chat][:type] == 'group' || message[:chat][:type] == 'supergroup') + unless Group.find_by(chat_id: message[:chat][:id]) + @group = Group.create(chat_id: message[:chat][:id], username: message[:chat][:username]) + HTTParty.get(CGI.escape("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{message[:chat][:id]}&text=Ok, l'impostazione predefinita è che l'invio del conto degli asd avvenga a mezzanotte, ma con questo comando la modifico. Procedo, asd.")) + @group.update_attribute(:nightsend, false) + else + @group = Group.find_by(chat_id: message[:chat][:id]) + if @group.nightsend + HTTParty.get(CGI.escape("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{message[:chat][:id]}&text=Ok, l'impostazione predefinita è che l'invio del conto degli asd avvenga a mezzanotte, ma con questo comando la modifico. Procedo, ora avrai un messaggio ogni asd, asd.")) + @group.update_attribute(:nightsend, false) + else + HTTParty.get(CGI.escape("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{message[:chat][:id]}&text=Sei una persona triste, asd. Vuoi che il conteggio venga inviato a mezzanotte. Bozzolan dice sì, Ferdi dice no. Tu dici sì, allora conteggio a mezzanotte sia, asd")) + @group.update_attribute(:nightsend, true) + end + end end end def classifica @groups = Group.all.sort_by{|sender| sender.asds.count}.reverse @senders = Sender.all.sort_by{|sender| sender.asds.count}.reverse end -end + + def home + end +end \ No newline at end of file diff --git a/app/jobs/process_message_job.rb b/app/jobs/process_message_job.rb deleted file mode 100644 index e93d0d3..0000000 --- a/app/jobs/process_message_job.rb +++ /dev/null @@ -1,86 +0,0 @@ -class ProcessMessageJob < ApplicationJob - queue_as :default - - def perform(message) - bot_api_key = '836213850:AAG-aBtJB8khJ53DNRlzORUVcFWOH5SOF9o' - message = OpenStruct.new(JSON.parse(message)) - if message.try(:chat).try(:type) == 'group' - if message.text.match?('/asd/') - unless Group.find_by(chat_id: message.chat.id) - @group = Group.create(chat_id: message.chat.id, username: message.chat.username) - else - @group = Group.find_by(chat_id: message.chat.id) - end - - unless Sender.find_by(chat_id: message.from.id) - @sender = Sender.create(chat_id: message.from.id, username: message.from.username) - else - @sender = Sender.find_by(chat_id: message.from.id) - end - @asd = Asd.create(group: @group, sender: @sender, text: message.text) - - asdcount = @group.asds.count - case asdcount - when 1 - addtext = 'Il primo asd. Benvenuto nella grande famiglia di asdbot' - when 10 - addtext = 'Il decimo asd! Complimenti, asd.' - when 100 - addtext = 'IL CENTESIMO ASD, ASD! COMPLIMENTS CONGRATULATIONS AUF WIDERSHEN' - when 1000 - addtext = '1000 asd, wow! Questo gruppo è così asdoso, asd' - when 10000 - addtext = '10000 è un record mondiale, asd' - else - noasdface = true - addtext = '' - end - unless noasdface - HTTParty.get("http://api.telegram.org/bot#{bot_api_key}/sendPhoto?chat_id=#{@group.chat_id}&photo=http://www.lanciano.it/faccine/asdone.gif&caption=Così asdoso, asd.") - end - position = 'primo in assoluto' - position = Group.all.sort_by{|group| group.asds.count}.pluck(:id).reverse.find_index(@group.id) if Group.count > 0 - HTTParty.get("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{@group.chat_id}&text=Il contasd conta ben #{asdcount}, asd. Sei il #{position}º gruppo per ASD inviati. #{addtext}"); - end - end - - if message.text == '/start' && message.try(:chat).try(:type) == 'group' - unless Group.find_by(chat_id: message.chat.id) - @group = Group.create(chat_id: message.chat.id, username: message.chat.username) - else - @group = Group.find_by(chat_id: message.chat.id) - end - HTTParty.get("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{@group.chat_id}&text=Bella zio! Sono il bot asdoso creato da Ferdinando Traversa (ferdinando.me) da idea di Valerio Bozzolan (reyboz.it), asd! Digita /grafico per ricevere il link ad un grafico, anche in privato per averne uno personale, asd o /classifca per scoprire cose interessanti."); - end - - if message.text == '/classifica' - HTTParty.get("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{@group.chat_id}&text=Vai su #{request.domain}/classifica per vedere la classifica."); - end - if message.text == '/start' && message.try(:chat).try(:type) == 'private' - HTTParty.get("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{message.chat.id}&text=Bella zio! Sono il bot asdoso creato da Ferdinando Traversa (ferdinando.me @ferdi2005) da idea di Valerio Bozzolan (reyboz.it), asd! Aggiungimi ad un bel gruppo e conterò gli asd, altrimenti digita /grafico per il tuo grafico personal personal."); - end - - if message.text == '/grafico' && message.try(:chat).try(:type) == 'private' - unless Sender.find_by(chat_id: message.from.id) - HTTParty.get("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{message.chat.id}&text=Non ho ancora un grafico per te, sei nuovo per me, non ti conosco. Iscriviti in qualche gruppo con questo bot e manda asd a ripetizione, poi torna da me."); - else - @sender = Sender.find_by(chat_id: message.from.id) - position = 'primo in assoluto' - position = Sender.all.sort_by{|sender| sender.asds.count}.pluck(:id).reverse.find_index(@sender.id) if Sender.count > 0 - HTTParty.get("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{message.chat.id}&text=Amico caro, la funzionalità grafico sta arrivando, arriverà quando questo numero (#{Group.count}) sarà uguale a 20, forse. Nel frattempo ti posso solo dire che hai al tuo attivo ben #{@sender.asds.count} asd e sei il #{position}º inviatore di ASD classifica globale!"); - end - end - - if message.text == '/grafico' && message.try(:chat).try(:type) == 'group' - unless Group.find_by(chat_id: message.chat.id) - @group = Group.create(chat_id: message.chat.id, username: message.chat.username) - HTTParty.get("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{message.chat.id}&text=Non ho ancora un grafico per te, sei nuovo per me, non ti conosco. Invia qualche asd e prova questo comando."); - else - @group = Group.find_by(chat_id: message.chat.id) - position = 'primo in assoluto' - position = Group.all.sort_by{|group| group.asds.count}.pluck(:id).reverse.find_index(@group.id) if Group.count > 0 - HTTParty.get("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{message.chat.id}&text=Amico caro, la funzionalità grafico sta arrivando, arriverà quando questo numero (#{Group.count}) sarà uguale a 20, forse. Nel frattempo ti posso solo dire che hai al tuo attivo ben #{@group.asds.count} asd e sei il #{position}º gruppo!"); - end - end -end -end diff --git a/app/jobs/send_asd_count_job.rb b/app/jobs/send_asd_count_job.rb new file mode 100644 index 0000000..465c1c3 --- /dev/null +++ b/app/jobs/send_asd_count_job.rb @@ -0,0 +1,18 @@ +class SendAsdCountJob < ApplicationJob + queue_as :default + + def perform(*args) + Group.where(nightsend: true).each do |group| + @group = group + date = Date.yesterday + @asds = Asd.where(created_at: date.midnight..date.end_of_day, group: group) + if SpecialEvent.find_by(group: @group) + HTTParty.get(CGI.escape("http://api.telegram.org/bot#{bot_api_key}/sendPhoto?chat_id=#{@group.chat_id}&photo=http://www.lanciano.it/faccine/asdone.gif&caption=Così asdoso, asd. #{SpecialEvent.find_by(group: @group).text}")) + SpecialEvent.find_by(group: @group).destroy + end + position = Group.all.sort_by{|group| group.asds.count}.pluck(:id).reverse.find_index(@group.id) + 1 + defmultipletimes = @asds.pluck(:multiple_times).sum + HTTParty.get(CGI.escape("http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{@group.chat_id}&text=È mezzanotte, ora di sapere! Il contasd di ieri conta ben #{@asds.count}, asd. Sei il #{position}º gruppo per ASD inviati. (compresi quelli multipli, che sono #{defmultipletimes})")) + end + end +end diff --git a/app/models/asd.rb b/app/models/asd.rb index e9e5d5b..f68ac0a 100644 --- a/app/models/asd.rb +++ b/app/models/asd.rb @@ -1,5 +1,10 @@ class Asd < ApplicationRecord belongs_to :group belongs_to :sender validates :update_id, uniqueness: true + has_many :special_events + before_create :default_values + def default_values + self.multiple_times = multiple_times.presence || 0 + end end diff --git a/app/models/group.rb b/app/models/group.rb index 875bed9..0dbb19f 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -1,3 +1,4 @@ class Group < ApplicationRecord has_many :asds + has_many :special_events end diff --git a/app/models/special_event.rb b/app/models/special_event.rb new file mode 100644 index 0000000..20e00be --- /dev/null +++ b/app/models/special_event.rb @@ -0,0 +1,4 @@ +class SpecialEvent < ApplicationRecord + belongs_to :group + belongs_to :asd +end diff --git a/app/views/message/classifica.html.erb b/app/views/message/classifica.html.erb index 8f7d5d3..d8462ea 100644 --- a/app/views/message/classifica.html.erb +++ b/app/views/message/classifica.html.erb @@ -1,14 +1,21 @@ +

LA CLASSIFICA SOMMA

GRUPPI

    -<%= @groups.each do |group| %> +<% @groups.each do |group| %>
  1. <%= group.username %> --> <%= group.asds.count %> asd
  2. <% end %> - +
+

UTENTI

    -<%= @senders.each do |group| %> +<% @senders.each do |group| %>
  1. <%= group.username %> --> <%= group.asds.count %> asd
  2. <% end %> - -Sviluppato con cuore da Ferdinando Traversa - Blog - Github - Basato su Rails 5 \ No newline at end of file +
+
+Sviluppato con cuore da Ferdinando Traversa - Blog - Github - Basato su Rails 5 \ No newline at end of file diff --git a/app/views/message/home.html.erb b/app/views/message/home.html.erb new file mode 100644 index 0000000..ef761ff --- /dev/null +++ b/app/views/message/home.html.erb @@ -0,0 +1,4 @@ +

Bot sviluppato da Ferdinando Traversa

+Sviluppato con cuore da Ferdinando Traversa - Blog - Github - Basato su Rails 5 + +Aggiungilo ai tuoi gruppi --> AsdCounterBot \ No newline at end of file diff --git a/config/application.rb b/config/application.rb index e27bb7d..4595378 100644 --- a/config/application.rb +++ b/config/application.rb @@ -1,35 +1,35 @@ require_relative 'boot' require "rails" # Pick the frameworks you want: require "active_model/railtie" require "active_job/railtie" require "active_record/railtie" require "active_storage/engine" require "action_controller/railtie" require "action_mailer/railtie" require "action_view/railtie" require "action_cable/engine" # require "sprockets/railtie" require "rails/test_unit/railtie" # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. Bundler.require(*Rails.groups) module Asdbot class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. config.load_defaults 5.2 - + config.time_zone = "Rome" # Settings in config/environments/* take precedence over those specified here. # Application configuration can go into files in config/initializers # -- all .rb files in that directory are automatically loaded after loading # the framework and any gems in your application. # Only loads a smaller set of middleware suitable for API only apps. # Middleware like session, flash, cookies can be added back manually. # Skip views, helpers and assets when generating a new resource. config.api_only = true end end diff --git a/config/cronotab.rb b/config/cronotab.rb new file mode 100644 index 0000000..ecdcad5 --- /dev/null +++ b/config/cronotab.rb @@ -0,0 +1,16 @@ +# cronotab.rb — Crono configuration file +# +# Here you can specify periodic jobs and schedule. +# You can use ActiveJob's jobs from `app/jobs/` +# You can use any class. The only requirement is that +# class should have a method `perform` without arguments. +# +# class TestJob +# def perform +# puts 'Test!' +# end +# end +# +# Crono.perform(TestJob).every 2.days, at: '15:30' +# +Crono.perform(SendAsdCountJob).every 1.day, at: {hour: 00, min: 00} diff --git a/db/migrate/20190811102515_create_crono_jobs.rb b/db/migrate/20190811102515_create_crono_jobs.rb new file mode 100644 index 0000000..d199913 --- /dev/null +++ b/db/migrate/20190811102515_create_crono_jobs.rb @@ -0,0 +1,16 @@ +class CreateCronoJobs < ActiveRecord::Migration[5.2] + def self.up + create_table :crono_jobs do |t| + t.string :job_id, null: false + t.text :log, limit: 1073741823 # LONGTEXT for MySQL + t.datetime :last_performed_at + t.boolean :healthy + t.timestamps null: false + end + add_index :crono_jobs, [:job_id], unique: true + end + + def self.down + drop_table :crono_jobs + end +end diff --git a/db/migrate/20190811102936_create_special_events.rb b/db/migrate/20190811102936_create_special_events.rb new file mode 100644 index 0000000..94221a1 --- /dev/null +++ b/db/migrate/20190811102936_create_special_events.rb @@ -0,0 +1,11 @@ +class CreateSpecialEvents < ActiveRecord::Migration[5.2] + def change + create_table :special_events do |t| + t.references :group, foreign_key: true + t.string :text + t.references :asd, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/migrate/20190811103045_add_multiple_times_to_asd.rb b/db/migrate/20190811103045_add_multiple_times_to_asd.rb new file mode 100644 index 0000000..a02cc98 --- /dev/null +++ b/db/migrate/20190811103045_add_multiple_times_to_asd.rb @@ -0,0 +1,5 @@ +class AddMultipleTimesToAsd < ActiveRecord::Migration[5.2] + def change + add_column :asds, :multiple_times, :integer + end +end diff --git a/db/migrate/20190811105324_add_welcomesent_to_groups.rb b/db/migrate/20190811105324_add_welcomesent_to_groups.rb new file mode 100644 index 0000000..0abf1df --- /dev/null +++ b/db/migrate/20190811105324_add_welcomesent_to_groups.rb @@ -0,0 +1,5 @@ +class AddWelcomesentToGroups < ActiveRecord::Migration[5.2] + def change + add_column :groups, :welcomesent, :boolean, default: false + end +end diff --git a/db/migrate/20190811114154_add_nightsend_to_groups.rb b/db/migrate/20190811114154_add_nightsend_to_groups.rb new file mode 100644 index 0000000..795f1f2 --- /dev/null +++ b/db/migrate/20190811114154_add_nightsend_to_groups.rb @@ -0,0 +1,5 @@ +class AddNightsendToGroups < ActiveRecord::Migration[5.2] + def change + add_column :groups, :nightsend, :boolean, default: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 93c231f..70222ec 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,39 +1,71 @@ # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # Note that this schema.rb definition is the authoritative source for your # database schema. If you need to create the application database on another # system, you should be using db:schema:load, not running all the migrations # from scratch. The latter is a flawed and unsustainable approach (the more migrations # you'll amass, the slower it'll run and the greater likelihood for issues). # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_08_10_152822) do +ActiveRecord::Schema.define(version: 2019_08_11_114154) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" create_table "asds", force: :cascade do |t| - t.integer "group_id" - t.integer "sender_id" + t.bigint "group_id" + t.bigint "sender_id" + t.integer "update_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "text" + t.boolean "nightsend" + t.integer "multiple_times" t.index ["group_id"], name: "index_asds_on_group_id" t.index ["sender_id"], name: "index_asds_on_sender_id" end + create_table "crono_jobs", force: :cascade do |t| + t.string "job_id", null: false + t.text "log" + t.datetime "last_performed_at" + t.boolean "healthy" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["job_id"], name: "index_crono_jobs_on_job_id", unique: true + end + create_table "groups", force: :cascade do |t| - t.integer "chat_id", limit: 18 + t.bigint "chat_id" t.string "username" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.boolean "welcomesent", default: false + t.boolean "nightsend" end create_table "senders", force: :cascade do |t| t.integer "chat_id" t.string "username" t.datetime "created_at", null: false t.datetime "updated_at", null: false end + create_table "special_events", force: :cascade do |t| + t.bigint "group_id" + t.string "text" + t.bigint "asd_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["asd_id"], name: "index_special_events_on_asd_id" + t.index ["group_id"], name: "index_special_events_on_group_id" + end + + add_foreign_key "asds", "groups" + add_foreign_key "asds", "senders" + add_foreign_key "special_events", "asds" + add_foreign_key "special_events", "groups" end diff --git a/test/fixtures/special_events.yml b/test/fixtures/special_events.yml new file mode 100644 index 0000000..5ee6527 --- /dev/null +++ b/test/fixtures/special_events.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + group: one + text: MyString + asd: one + +two: + group: two + text: MyString + asd: two diff --git a/test/jobs/send_asd_count_job_test.rb b/test/jobs/send_asd_count_job_test.rb new file mode 100644 index 0000000..2b99e5f --- /dev/null +++ b/test/jobs/send_asd_count_job_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class SendAsdCountJobTest < ActiveJob::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/special_event_test.rb b/test/models/special_event_test.rb new file mode 100644 index 0000000..d08be97 --- /dev/null +++ b/test/models/special_event_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class SpecialEventTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end