diff --git a/app/controllers/message_controller.rb b/app/controllers/message_controller.rb index 39e0b57..5694fe5 100644 --- a/app/controllers/message_controller.rb +++ b/app/controllers/message_controller.rb @@ -1,132 +1,132 @@ class MessageController < ApplicationController def message_process bot_api_key = ENV['BOT_API_KEY'] client = HTTPClient message = params[:message].to_unsafe_h - return false if message.nil? || message[:chat].nil? + return if message.nil? || message[:chat].nil? unless message[:text].nil? text = message[:text] else text = message[:caption] end type = message[:chat][:type] id = message[:chat][:id] username = message[:chat][:username] update_id = params[:update_id] fromid = message[:from][:id] fromusername = message[:from][:username] if type == 'group' || type == 'supergroup' if text =~ /asd/i multiplevalue = text.scan(/asd/i).count unless Group.find_by(chat_id: id) @group = Group.create(chat_id: id, username: username) else @group = Group.find_by(chat_id: id) end unless @group.welcomesent client.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, 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 %C3%A8 che io invii il conto degli ASD alla fine della serata, cos%C3%AC per%C3%B2 ti perdi cose belle come la faccina dell'ASD di mezzanotte. Per modificare questa impostazione, basta digitare /nightsend ed invier%C3%B2 il messaggio di conteggio appena invii un asd" @group.update_attribute(:welcomesent, true) end - unless Sender.find_by(chat_id: id) - @sender = Sender.create(chat_id: id, username: fromusername) + unless Sender.find_by(chat_id: fromid) + @sender = Sender.create(chat_id: fromid, username: fromusername) else - @sender = Sender.find_by(chat_id: id) + @sender = Sender.find_by(chat_id: fromid) end defmultiplevalue = multiplevalue - 1 @asd = Asd.new(group: @group, sender: @sender, text: text, update_id: update_id, multiple_times: defmultiplevalue) defmultiplevalue.times do Asd.create(group: @group, sender: @sender, text: 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%C3%AC asdoso, asd' SpecialEvent.create(text: addtext, group: @group, asd: @asd) when 10000 addtext = '10000 %C3%A8 un record mondiale, asd' SpecialEvent.create(text: addtext, group: @group, asd: @asd) end unless @group.nightsend if SpecialEvent.find_by(asd: @asd) client.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%C3%AC 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 client.get "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}%C2%BA gruppo per ASD inviati." if @asd.created_at.strftime('%H:%M') == '00:00' client.get "http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{@group.chat_id}&text=Asd di mezzanotte %F0%9F%8C%9A" end end end end if text == '/start' && (type == 'group' || type == 'supergroup') unless Group.find_by(chat_id: id) @group = Group.create(chat_id: id, username: username) else @group = Group.find_by(chat_id: id) end client.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, 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 %C3%A8 che io invii il conto degli ASD alla fine della serata, cos%C3%AC per%C3%B2 ti perdi cose belle come la faccina dell'ASD di mezzanotte. Per modificare questa impostazione, basta digitare /nightsend ed invier%C3%B2 il messaggio di conteggio appena invii un asd" @group.update_attribute(:welcomesent, true) end if text == '/classifica' client.get "http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{id}&text=Vai su #{ENV['DOMAIN']}/classifica per vedere la classifica. Ci sono #{Group.count} che usano questo bot, comunque." end if text == '/start' && type == 'private' client.get "http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{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%C3%B2 gli asd, altrimenti digita /grafico per il tuo grafico personal personal." end if text == '/grafico' && type == 'private' - unless Sender.find_by(chat_id: id) + unless Sender.find_by(chat_id: fromid) client.get "http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{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: id) + @sender = Sender.find_by(chat_id: fromid) 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 client.get "http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{id}&text=Guarda il tuo grafico personalizzato per il gruppo su #{ENV['DOMAIN']}/grafico?s=#{@sender.chat_id}" end end if text == '/grafico' && (type == 'group' || type == 'supergroup') unless Group.find_by(chat_id: id) client.get "http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{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: id) position = Group.all.sort_by{|group| group.asds.count}.pluck(:id).reverse.find_index(@group.id) + 1 client.get "http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{id}&text=Guarda il tuo grafico personalizzato per il gruppo su #{ENV['DOMAIN']}/grafico?g=#{@group.chat_id}" end end if text == '/nightsend' && (type == 'group' || type == 'supergroup') unless Group.find_by(chat_id: id) client.get "http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{id}&text=Ok, l'impostazione predefinita %C3%A8 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: id) if @group.nightsend client.get "http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{id}&text=Ok, l'impostazione predefinita %C3%A8 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 client.get "http://api.telegram.org/bot#{bot_api_key}/sendMessage?chat_id=#{id}&text=Sei una persona triste, asd. Vuoi che il conteggio venga inviato a mezzanotte. Bozzolan dice s%C3%AC, Ferdi dice no. Tu dici s%C3%AC, allora conteggio a mezzanotte sia, asd" @group.update_attribute(:nightsend, true) end end end render nothing: true end end \ No newline at end of file diff --git a/db/migrate/20190811141634_change_chat_id_to_be_bigint_in_groups.rb b/db/migrate/20190811141634_change_chat_id_to_be_bigint_in_groups.rb new file mode 100644 index 0000000..7c9ea62 --- /dev/null +++ b/db/migrate/20190811141634_change_chat_id_to_be_bigint_in_groups.rb @@ -0,0 +1,5 @@ +class ChangeChatIdToBeBigintInGroups < ActiveRecord::Migration[5.2] + def change + change_column :groups, :chat_id, :bigint + end +end diff --git a/db/schema.rb b/db/schema.rb index 70222ec..9c7153e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,71 +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_11_114154) do +ActiveRecord::Schema.define(version: 2019_08_11_141634) 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.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.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