diff --git a/app/assets/javascripts/standardpage.coffee b/app/assets/javascripts/standardpage.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/standardpage.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/standardpage.scss b/app/assets/stylesheets/standardpage.scss new file mode 100644 index 0000000..f1c3473 --- /dev/null +++ b/app/assets/stylesheets/standardpage.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the standardpage controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/message_controller.rb b/app/controllers/message_controller.rb index 3a7ef83..7dda6d2 100644 --- a/app/controllers/message_controller.rb +++ b/app/controllers/message_controller.rb @@ -1,135 +1,120 @@ -class MessageController < ApplicationController +class MessageController < ActionController::API def message_process bot_api_key = ENV['BOT_API_KEY'] + byebug return false if params[:message].blank? message = params[:message].to_unsafe_h if message[:chat][:type] == 'group' || message[:chat][:type] == 'supergroup' if message[:text].downcase =~ /asd/ 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 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' 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 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 end end end 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, 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 #{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, 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=Guarda il tuo grafico personalizzato per il gruppo su #{ENV['DOMAIN']}/grafico?s=#{@sender.chat_id}")) end end if message[:text] == '/grafico' && (message[:chat][:type] == 'group' || message[:chat][:type] == 'supergroup') unless Group.find_by(chat_id: message[:chat][: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. Invia qualche asd e prova questo comando.")) else @group = Group.find_by(chat_id: message[:chat][:id]) 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=Guarda il tuo grafico personalizzato per il gruppo su #{ENV['DOMAIN']}/grafico?g=#{@group.chat_id}")) end end if message[:text] == '/nightsend' && (message[:chat][:type] == 'group' || message[:chat][:type] == 'supergroup') unless Group.find_by(chat_id: message[:chat][:id]) 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 - - def grafico - if !params[:s].nil? - @sender = Sender.find_by(chat_id: params[:s]) - elsif !params[:g].nil? - @group = Group.find_by(chat_id: params[:g]) - end - end - - def home + render status: 200 end end \ No newline at end of file diff --git a/app/controllers/standardpage_controller.rb b/app/controllers/standardpage_controller.rb new file mode 100644 index 0000000..300ad91 --- /dev/null +++ b/app/controllers/standardpage_controller.rb @@ -0,0 +1,17 @@ +class StandardpageController < ApplicationController + def classifica + @groups = Group.all.sort_by{|sender| sender.asds.count}.reverse + @senders = Sender.all.sort_by{|sender| sender.asds.count}.reverse + end + + def grafico + if !params[:s].nil? + @sender = Sender.find_by(chat_id: params[:s]) + elsif !params[:g].nil? + @group = Group.find_by(chat_id: params[:g]) + end + end + + def home + end +end diff --git a/app/helpers/standardpage_helper.rb b/app/helpers/standardpage_helper.rb new file mode 100644 index 0000000..d89f253 --- /dev/null +++ b/app/helpers/standardpage_helper.rb @@ -0,0 +1,2 @@ +module StandardpageHelper +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 7d337ad..e820bae 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,15 +1,20 @@ Asdbot <%= csrf_meta_tags %> <%= csp_meta_tag %> <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> <%= yield %> +
+
+ Sviluppato con cuore da Ferdinando Traversa - Blog - Github - Basato su Rails 5
+ Aggiungilo ai tuoi gruppi --> AsdCounterBot +
diff --git a/app/views/message/home.html.erb b/app/views/message/home.html.erb deleted file mode 100644 index ef761ff..0000000 --- a/app/views/message/home.html.erb +++ /dev/null @@ -1,4 +0,0 @@ -

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/app/views/message/classifica.html.erb b/app/views/standardpage/classifica.html.erb similarity index 67% rename from app/views/message/classifica.html.erb rename to app/views/standardpage/classifica.html.erb index 8cbcbc9..d7f0596 100644 --- a/app/views/message/classifica.html.erb +++ b/app/views/standardpage/classifica.html.erb @@ -1,16 +1,15 @@

LA CLASSIFICA SOMMA, ASD

GRUPPI

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

UTENTI

    <% @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 diff --git a/app/views/message/grafico.html.erb b/app/views/standardpage/grafico.html.erb similarity index 88% rename from app/views/message/grafico.html.erb rename to app/views/standardpage/grafico.html.erb index 838d8a8..dd6f09e 100644 --- a/app/views/message/grafico.html.erb +++ b/app/views/standardpage/grafico.html.erb @@ -1,7 +1,7 @@ -<%= unless @sender.nil? %> +<% unless @sender.nil? %> <%= line_chart Asd.where(sender: @sender).group_by_day(:created_at).count %> <% end %> <% unless @group.nil? %> <%= line_chart Asd.where(group: @group).group_by_day(:created_at).count %> <% end %> \ No newline at end of file diff --git a/app/views/standardpage/home.html.erb b/app/views/standardpage/home.html.erb new file mode 100644 index 0000000..9bdd698 --- /dev/null +++ b/app/views/standardpage/home.html.erb @@ -0,0 +1 @@ +

AsdBot

diff --git a/config/routes.rb b/config/routes.rb index a027d11..72ce3ff 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,6 @@ Rails.application.routes.draw do post 'message_process', to: 'message#message_process' - get 'classifica', to: 'message#classifica' + get 'classifica', to: 'standardpage#classifica' + get 'grafico', to: 'standardpage#grafico' # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end diff --git a/test/controllers/standardpage_controller_test.rb b/test/controllers/standardpage_controller_test.rb new file mode 100644 index 0000000..f6a4a37 --- /dev/null +++ b/test/controllers/standardpage_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class StandardpageControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end