Add telegram subscriber ids to Bots

This commit is contained in:
TheNeedful-DO 2023-11-29 17:39:31 +00:00
parent 75e83b8e84
commit f172464666
7 changed files with 57 additions and 6 deletions

View File

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

View File

@ -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." }

View File

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

View File

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

View File

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

View File

@ -0,0 +1,5 @@
class AddTelegramSubscriberIdsToBot < ActiveRecord::Migration[6.1]
def change
add_column :bots, :telegram_subscriber_ids, :text
end
end

View File

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