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 @@ +