chatgpt_assistant 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.env_sample +1 -1
- data/.rubocop.yml +4 -4
- data/Gemfile +18 -6
- data/Gemfile.lock +62 -59
- data/README.md +14 -13
- data/docker-compose.prod.yml +1 -1
- data/docker-compose.yml +18 -0
- data/exe/chatgpt_bot +3 -0
- data/lib/bots/application_bot.rb +15 -5
- data/lib/bots/discord_bot.rb +6 -9
- data/lib/bots/helpers/authentication_helper.rb +3 -2
- data/lib/bots/helpers/discord_helper.rb +4 -4
- data/lib/bots/helpers/messenger_helper.rb +14 -15
- data/lib/bots/helpers/search_helper.rb +3 -3
- data/lib/bots/helpers/telegram_helper.rb +3 -7
- data/lib/bots/helpers/validation_helper.rb +1 -1
- data/lib/bots/helpers/visit_helper.rb +4 -0
- data/lib/bots/jobs/new_chat_job.rb +17 -0
- data/lib/bots/jobs/register_job.rb +16 -0
- data/lib/bots/jobs/voice_connect_job.rb +14 -0
- data/lib/bots/services/new_chat_service.rb +34 -0
- data/lib/bots/services/register_service.rb +36 -0
- data/lib/bots/services/voice_connect_service.rb +29 -0
- data/lib/bots/telegram_bot.rb +70 -39
- data/lib/chatgpt_assistant/config.rb +23 -1
- data/lib/chatgpt_assistant/default_messages.rb +72 -30
- data/lib/chatgpt_assistant/error.rb +219 -0
- data/lib/chatgpt_assistant/models.rb +18 -13
- data/lib/chatgpt_assistant/sidekiq.rb +7 -0
- data/lib/chatgpt_assistant/sidekiq.yml +10 -0
- data/lib/chatgpt_assistant/version.rb +1 -1
- data/lib/chatgpt_assistant.rb +3 -12
- data/prompts-data/{awesome-chatgpt-prompts.csv → awesome-en-prompts.csv} +164 -164
- data/prompts-data/awesome-pt-prompts.csv +164 -0
- metadata +14 -5
- data/lib/bots/helpers/logger_action_helper.rb +0 -7
@@ -5,7 +5,7 @@ module ChatgptAssistant
|
|
5
5
|
module MessengerHelper
|
6
6
|
def chat_success(chat_id)
|
7
7
|
user_message = Message.new(chat_id: chat_id, content: msg.text, role: "user")
|
8
|
-
if user_message
|
8
|
+
if user_message&.save
|
9
9
|
text = chatter.chat(msg.text, chat_id, error_messages[:something_went_wrong])
|
10
10
|
mess = parse_message(text, 4096)
|
11
11
|
mess.each { |m| send_message m, msg.chat.id }
|
@@ -53,27 +53,27 @@ module ChatgptAssistant
|
|
53
53
|
|
54
54
|
def user_logged_message
|
55
55
|
user.update(telegram_id: msg.chat.id)
|
56
|
-
bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:user_logged_in])
|
56
|
+
bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:user_logged_in]) if bot.respond_to?(:api)
|
57
57
|
evnt&.respond success_messages[:user_logged_in] if evnt.present?
|
58
58
|
end
|
59
59
|
|
60
60
|
def invalid_command_error_message
|
61
|
-
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:invalid_command])
|
61
|
+
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:invalid_command]) if bot.respond_to?(:api)
|
62
62
|
evnt&.respond error_messages[:invalid_command]
|
63
63
|
end
|
64
64
|
|
65
65
|
def user_not_found_error_message
|
66
|
-
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:user_not_found]) if bot.api
|
66
|
+
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:user_not_found]) if bot.respond_to?(:api)
|
67
67
|
evnt&.respond error_messages[:user_not_found] if evnt.present?
|
68
68
|
end
|
69
69
|
|
70
70
|
def user_created_message
|
71
|
-
bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:user_created]) if bot.api
|
71
|
+
bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:user_created]) if bot.respond_to?(:api)
|
72
72
|
evnt&.respond success_messages[:user_created] if evnt.present?
|
73
73
|
end
|
74
74
|
|
75
75
|
def user_creation_error_message
|
76
|
-
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:user_creation]) if bot.api
|
76
|
+
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:user_creation]) if bot.respond_to?(:api)
|
77
77
|
evnt&.respond error_messages[:user_creation] if evnt.present?
|
78
78
|
end
|
79
79
|
|
@@ -87,47 +87,46 @@ module ChatgptAssistant
|
|
87
87
|
def telegram_created(chat)
|
88
88
|
bot.api.send_message(chat_id: msg.chat.id, text: "Intructions sended to actor:\n#{chat.prompt}") unless chat.actor.nil?
|
89
89
|
bot.api.send_message(chat_id: msg.chat.id, text: "Response from assistant:\n#{chat.messages[1].content}") unless chat.actor.nil?
|
90
|
-
bot.api.send_message(chat_id: msg.chat.id, text: success_messages[:chat_created]) if bot.api
|
90
|
+
bot.api.send_message(chat_id: msg.chat.id, text: success_messages[:chat_created]) if bot.respond_to?(:api)
|
91
91
|
end
|
92
92
|
|
93
93
|
def discord_created(chat)
|
94
94
|
evnt.respond "Intructions sended to actor:\n#{chat.prompt}" unless chat.actor.nil?
|
95
|
-
evnt.respond "Response from assistant:\n#{chat.messages[1].content}" unless chat.actor.nil?
|
96
95
|
evnt.respond success_messages[:chat_created] if evnt.present?
|
97
96
|
end
|
98
97
|
|
99
98
|
def not_logged_in_message
|
100
|
-
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:user_not_logged_in])
|
99
|
+
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:user_not_logged_in]) if bot.respond_to?(:api)
|
101
100
|
evnt&.respond(error_messages[:user_not_logged_in])
|
102
101
|
end
|
103
102
|
|
104
103
|
def wrong_password_error_message
|
105
|
-
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:password])
|
104
|
+
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:password]) if bot.respond_to?(:api)
|
106
105
|
evnt&.respond(error_messages[:password])
|
107
106
|
end
|
108
107
|
|
109
108
|
def chat_not_found_message
|
110
|
-
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:chat_not_found])
|
109
|
+
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:chat_not_found]) if bot.respond_to?(:api)
|
111
110
|
evnt&.respond(error_messages[:chat_not_found])
|
112
111
|
end
|
113
112
|
|
114
113
|
def no_chat_selected_message
|
115
|
-
bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:no_chat_selected])
|
114
|
+
bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:no_chat_selected]) if bot.respond_to?(:api)
|
116
115
|
evnt&.respond(error_messages[:no_chat_selected])
|
117
116
|
end
|
118
117
|
|
119
118
|
def no_messages_founded_message
|
120
|
-
bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:no_messages_founded])
|
119
|
+
bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:no_messages_founded]) if bot.respond_to?(:api)
|
121
120
|
evnt&.respond(error_messages[:no_messages_founded])
|
122
121
|
end
|
123
122
|
|
124
123
|
def chat_creation_failed_message
|
125
|
-
bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:chat_creation_failed])
|
124
|
+
bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:chat_creation_failed]) if bot.respond_to?(:api)
|
126
125
|
evnt&.respond(error_messages[:chat_creation_failed])
|
127
126
|
end
|
128
127
|
|
129
128
|
def user_logged_in_message
|
130
|
-
bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:user_logged_in])
|
129
|
+
bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:user_logged_in]) if bot.respond_to?(:api)
|
131
130
|
evnt&.respond(success_messages[:user_logged_in])
|
132
131
|
end
|
133
132
|
end
|
@@ -23,11 +23,11 @@ module ChatgptAssistant
|
|
23
23
|
|
24
24
|
def where_user(telegram_id: nil, discord_id: nil, email: nil)
|
25
25
|
if telegram_id
|
26
|
-
User.where(telegram_id: telegram_id)
|
26
|
+
User.where(telegram_id: telegram_id).to_a
|
27
27
|
elsif discord_id
|
28
|
-
User.where(discord_id: discord_id)
|
28
|
+
User.where(discord_id: discord_id).to_a
|
29
29
|
elsif email
|
30
|
-
User.where(email: email)
|
30
|
+
User.where(email: email).to_a
|
31
31
|
end
|
32
32
|
end
|
33
33
|
end
|
@@ -25,11 +25,11 @@ module ChatgptAssistant
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def telegram_send_start_message
|
28
|
-
send_message
|
28
|
+
send_message common_messages[:start], msg.chat.id
|
29
29
|
help_message = help_messages.join("\n").to_s
|
30
30
|
send_message help_message, msg.chat.id
|
31
|
-
send_message
|
32
|
-
send_message
|
31
|
+
send_message common_messages[:start_helper], msg.chat.id
|
32
|
+
send_message common_messages[:start_sec_helper], msg.chat.id
|
33
33
|
end
|
34
34
|
|
35
35
|
def telegram_create_chat
|
@@ -54,10 +54,6 @@ module ChatgptAssistant
|
|
54
54
|
chat.save ? chat_created_message(chat) : chat_creation_failed_message
|
55
55
|
end
|
56
56
|
|
57
|
-
def telegram_user_history
|
58
|
-
user.current_chat.messages.last(10).map { |m| "#{m.role}: #{m.content}\nat: #{m.created_at}" }
|
59
|
-
end
|
60
|
-
|
61
57
|
def telegram_text_or_audio?
|
62
58
|
msg.respond_to?(:text) || msg.respond_to?(:audio) || msg.respond_to?(:voice)
|
63
59
|
end
|
@@ -27,7 +27,7 @@ module ChatgptAssistant
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def discord_next_action?
|
30
|
-
return true if evnt.channel.type != 1
|
30
|
+
return true if evnt.channel.type != 1 && evnt.channel.name != "ai-spaces"
|
31
31
|
|
32
32
|
%w[login register start help new_chat sl_chat ask list hist connect disconnect speak].each do |action|
|
33
33
|
return true if evnt.message.content.include?("#{discord_prefix}#{action}")
|
@@ -4,6 +4,8 @@ module ChatgptAssistant
|
|
4
4
|
# Helper for visit
|
5
5
|
module VisitHelper
|
6
6
|
def telegram_visited?(chat_id)
|
7
|
+
return unless msg
|
8
|
+
|
7
9
|
visitor = Visitor.find_by(telegram_id: chat_id, name: msg.from.first_name)
|
8
10
|
if visitor.nil?
|
9
11
|
Visitor.create(telegram_id: chat_id, name: msg.from.first_name)
|
@@ -13,6 +15,8 @@ module ChatgptAssistant
|
|
13
15
|
end
|
14
16
|
|
15
17
|
def discord_visited?(user_id)
|
18
|
+
return unless evnt
|
19
|
+
|
16
20
|
visitor = Visitor.find_by(discord_id: user_id, name: evnt.user.name)
|
17
21
|
if visitor.nil?
|
18
22
|
Visitor.create(discord_id: user_id, name: evnt.user.name)
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sidekiq"
|
4
|
+
|
5
|
+
module ChatgptAssistant
|
6
|
+
# This class is responsible to background the new chat service
|
7
|
+
class NewChatJob
|
8
|
+
include Sidekiq::Job
|
9
|
+
|
10
|
+
def perform(chat_title, user_id, chat_id)
|
11
|
+
@config = Config.new
|
12
|
+
@config.db_connection
|
13
|
+
|
14
|
+
NewChatService.new(chat_title, user_id, chat_id, @config).call
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sidekiq"
|
4
|
+
|
5
|
+
module ChatgptAssistant
|
6
|
+
# This class is responsible to background the register service
|
7
|
+
class RegisterJob
|
8
|
+
include Sidekiq::Job
|
9
|
+
|
10
|
+
sidekiq_options queue: :default
|
11
|
+
|
12
|
+
def perform(email, password, chat_id)
|
13
|
+
RegisterService.new(email, password, chat_id).call
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sidekiq"
|
4
|
+
|
5
|
+
module ChatgptAssistant
|
6
|
+
# This class is responsible to background the voice connect service
|
7
|
+
class VoiceConnectJob
|
8
|
+
include Sidekiq::Worker
|
9
|
+
|
10
|
+
def perform(channel_id)
|
11
|
+
VoiceConnectService.new(channel_id).call
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ChatgptAssistant
|
4
|
+
# This class is responsible to background the new chat service
|
5
|
+
class NewChatService
|
6
|
+
def initialize(chat_title, user_id, chat_id, config)
|
7
|
+
@chat_title = chat_title
|
8
|
+
@user_id = user_id
|
9
|
+
@chat_id = chat_id
|
10
|
+
@config = config
|
11
|
+
end
|
12
|
+
|
13
|
+
def telegram_async
|
14
|
+
TelegramBot.new(@config)
|
15
|
+
end
|
16
|
+
|
17
|
+
def call
|
18
|
+
raise ChatAlreadyExistsError if Chat.find_by(title: @chat_title)
|
19
|
+
raise NilError if @chat_title.nil?
|
20
|
+
raise NilError if @user_id.nil?
|
21
|
+
raise NilError if @chat_id.nil?
|
22
|
+
|
23
|
+
chat = Chat.new(title: @chat_title, user_id: @user_id)
|
24
|
+
if chat.save
|
25
|
+
telegram_async.send_message("Chat created succesfully", @chat_id)
|
26
|
+
else
|
27
|
+
raise ChatAlreadyExistsError if Chat.find_by(title: @chat_title)
|
28
|
+
raise NilError if chat.errors[:title].any?
|
29
|
+
end
|
30
|
+
rescue ChatAlreadyExistsError, NilError => e
|
31
|
+
telegram_async.send_message(e.message, @chat_id)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ChatgptAssistant
|
4
|
+
# This class is responsible to background the register service
|
5
|
+
class RegisterService
|
6
|
+
def initialize(email, password, chat_id)
|
7
|
+
@email = email
|
8
|
+
@password = password
|
9
|
+
@chat_id = chat_id
|
10
|
+
@config = Config.new
|
11
|
+
@config.db_connection
|
12
|
+
end
|
13
|
+
|
14
|
+
def telegram_async
|
15
|
+
TelegramBot.new(@config)
|
16
|
+
end
|
17
|
+
|
18
|
+
def call
|
19
|
+
user = User.find_by(email: @email)
|
20
|
+
if user
|
21
|
+
telegram_async.send_message("User already exists", @chat_id)
|
22
|
+
else
|
23
|
+
user = User.new(email: @email, password: @password, telegram_id: @chat_id)
|
24
|
+
if user.save
|
25
|
+
telegram_async.send_message("User created", @chat_id)
|
26
|
+
else
|
27
|
+
raise UserAlreadyExistsError if User.find_by(email: @email)
|
28
|
+
raise WrongEmailError if user.errors[:email].any?
|
29
|
+
raise WrongPasswordError if user.errors[:password].any?
|
30
|
+
end
|
31
|
+
end
|
32
|
+
rescue UserAlreadyExistsError, WrongEmailError, WrongPasswordError => e
|
33
|
+
telegram_async.send_message(e.message, @chat_id)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ChatgptAssistant
|
4
|
+
# This class is responsible to handle the discord bot
|
5
|
+
class VoiceConnectService
|
6
|
+
def initialize(channel_id)
|
7
|
+
@channel_id = channel_id
|
8
|
+
@config = Config.new
|
9
|
+
# @config.db_connection
|
10
|
+
end
|
11
|
+
|
12
|
+
def bot
|
13
|
+
@bot ||= Discordrb::Bot.new(token: @config.discord_token)
|
14
|
+
end
|
15
|
+
|
16
|
+
def call
|
17
|
+
bot.run :async
|
18
|
+
channel = bot.channel(@channel_id)
|
19
|
+
|
20
|
+
bot.voice_connect(channel)
|
21
|
+
bot.join
|
22
|
+
true
|
23
|
+
rescue StandardError => e
|
24
|
+
puts e.message
|
25
|
+
e.backtrace.each { |line| puts line }
|
26
|
+
false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/bots/telegram_bot.rb
CHANGED
@@ -8,8 +8,6 @@ module ChatgptAssistant
|
|
8
8
|
class TelegramBot < ApplicationBot
|
9
9
|
def start
|
10
10
|
bot.listen do |message|
|
11
|
-
next if message.chat.type != "private" # disable group and channel messages, we will enable it later
|
12
|
-
|
13
11
|
@msg = message
|
14
12
|
@visitor = telegram_visited?(@msg.chat.id)
|
15
13
|
next unless telegram_text_or_audio?
|
@@ -42,10 +40,12 @@ module ChatgptAssistant
|
|
42
40
|
when "/stop"
|
43
41
|
stop_event
|
44
42
|
when nil
|
45
|
-
|
43
|
+
raise NilError
|
46
44
|
else
|
47
45
|
action_events
|
48
46
|
end
|
47
|
+
rescue NilError => e
|
48
|
+
send_message e.message, msg.chat.id
|
49
49
|
end
|
50
50
|
|
51
51
|
def start_event
|
@@ -57,93 +57,124 @@ module ChatgptAssistant
|
|
57
57
|
end
|
58
58
|
|
59
59
|
def hist_event
|
60
|
-
|
61
|
-
|
62
|
-
|
60
|
+
raise UserNotLoggedInError if user.nil?
|
61
|
+
raise NoChatSelectedError if user.current_chat.nil?
|
62
|
+
raise NoMessagesFoundedError if user.current_chat.messages.count.zero?
|
63
63
|
|
64
|
-
|
64
|
+
user.chat_history.each do |m|
|
65
65
|
send_message m, msg.chat.id
|
66
66
|
end
|
67
|
+
rescue NoChatSelectedError, UserNotLoggedInError, NoMessagesFoundedError => e
|
68
|
+
send_message e.message, msg.chat.id
|
67
69
|
end
|
68
70
|
|
69
71
|
def list_event
|
70
|
-
|
72
|
+
raise UserNotLoggedInError if user.nil?
|
73
|
+
raise NoChatsFoundedError if user.chats.count.zero?
|
71
74
|
|
72
|
-
send_message
|
75
|
+
send_message common_messages[:chat_list], msg.chat.id
|
73
76
|
chats_str = ""
|
74
77
|
user.chats.each_with_index { |c, i| chats_str += "Chat #{i + 1} - #{c.title}\n" }
|
75
78
|
send_message chats_str, msg.chat.id
|
79
|
+
rescue NoChatsFoundedError, UserNotLoggedInError => e
|
80
|
+
send_message e.message, msg.chat.id
|
76
81
|
end
|
77
82
|
|
78
83
|
def action_events
|
79
|
-
return
|
80
|
-
return register_event if msg.text.include?("register/")
|
84
|
+
return auth_events if auth_event?
|
81
85
|
return new_chat_event if msg.text.include?("new_chat/")
|
82
86
|
return select_chat_event if msg.text.include?("sl_chat/")
|
83
87
|
return telegram_chat_event unless telegram_actions?
|
84
88
|
|
85
|
-
|
89
|
+
raise InvalidCommandError
|
90
|
+
rescue InvalidCommandError => e
|
91
|
+
send_message e.message, msg.chat.id
|
92
|
+
end
|
93
|
+
|
94
|
+
def auth_event?
|
95
|
+
msg.text.include?("login/") || msg.text.include?("register/") || msg.text.include?("sign_out/")
|
96
|
+
end
|
97
|
+
|
98
|
+
def auth_events
|
99
|
+
return login_event if msg.text.include?("login/")
|
100
|
+
return register_event if msg.text.include?("register/")
|
101
|
+
return sign_out_event if msg.text.include?("sign_out/")
|
86
102
|
end
|
87
103
|
|
88
104
|
def login_event
|
105
|
+
raise UserLoggedInError if user
|
106
|
+
|
89
107
|
user_info = msg.text.split("/").last
|
90
108
|
email, password = user_info.split(":")
|
91
109
|
case telegram_user_auth(email, password, msg.chat.id)
|
92
110
|
when "user not found"
|
93
|
-
|
111
|
+
raise UserNotFoundError
|
94
112
|
when "wrong password"
|
95
|
-
|
113
|
+
raise WrongPasswordError
|
96
114
|
when email
|
97
115
|
user_logged_in_message
|
98
116
|
end
|
117
|
+
rescue UserNotFoundError, WrongPasswordError, UserLoggedInError => e
|
118
|
+
send_message e.message, msg.chat.id
|
99
119
|
end
|
100
120
|
|
101
121
|
def register_event
|
102
122
|
user_info = msg.text.split("/").last
|
123
|
+
raise NoRegisterInfoError if user_info.nil?
|
124
|
+
raise UserLoggedInError if user
|
125
|
+
|
103
126
|
email, password = user_info.split(":")
|
104
|
-
|
105
|
-
|
127
|
+
raise NoRegisterInfoError if email.nil? || password.nil?
|
128
|
+
|
129
|
+
RegisterJob.perform_async(email, password, visitor.telegram_id)
|
130
|
+
rescue NoRegisterInfoError, UserLoggedInError => e
|
131
|
+
send_message e.message, msg.chat.id
|
132
|
+
end
|
133
|
+
|
134
|
+
def sign_out_event
|
135
|
+
raise UserNotLoggedInError if user.nil?
|
136
|
+
|
137
|
+
user.update(telegram_id: nil)
|
138
|
+
send_message success_messages[:user_logged_out], msg.chat.id
|
139
|
+
rescue UserNotLoggedInError => e
|
140
|
+
send_message e.message, msg.chat.id
|
106
141
|
end
|
107
142
|
|
108
143
|
def new_chat_event
|
109
|
-
|
144
|
+
raise UserNotLoggedInError if user.nil?
|
110
145
|
|
111
|
-
|
146
|
+
NewChatJob.perform_async(msg.text.split("/").last, user.id, msg.chat.id)
|
147
|
+
rescue UserNotLoggedInError => e
|
148
|
+
send_message e.message, msg.chat.id
|
112
149
|
end
|
113
150
|
|
114
151
|
def select_chat_event
|
115
|
-
|
152
|
+
raise UserNotLoggedInError if user.nil?
|
116
153
|
|
117
154
|
title = msg.text.split("/").last
|
118
155
|
chat = user.chat_by_title(title)
|
119
|
-
|
156
|
+
raise ChatNotFoundError if chat.nil?
|
120
157
|
|
121
|
-
user.update(current_chat_id: chat.id)
|
122
|
-
send_message success_messages[:chat_selected], msg.chat.id
|
123
|
-
end
|
158
|
+
raise ChatNotFoundError unless user.update(current_chat_id: chat.id)
|
124
159
|
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
end
|
129
|
-
|
130
|
-
def nil_event
|
131
|
-
send_message error_messages[:nil], msg.chat.id
|
160
|
+
send_message success_messages[:chat_selected], msg.chat.id
|
161
|
+
rescue UserNotLoggedInError, ChatNotFoundError => e
|
162
|
+
send_message e.message, msg.chat.id
|
132
163
|
end
|
133
164
|
|
134
165
|
def audio_event
|
135
|
-
|
136
|
-
|
166
|
+
raise UserNotLoggedInError if user.nil?
|
167
|
+
raise NoChatSelectedError if user.current_chat.nil?
|
137
168
|
|
138
169
|
user_audio = transcribe_file(telegram_audio_url)
|
139
170
|
message = Message.new(content: user_audio[:text], chat_id: user.current_chat_id, role: "user")
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
171
|
+
raise MessageNotSavedError unless message.save
|
172
|
+
|
173
|
+
ai_response = telegram_process_ai_voice(user_audio[:file])
|
174
|
+
telegram_send_voice_message(voice: ai_response[:voice], text: ai_response[:text])
|
175
|
+
delete_file ai_response[:voice]
|
176
|
+
rescue UserNotLoggedInError, NoChatSelectedError, MessageNotSavedError => e
|
177
|
+
send_message e.message, msg.chat.id
|
147
178
|
end
|
148
179
|
end
|
149
180
|
end
|
@@ -44,9 +44,25 @@ module ChatgptAssistant
|
|
44
44
|
ActiveRecord::Base.logger = Logger.new($stdout) if ENV["ENV_TYPE"] == "development"
|
45
45
|
end
|
46
46
|
|
47
|
+
def create_db
|
48
|
+
db_connection
|
49
|
+
return if database_exists?
|
50
|
+
|
51
|
+
ActiveRecord::Base.establish_connection(
|
52
|
+
adapter: "postgresql",
|
53
|
+
host: database_host,
|
54
|
+
port: 5432,
|
55
|
+
database: "postgres",
|
56
|
+
username: database_username,
|
57
|
+
password: database_password
|
58
|
+
)
|
59
|
+
ActiveRecord::Base.logger = Logger.new($stdout) if ENV["ENV_TYPE"] == "development"
|
60
|
+
ActiveRecord::Base.connection.create_database(database_name)
|
61
|
+
end
|
62
|
+
|
47
63
|
def migrate
|
48
64
|
db_connection
|
49
|
-
ActiveRecord::Base.logger = Logger.new($stdout)
|
65
|
+
ActiveRecord::Base.logger = Logger.new($stdout) if ENV["ENV_TYPE"] == "development"
|
50
66
|
VisitorMigration.new.migrate(:up)
|
51
67
|
UserMigration.new.migrate(:up)
|
52
68
|
ChatMigration.new.migrate(:up)
|
@@ -57,5 +73,11 @@ module ChatgptAssistant
|
|
57
73
|
private
|
58
74
|
|
59
75
|
attr_reader :database_host, :database_name, :database_username, :database_password
|
76
|
+
|
77
|
+
def database_exists?
|
78
|
+
ActiveRecord::Base.connection
|
79
|
+
rescue ActiveRecord::NoDatabaseError
|
80
|
+
false
|
81
|
+
end
|
60
82
|
end
|
61
83
|
end
|