From f1724646660a85a42f4b1d4b5fbc30eb16810cef Mon Sep 17 00:00:00 2001 From: TheNeedful-DO Date: Wed, 29 Nov 2023 17:39:31 +0000 Subject: [PATCH] Add telegram subscriber ids to Bots --- app/assets/stylesheets/application.css.sass | 6 +++- app/controllers/bots_controller.rb | 2 ++ app/models/bot.rb | 36 ++++++++++++++++++- app/views/bots/_form.html.haml | 1 + app/views/bots/show.html.haml | 10 ++++-- ...1945_add_telegram_subscriber_ids_to_bot.rb | 5 +++ db/schema.rb | 3 +- 7 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20231129171945_add_telegram_subscriber_ids_to_bot.rb diff --git a/app/assets/stylesheets/application.css.sass b/app/assets/stylesheets/application.css.sass index 0026ea23..14a4c6c5 100644 --- a/app/assets/stylesheets/application.css.sass +++ b/app/assets/stylesheets/application.css.sass @@ -26,7 +26,11 @@ a color: gold p - // margin: 0.4em 0 + font-family: sans + text-shadow: none + b + font-family: "Anton", Impact, fantasy, sans-serif + text-shadow: -1px 1px #e1b027, 1px -1px #00ddff h1 font-size: 7vmin diff --git a/app/controllers/bots_controller.rb b/app/controllers/bots_controller.rb index 5bac86dd..d9956e6a 100644 --- a/app/controllers/bots_controller.rb +++ b/app/controllers/bots_controller.rb @@ -18,6 +18,7 @@ class BotsController < ApplicationController # GET /bots/1/edit def edit + authorize @bot end def telegram_listen @@ -43,6 +44,7 @@ class BotsController < ApplicationController # PATCH/PUT /bots/1 or /bots/1.json def update + authorize @bot respond_to do |format| if @bot.update(bot_params) format.html { redirect_to bot_url(@bot), notice: "Bot was successfully updated." } diff --git a/app/models/bot.rb b/app/models/bot.rb index 76db0bbc..825fc39b 100644 --- a/app/models/bot.rb +++ b/app/models/bot.rb @@ -1,6 +1,7 @@ class Bot < ActiveRecord::Base belongs_to :user belongs_to :profile, optional: true + serialize :telegram_subscriber_ids, Array # def send_text # if self.telegram_api_token @@ -28,8 +29,9 @@ class Bot < ActiveRecord::Base when Telegram::Bot::Types::Message # Debugging use next to skip/drop the queue # next + # eval(IO.read('./app/models/commands/fortune.rb'), binding) case message.text - when /^\/fortune/i + when /^\/fortune/i response_text = `fortune -a` begin bot.api.send_message(chat_id: message.chat.id, reply_to_message_id: message.message_id, text: response_text, parse_mode: "Markdown") @@ -37,6 +39,38 @@ class Bot < ActiveRecord::Base puts "Error from fortune" end end + case message.text + when /^\/subscribe/i, /^\/sub/i, /^\/start/i + response_text = `fortune -a` + if self.telegram_subscriber_ids.include? message.chat.id + response_text = "You remain subscribed to #{bot.api.call("getMe")["result"]["first_name"]}\n\n" + response_text + else + self.telegram_subscriber_ids << message.chat.id + self.save + response_text = "Now subscribed to #{bot.api.call("getMe")["result"]["first_name"]}\n\n" + response_text + end + begin + bot.api.send_message(chat_id: message.chat.id, reply_to_message_id: message.message_id, text: response_text, parse_mode: "Markdown") + rescue + puts "Error from subscribe / start" + end + end + case message.text + when /^\/stop/i, /^\/unsub/i,/^\/unsubscribe/i + response_text = `fortune -a` + if self.telegram_subscriber_ids.include? message.chat.id + self.telegram_subscriber_ids.delete message.chat.id + self.save + response_text = "You are now unsubscribed from #{bot.api.call("getMe")["result"]["first_name"]}\n\n" + response_text + else + response_text = "You remain unsubscribed to #{bot.api.call("getMe")["result"]["first_name"]}\n\n" + response_text + end + begin + bot.api.send_message(chat_id: message.chat.id, reply_to_message_id: message.message_id, text: response_text, parse_mode: "Markdown") + rescue + puts "Error from start" + end + end end end end diff --git a/app/views/bots/_form.html.haml b/app/views/bots/_form.html.haml index 83ae8f21..487245df 100644 --- a/app/views/bots/_form.html.haml +++ b/app/views/bots/_form.html.haml @@ -4,6 +4,7 @@ .form-inputs = f.input :name = f.input :telegram_api_token + -# = f.input :telegram_subscriber_ids = f.input :discord_api_token = f.hidden_field :user_id = f.association :profile diff --git a/app/views/bots/show.html.haml b/app/views/bots/show.html.haml index 2ac10a2e..25d63131 100644 --- a/app/views/bots/show.html.haml +++ b/app/views/bots/show.html.haml @@ -5,15 +5,19 @@ - if policy(@bot).edit? = link_to 'Edit', edit_bot_path(@bot) + %p + %b Telegram subscribers: + = @bot.telegram_subscriber_ids %p + - if policy(@bot).telegram_listen? + = link_to 'Start', telegram_listen_bot_path(@bot) + %br %b Telegram api token: = @bot.telegram_api_token - - if policy(@bot).telegram_listen? - %br= link_to 'Start', telegram_listen_bot_path(@bot) %p %b Discord api token: = @bot.discord_api_token - %p + -# %p %b User: = link_to @bot.user, @bot.user - if @bot.profile diff --git a/db/migrate/20231129171945_add_telegram_subscriber_ids_to_bot.rb b/db/migrate/20231129171945_add_telegram_subscriber_ids_to_bot.rb new file mode 100644 index 00000000..000c9fe3 --- /dev/null +++ b/db/migrate/20231129171945_add_telegram_subscriber_ids_to_bot.rb @@ -0,0 +1,5 @@ +class AddTelegramSubscriberIdsToBot < ActiveRecord::Migration[6.1] + def change + add_column :bots, :telegram_subscriber_ids, :text + end +end diff --git a/db/schema.rb b/db/schema.rb index fd6b306c..6ec194d3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2023_11_28_174336) do +ActiveRecord::Schema.define(version: 2023_11_29_171945) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -23,6 +23,7 @@ ActiveRecord::Schema.define(version: 2023_11_28_174336) do t.bigint "profile_id" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false + t.text "telegram_subscriber_ids" t.index ["profile_id"], name: "index_bots_on_profile_id" t.index ["user_id"], name: "index_bots_on_user_id" end