chatgpt_assistant 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.env_sample +16 -7
- data/.rubocop.yml +14 -3
- data/Dockerfile +11 -5
- data/Gemfile +25 -23
- data/Gemfile.lock +123 -85
- data/README.md +15 -18
- data/Rakefile +1 -0
- data/docker-compose.yml +8 -4
- data/exe/chatgpt_assistant +1 -1
- data/lib/{bots → chatgpt_assistant/bots}/application_bot.rb +0 -6
- data/lib/chatgpt_assistant/bots/discord/actions.rb +104 -0
- data/lib/chatgpt_assistant/bots/discord/auth.rb +36 -0
- data/lib/chatgpt_assistant/bots/discord/bot.rb +29 -0
- data/lib/chatgpt_assistant/bots/discord/chat_actions.rb +33 -0
- data/lib/chatgpt_assistant/bots/discord/events.rb +138 -0
- data/lib/chatgpt_assistant/bots/discord/validations.rb +38 -0
- data/lib/chatgpt_assistant/bots/discord/voice_actions.rb +33 -0
- data/lib/chatgpt_assistant/bots/discord/voice_checkers.rb +35 -0
- data/lib/chatgpt_assistant/bots/discord_bot.rb +42 -0
- data/lib/chatgpt_assistant/bots/helpers/messenger_helper.rb +16 -0
- data/lib/{bots → chatgpt_assistant/bots}/jobs/register_job.rb +2 -2
- data/lib/chatgpt_assistant/bots/mailers/account_mailer.rb +30 -0
- data/lib/chatgpt_assistant/bots/mailers/application_mailer.rb +21 -0
- data/lib/chatgpt_assistant/bots/services/new_chat_service.rb +59 -0
- data/lib/chatgpt_assistant/bots/services/register_service.rb +45 -0
- data/lib/chatgpt_assistant/bots/telegram/actions.rb +54 -0
- data/lib/chatgpt_assistant/bots/telegram/auth.rb +40 -0
- data/lib/chatgpt_assistant/bots/telegram/bot.rb +17 -0
- data/lib/chatgpt_assistant/bots/telegram/chat_actions.rb +30 -0
- data/lib/chatgpt_assistant/bots/telegram/events.rb +120 -0
- data/lib/chatgpt_assistant/bots/telegram/events_controller.rb +48 -0
- data/lib/chatgpt_assistant/bots/telegram/permissions.rb +48 -0
- data/lib/chatgpt_assistant/bots/telegram/validations.rb +55 -0
- data/lib/chatgpt_assistant/bots/telegram/voice_actions.rb +36 -0
- data/lib/chatgpt_assistant/bots/telegram_bot.rb +44 -0
- data/lib/chatgpt_assistant/chatter.rb +10 -2
- data/lib/chatgpt_assistant/config.rb +4 -1
- data/lib/chatgpt_assistant/default_messages.rb +10 -4
- data/lib/chatgpt_assistant/error.rb +9 -0
- data/lib/chatgpt_assistant/migrations.rb +5 -0
- data/lib/chatgpt_assistant/models.rb +20 -0
- data/lib/chatgpt_assistant/version.rb +1 -1
- data/lib/chatgpt_assistant.rb +5 -3
- metadata +34 -24
- data/.env_prod_sample +0 -18
- data/docker-compose.prod.yml +0 -34
- data/lib/bots/discord_bot.rb +0 -182
- data/lib/bots/helpers/authentication_helper.rb +0 -48
- data/lib/bots/helpers/discord_helper.rb +0 -124
- data/lib/bots/helpers/discord_voice_helper.rb +0 -50
- data/lib/bots/helpers/messenger_helper.rb +0 -133
- data/lib/bots/helpers/telegram_helper.rb +0 -73
- data/lib/bots/helpers/telegram_voice_helper.rb +0 -33
- data/lib/bots/helpers/validation_helper.rb +0 -38
- data/lib/bots/helpers/visit_helper.rb +0 -28
- data/lib/bots/services/new_chat_service.rb +0 -34
- data/lib/bots/services/register_service.rb +0 -36
- data/lib/bots/telegram_bot.rb +0 -180
- /data/lib/{bots → chatgpt_assistant/bots}/helpers/audio_helper.rb +0 -0
- /data/lib/{bots → chatgpt_assistant/bots}/helpers/file_helper.rb +0 -0
- /data/lib/{bots → chatgpt_assistant/bots}/helpers/search_helper.rb +0 -0
- /data/lib/{bots → chatgpt_assistant/bots}/jobs/new_chat_job.rb +0 -0
- /data/lib/{bots → chatgpt_assistant/bots}/jobs/voice_connect_job.rb +0 -0
- /data/lib/{bots → chatgpt_assistant/bots}/services/voice_connect_service.rb +0 -0
@@ -1,133 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ChatgptAssistant
|
4
|
-
# Helper for messenger
|
5
|
-
module MessengerHelper
|
6
|
-
def chat_success(chat_id)
|
7
|
-
user_message = Message.new(chat_id: chat_id, content: msg.text, role: "user")
|
8
|
-
if user_message&.save
|
9
|
-
text = chatter.chat(msg.text, chat_id, error_messages[:something_went_wrong])
|
10
|
-
mess = parse_message(text, 4096)
|
11
|
-
mess.each { |m| send_message m, msg.chat.id }
|
12
|
-
else
|
13
|
-
send_message error_messages[:something_went_wrong], msg.chat.id
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def respond_with_success(chat)
|
18
|
-
user.update(current_chat_id: chat.id)
|
19
|
-
send_message success_messages[:chat_created]
|
20
|
-
end
|
21
|
-
|
22
|
-
def parse_message(message, max_length)
|
23
|
-
if message.length > max_length
|
24
|
-
array = message.scan(/.{1,#{max_length}}/) if max_length.positive?
|
25
|
-
array = ["Something went wrong! Try again later"] if max_length <= 0
|
26
|
-
else
|
27
|
-
array = [message]
|
28
|
-
end
|
29
|
-
array
|
30
|
-
end
|
31
|
-
|
32
|
-
def send_message(text, chat_id = nil)
|
33
|
-
@send_message = bot.respond_to?(:api) ? telegram_send_message(text, chat_id) : discord_send_message(text)
|
34
|
-
end
|
35
|
-
|
36
|
-
def telegram_send_message(text, chat_id)
|
37
|
-
messages = parse_message(text, 4096)
|
38
|
-
messages.each { |m| bot.api.send_message(chat_id: chat_id, text: m) }
|
39
|
-
end
|
40
|
-
|
41
|
-
def discord_send_message(text)
|
42
|
-
messages = parse_message(text, 2000)
|
43
|
-
messages.each { |m| evnt.respond m }
|
44
|
-
end
|
45
|
-
|
46
|
-
def discord_help_message
|
47
|
-
help_messages.join("\n").gsub(" /", discord_prefix)
|
48
|
-
.gsub("register/", "#{discord_prefix}register ")
|
49
|
-
.gsub("login/", "#{discord_prefix}login ")
|
50
|
-
.gsub("new_chat/", "#{discord_prefix}new_chat/")
|
51
|
-
.gsub("sl_chat/", "#{discord_prefix}sl_chat/")
|
52
|
-
end
|
53
|
-
|
54
|
-
def user_logged_message
|
55
|
-
user.update(telegram_id: msg.chat.id)
|
56
|
-
bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:user_logged_in]) if bot.respond_to?(:api)
|
57
|
-
evnt&.respond success_messages[:user_logged_in] if evnt.present?
|
58
|
-
end
|
59
|
-
|
60
|
-
def invalid_command_error_message
|
61
|
-
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:invalid_command]) if bot.respond_to?(:api)
|
62
|
-
evnt&.respond error_messages[:invalid_command]
|
63
|
-
end
|
64
|
-
|
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.respond_to?(:api)
|
67
|
-
evnt&.respond error_messages[:user_not_found] if evnt.present?
|
68
|
-
end
|
69
|
-
|
70
|
-
def user_created_message
|
71
|
-
bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:user_created]) if bot.respond_to?(:api)
|
72
|
-
evnt&.respond success_messages[:user_created] if evnt.present?
|
73
|
-
end
|
74
|
-
|
75
|
-
def user_creation_error_message
|
76
|
-
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:user_creation]) if bot.respond_to?(:api)
|
77
|
-
evnt&.respond error_messages[:user_creation] if evnt.present?
|
78
|
-
end
|
79
|
-
|
80
|
-
def chat_created_message(chat)
|
81
|
-
user&.update(current_chat_id: chat.id)
|
82
|
-
return telegram_created(chat) if bot.respond_to?(:api)
|
83
|
-
|
84
|
-
discord_created(chat)
|
85
|
-
end
|
86
|
-
|
87
|
-
def telegram_created(chat)
|
88
|
-
bot.api.send_message(chat_id: msg.chat.id, text: "Intructions sended to actor:\n#{chat.prompt}") unless chat.actor.nil?
|
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.respond_to?(:api)
|
91
|
-
end
|
92
|
-
|
93
|
-
def discord_created(chat)
|
94
|
-
evnt.respond "Intructions sended to actor:\n#{chat.prompt}" unless chat.actor.nil?
|
95
|
-
evnt.respond success_messages[:chat_created] if evnt.present?
|
96
|
-
end
|
97
|
-
|
98
|
-
def not_logged_in_message
|
99
|
-
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:user_not_logged_in]) if bot.respond_to?(:api)
|
100
|
-
evnt&.respond(error_messages[:user_not_logged_in])
|
101
|
-
end
|
102
|
-
|
103
|
-
def wrong_password_error_message
|
104
|
-
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:password]) if bot.respond_to?(:api)
|
105
|
-
evnt&.respond(error_messages[:password])
|
106
|
-
end
|
107
|
-
|
108
|
-
def chat_not_found_message
|
109
|
-
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:chat_not_found]) if bot.respond_to?(:api)
|
110
|
-
evnt&.respond(error_messages[:chat_not_found])
|
111
|
-
end
|
112
|
-
|
113
|
-
def no_chat_selected_message
|
114
|
-
bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:no_chat_selected]) if bot.respond_to?(:api)
|
115
|
-
evnt&.respond(error_messages[:no_chat_selected])
|
116
|
-
end
|
117
|
-
|
118
|
-
def no_messages_founded_message
|
119
|
-
bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:no_messages_founded]) if bot.respond_to?(:api)
|
120
|
-
evnt&.respond(error_messages[:no_messages_founded])
|
121
|
-
end
|
122
|
-
|
123
|
-
def chat_creation_failed_message
|
124
|
-
bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:chat_creation_failed]) if bot.respond_to?(:api)
|
125
|
-
evnt&.respond(error_messages[:chat_creation_failed])
|
126
|
-
end
|
127
|
-
|
128
|
-
def user_logged_in_message
|
129
|
-
bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:user_logged_in]) if bot.respond_to?(:api)
|
130
|
-
evnt&.respond(success_messages[:user_logged_in])
|
131
|
-
end
|
132
|
-
end
|
133
|
-
end
|
@@ -1,73 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ChatgptAssistant
|
4
|
-
# Helper for telegram
|
5
|
-
module TelegramHelper
|
6
|
-
def user
|
7
|
-
@user = find_user(telegram_id: msg.chat.id)
|
8
|
-
end
|
9
|
-
|
10
|
-
def bot
|
11
|
-
@bot ||= Telegram::Bot::Client.new(telegram_token)
|
12
|
-
end
|
13
|
-
|
14
|
-
def telegram_chat_event
|
15
|
-
user ? chat_if_exists : not_logged_in_message
|
16
|
-
end
|
17
|
-
|
18
|
-
def telegram_user_create(visitor_id, email, password)
|
19
|
-
visitor = Visitor.find_by(id: visitor_id)
|
20
|
-
return false unless visitor
|
21
|
-
|
22
|
-
visitor.tel_user.update(telegram_id: nil) if visitor.tel_user.present?
|
23
|
-
user = User.new(telegram_id: visitor.telegram_id, email: email, password: password)
|
24
|
-
user.save ? user.email : user.errors.full_messages
|
25
|
-
end
|
26
|
-
|
27
|
-
def telegram_send_start_message
|
28
|
-
send_message common_messages[:start], msg.chat.id
|
29
|
-
help_message = help_messages.join("\n").to_s
|
30
|
-
send_message help_message, msg.chat.id
|
31
|
-
send_message common_messages[:start_helper], msg.chat.id
|
32
|
-
send_message common_messages[:start_sec_helper], msg.chat.id
|
33
|
-
end
|
34
|
-
|
35
|
-
def telegram_create_chat
|
36
|
-
text = msg.text
|
37
|
-
title = text.split("/").last
|
38
|
-
mode = nil
|
39
|
-
if title.include?(":")
|
40
|
-
mode = title.split(":").last
|
41
|
-
title = title.split(":").first
|
42
|
-
end
|
43
|
-
actor_modes = AwesomeChatgptActors::CastControl.actors
|
44
|
-
return send_message "invalid mode", msg.chat.id unless (mode.to_i >= 1 && mode.to_i <= actor_modes.size + 1) || mode.nil?
|
45
|
-
return send_message "invalid chat title", msg.chat.id if title.nil? || title.empty?
|
46
|
-
return send_message "You already have a chat with this title", msg.chat.id if user.chat_by_title(title)
|
47
|
-
|
48
|
-
actor_name = actor_modes[mode.to_i - 1] if mode
|
49
|
-
actor = AwesomeChatgptActors::Actor.new(prompt_type: actor_name) if actor_name
|
50
|
-
chat = Chat.new(user_id: user.id, status: 0, title: title, actor: actor_name, prompt: actor.prompt) if actor
|
51
|
-
chat = Chat.new(user_id: user.id, status: 0, title: title) unless actor
|
52
|
-
return send_message "Something went wrong", msg.chat.id unless chat
|
53
|
-
|
54
|
-
chat.save ? chat_created_message(chat) : chat_creation_failed_message
|
55
|
-
end
|
56
|
-
|
57
|
-
def telegram_text_or_audio?
|
58
|
-
msg.respond_to?(:text) || msg.respond_to?(:audio) || msg.respond_to?(:voice)
|
59
|
-
end
|
60
|
-
|
61
|
-
def telegram_message_has_text?
|
62
|
-
msg.text.present?
|
63
|
-
end
|
64
|
-
|
65
|
-
def telegram_message_has_audio?
|
66
|
-
msg.audio.present? || msg.voice.present?
|
67
|
-
end
|
68
|
-
|
69
|
-
def telegram_actions?
|
70
|
-
msg.text.include?("new_chat/") || msg.text.include?("sl_chat/") || msg.text.include?("login/") || msg.text.include?("register/")
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ChatgptAssistant
|
4
|
-
# Telegram Voice Helper
|
5
|
-
module TelegramVoiceHelper
|
6
|
-
def telegram_audio_info
|
7
|
-
bot.api.get_file(file_id: telegram_audio.file_id)
|
8
|
-
end
|
9
|
-
|
10
|
-
def telegram_audio_url
|
11
|
-
"https://api.telegram.org/file/bot#{telegram_token}/#{telegram_audio_info["result"]["file_path"]}"
|
12
|
-
end
|
13
|
-
|
14
|
-
def telegram_audio
|
15
|
-
msg.audio || msg.voice
|
16
|
-
end
|
17
|
-
|
18
|
-
def telegram_process_ai_voice(user_audio)
|
19
|
-
ai_text = chatter.chat(user_audio["text"], user.current_chat_id, error_messages[:something_went_wrong])
|
20
|
-
ai_voice = synthesis.synthesize_text(ai_text)
|
21
|
-
{
|
22
|
-
voice: ai_voice,
|
23
|
-
text: ai_text
|
24
|
-
}
|
25
|
-
end
|
26
|
-
|
27
|
-
def telegram_send_voice_message(voice: nil, text: nil)
|
28
|
-
messages = parse_message text, 4096
|
29
|
-
bot.api.send_voice(chat_id: msg.chat.id, voice: Faraday::UploadIO.new(voice, "audio/mp3"))
|
30
|
-
messages.each { |message| send_message message, msg.chat.id }
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ChatgptAssistant
|
4
|
-
# This module is responsible for the validation of the bot actions
|
5
|
-
module ValidationHelper
|
6
|
-
def valid_for_list_action?
|
7
|
-
not_logged_in_message if user.nil?
|
8
|
-
chat_not_found_message if user.chats.count.zero?
|
9
|
-
!user.nil? && user.chats.count.positive?
|
10
|
-
end
|
11
|
-
|
12
|
-
def chat_if_exists
|
13
|
-
chat = Chat.find_by(user_id: user.id, id: user.current_chat_id)
|
14
|
-
chat ? chat_success(chat.id) : no_chat_selected_message
|
15
|
-
end
|
16
|
-
|
17
|
-
def visitor_user?
|
18
|
-
visitor&.tel_user.nil? && visitor&.dis_user.nil?
|
19
|
-
end
|
20
|
-
|
21
|
-
def discord_voice_bot_disconnected?
|
22
|
-
user && evnt.user.voice_channel && !evnt.voice && !chat.nil?
|
23
|
-
end
|
24
|
-
|
25
|
-
def discord_voice_bot_connected?
|
26
|
-
user && evnt.user.voice_channel && evnt.voice && !chat.nil?
|
27
|
-
end
|
28
|
-
|
29
|
-
def discord_next_action?
|
30
|
-
return true if evnt.channel.type != 1 && evnt.channel.name != "ai-spaces"
|
31
|
-
|
32
|
-
%w[login register start help new_chat sl_chat ask list hist connect disconnect speak].each do |action|
|
33
|
-
return true if evnt.message.content.include?("#{discord_prefix}#{action}")
|
34
|
-
end
|
35
|
-
false
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ChatgptAssistant
|
4
|
-
# Helper for visit
|
5
|
-
module VisitHelper
|
6
|
-
def telegram_visited?(chat_id)
|
7
|
-
return unless msg
|
8
|
-
|
9
|
-
visitor = Visitor.find_by(telegram_id: chat_id, name: msg.from.first_name)
|
10
|
-
if visitor.nil?
|
11
|
-
Visitor.create(telegram_id: chat_id, name: msg.from.first_name)
|
12
|
-
else
|
13
|
-
visitor
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def discord_visited?(user_id)
|
18
|
-
return unless evnt
|
19
|
-
|
20
|
-
visitor = Visitor.find_by(discord_id: user_id, name: evnt.user.name)
|
21
|
-
if visitor.nil?
|
22
|
-
Visitor.create(discord_id: user_id, name: evnt.user.name)
|
23
|
-
else
|
24
|
-
visitor
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,34 +0,0 @@
|
|
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
|
@@ -1,36 +0,0 @@
|
|
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
|
data/lib/bots/telegram_bot.rb
DELETED
@@ -1,180 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative "helpers/telegram_helper"
|
4
|
-
require_relative "helpers/telegram_voice_helper"
|
5
|
-
|
6
|
-
module ChatgptAssistant
|
7
|
-
# This class is responsible for the telegram bot features
|
8
|
-
class TelegramBot < ApplicationBot
|
9
|
-
def start
|
10
|
-
bot.listen do |message|
|
11
|
-
@msg = message
|
12
|
-
@visitor = telegram_visited?(@msg.chat.id)
|
13
|
-
next unless telegram_text_or_audio?
|
14
|
-
|
15
|
-
text_events if telegram_message_has_text?
|
16
|
-
audio_event if telegram_message_has_audio?
|
17
|
-
end
|
18
|
-
rescue StandardError => e
|
19
|
-
Error.create(message: e.message, backtrace: e.backtrace)
|
20
|
-
retry
|
21
|
-
end
|
22
|
-
|
23
|
-
private
|
24
|
-
|
25
|
-
include TelegramHelper
|
26
|
-
include TelegramVoiceHelper
|
27
|
-
|
28
|
-
attr_accessor :msg, :visitor, :chat, :chat_id
|
29
|
-
|
30
|
-
def text_events
|
31
|
-
case msg.text
|
32
|
-
when "/start"
|
33
|
-
start_event
|
34
|
-
when "/help"
|
35
|
-
help_event
|
36
|
-
when "/hist"
|
37
|
-
hist_event
|
38
|
-
when "/list"
|
39
|
-
list_event
|
40
|
-
when "/stop"
|
41
|
-
stop_event
|
42
|
-
when nil
|
43
|
-
raise NilError
|
44
|
-
else
|
45
|
-
action_events
|
46
|
-
end
|
47
|
-
rescue NilError => e
|
48
|
-
send_message e.message, msg.chat.id
|
49
|
-
end
|
50
|
-
|
51
|
-
def start_event
|
52
|
-
telegram_send_start_message
|
53
|
-
end
|
54
|
-
|
55
|
-
def help_event
|
56
|
-
help_messages.each { |m| send_message m, msg.chat.id }
|
57
|
-
end
|
58
|
-
|
59
|
-
def hist_event
|
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
|
-
|
64
|
-
user.chat_history.each do |m|
|
65
|
-
send_message m, msg.chat.id
|
66
|
-
end
|
67
|
-
rescue NoChatSelectedError, UserNotLoggedInError, NoMessagesFoundedError => e
|
68
|
-
send_message e.message, msg.chat.id
|
69
|
-
end
|
70
|
-
|
71
|
-
def list_event
|
72
|
-
raise UserNotLoggedInError if user.nil?
|
73
|
-
raise NoChatsFoundedError if user.chats.count.zero?
|
74
|
-
|
75
|
-
send_message common_messages[:chat_list], msg.chat.id
|
76
|
-
chats_str = ""
|
77
|
-
user.chats.each_with_index { |c, i| chats_str += "Chat #{i + 1} - #{c.title}\n" }
|
78
|
-
send_message chats_str, msg.chat.id
|
79
|
-
rescue NoChatsFoundedError, UserNotLoggedInError => e
|
80
|
-
send_message e.message, msg.chat.id
|
81
|
-
end
|
82
|
-
|
83
|
-
def action_events
|
84
|
-
return auth_events if auth_event?
|
85
|
-
return new_chat_event if msg.text.include?("new_chat/")
|
86
|
-
return select_chat_event if msg.text.include?("sl_chat/")
|
87
|
-
return telegram_chat_event unless telegram_actions?
|
88
|
-
|
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/")
|
102
|
-
end
|
103
|
-
|
104
|
-
def login_event
|
105
|
-
raise UserLoggedInError if user
|
106
|
-
|
107
|
-
user_info = msg.text.split("/").last
|
108
|
-
email, password = user_info.split(":")
|
109
|
-
case telegram_user_auth(email, password, msg.chat.id)
|
110
|
-
when "user not found"
|
111
|
-
raise UserNotFoundError
|
112
|
-
when "wrong password"
|
113
|
-
raise WrongPasswordError
|
114
|
-
when email
|
115
|
-
user_logged_in_message
|
116
|
-
end
|
117
|
-
rescue UserNotFoundError, WrongPasswordError, UserLoggedInError => e
|
118
|
-
send_message e.message, msg.chat.id
|
119
|
-
end
|
120
|
-
|
121
|
-
def register_event
|
122
|
-
user_info = msg.text.split("/").last
|
123
|
-
raise NoRegisterInfoError if user_info.nil?
|
124
|
-
raise UserLoggedInError if user
|
125
|
-
|
126
|
-
email, password = user_info.split(":")
|
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
|
141
|
-
end
|
142
|
-
|
143
|
-
def new_chat_event
|
144
|
-
raise UserNotLoggedInError if user.nil?
|
145
|
-
|
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
|
149
|
-
end
|
150
|
-
|
151
|
-
def select_chat_event
|
152
|
-
raise UserNotLoggedInError if user.nil?
|
153
|
-
|
154
|
-
title = msg.text.split("/").last
|
155
|
-
chat = user.chat_by_title(title)
|
156
|
-
raise ChatNotFoundError if chat.nil?
|
157
|
-
|
158
|
-
raise ChatNotFoundError unless user.update(current_chat_id: chat.id)
|
159
|
-
|
160
|
-
send_message success_messages[:chat_selected], msg.chat.id
|
161
|
-
rescue UserNotLoggedInError, ChatNotFoundError => e
|
162
|
-
send_message e.message, msg.chat.id
|
163
|
-
end
|
164
|
-
|
165
|
-
def audio_event
|
166
|
-
raise UserNotLoggedInError if user.nil?
|
167
|
-
raise NoChatSelectedError if user.current_chat.nil?
|
168
|
-
|
169
|
-
user_audio = transcribe_file(telegram_audio_url)
|
170
|
-
message = Message.new(content: user_audio[:text], chat_id: user.current_chat_id, role: "user")
|
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
|
178
|
-
end
|
179
|
-
end
|
180
|
-
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|