Add telegram subscriber ids to Bots
This commit is contained in:
parent
75e83b8e84
commit
f172464666
@ -26,7 +26,11 @@ a
|
|||||||
color: gold
|
color: gold
|
||||||
|
|
||||||
p
|
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
|
h1
|
||||||
font-size: 7vmin
|
font-size: 7vmin
|
||||||
|
|||||||
@ -18,6 +18,7 @@ class BotsController < ApplicationController
|
|||||||
|
|
||||||
# GET /bots/1/edit
|
# GET /bots/1/edit
|
||||||
def edit
|
def edit
|
||||||
|
authorize @bot
|
||||||
end
|
end
|
||||||
|
|
||||||
def telegram_listen
|
def telegram_listen
|
||||||
@ -43,6 +44,7 @@ class BotsController < ApplicationController
|
|||||||
|
|
||||||
# PATCH/PUT /bots/1 or /bots/1.json
|
# PATCH/PUT /bots/1 or /bots/1.json
|
||||||
def update
|
def update
|
||||||
|
authorize @bot
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
if @bot.update(bot_params)
|
if @bot.update(bot_params)
|
||||||
format.html { redirect_to bot_url(@bot), notice: "Bot was successfully updated." }
|
format.html { redirect_to bot_url(@bot), notice: "Bot was successfully updated." }
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
class Bot < ActiveRecord::Base
|
class Bot < ActiveRecord::Base
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to :profile, optional: true
|
belongs_to :profile, optional: true
|
||||||
|
serialize :telegram_subscriber_ids, Array
|
||||||
|
|
||||||
# def send_text
|
# def send_text
|
||||||
# if self.telegram_api_token
|
# if self.telegram_api_token
|
||||||
@ -28,8 +29,9 @@ class Bot < ActiveRecord::Base
|
|||||||
when Telegram::Bot::Types::Message
|
when Telegram::Bot::Types::Message
|
||||||
# Debugging use next to skip/drop the queue
|
# Debugging use next to skip/drop the queue
|
||||||
# next
|
# next
|
||||||
|
# eval(IO.read('./app/models/commands/fortune.rb'), binding)
|
||||||
case message.text
|
case message.text
|
||||||
when /^\/fortune/i
|
when /^\/fortune/i
|
||||||
response_text = `fortune -a`
|
response_text = `fortune -a`
|
||||||
begin
|
begin
|
||||||
bot.api.send_message(chat_id: message.chat.id, reply_to_message_id: message.message_id, text: response_text, parse_mode: "Markdown")
|
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"
|
puts "Error from fortune"
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -4,6 +4,7 @@
|
|||||||
.form-inputs
|
.form-inputs
|
||||||
= f.input :name
|
= f.input :name
|
||||||
= f.input :telegram_api_token
|
= f.input :telegram_api_token
|
||||||
|
-# = f.input :telegram_subscriber_ids
|
||||||
= f.input :discord_api_token
|
= f.input :discord_api_token
|
||||||
= f.hidden_field :user_id
|
= f.hidden_field :user_id
|
||||||
= f.association :profile
|
= f.association :profile
|
||||||
|
|||||||
@ -5,15 +5,19 @@
|
|||||||
- if policy(@bot).edit?
|
- if policy(@bot).edit?
|
||||||
= link_to 'Edit', edit_bot_path(@bot)
|
= link_to 'Edit', edit_bot_path(@bot)
|
||||||
|
|
||||||
|
%p
|
||||||
|
%b Telegram subscribers:
|
||||||
|
= @bot.telegram_subscriber_ids
|
||||||
%p
|
%p
|
||||||
|
- if policy(@bot).telegram_listen?
|
||||||
|
= link_to 'Start', telegram_listen_bot_path(@bot)
|
||||||
|
%br
|
||||||
%b Telegram api token:
|
%b Telegram api token:
|
||||||
= @bot.telegram_api_token
|
= @bot.telegram_api_token
|
||||||
- if policy(@bot).telegram_listen?
|
|
||||||
%br= link_to 'Start', telegram_listen_bot_path(@bot)
|
|
||||||
%p
|
%p
|
||||||
%b Discord api token:
|
%b Discord api token:
|
||||||
= @bot.discord_api_token
|
= @bot.discord_api_token
|
||||||
%p
|
-# %p
|
||||||
%b User:
|
%b User:
|
||||||
= link_to @bot.user, @bot.user
|
= link_to @bot.user, @bot.user
|
||||||
- if @bot.profile
|
- if @bot.profile
|
||||||
|
|||||||
@ -0,0 +1,5 @@
|
|||||||
|
class AddTelegramSubscriberIdsToBot < ActiveRecord::Migration[6.1]
|
||||||
|
def change
|
||||||
|
add_column :bots, :telegram_subscriber_ids, :text
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -10,7 +10,7 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# 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
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
@ -23,6 +23,7 @@ ActiveRecord::Schema.define(version: 2023_11_28_174336) do
|
|||||||
t.bigint "profile_id"
|
t.bigint "profile_id"
|
||||||
t.datetime "created_at", precision: 6, null: false
|
t.datetime "created_at", precision: 6, null: false
|
||||||
t.datetime "updated_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 ["profile_id"], name: "index_bots_on_profile_id"
|
||||||
t.index ["user_id"], name: "index_bots_on_user_id"
|
t.index ["user_id"], name: "index_bots_on_user_id"
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user