chatgpt_assistant 0.1.2 → 0.1.4
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_prod_sample +1 -1
- data/.env_sample +1 -1
- data/.rubocop.yml +17 -5
- data/Dockerfile +2 -5
- data/Gemfile +1 -1
- data/Gemfile.lock +6 -6
- data/SECURITY.md +15 -0
- data/exe/chatgpt_assistant +2 -2
- data/exe/chatgpt_bot +1 -1
- data/lib/bots/application_bot.rb +22 -91
- data/lib/bots/discord_bot.rb +102 -241
- data/lib/bots/helpers/audio_helper.rb +18 -0
- data/lib/bots/helpers/authentication_helper.rb +47 -0
- data/lib/bots/helpers/discord_helper.rb +102 -0
- data/lib/bots/helpers/discord_voice_helper.rb +50 -0
- data/lib/bots/helpers/file_helper.rb +10 -0
- data/lib/bots/helpers/logger_action_helper.rb +14 -0
- data/lib/bots/helpers/messenger_helper.rb +134 -0
- data/lib/bots/helpers/search_helper.rb +34 -0
- data/lib/bots/helpers/telegram_helper.rb +89 -0
- data/lib/bots/helpers/validation_helper.rb +29 -0
- data/lib/bots/helpers/visit_helper.rb +24 -0
- data/lib/bots/telegram_bot.rb +120 -235
- data/lib/chatgpt_assistant/audio_recognition.rb +31 -32
- data/lib/chatgpt_assistant/audio_synthesis.rb +72 -76
- data/lib/chatgpt_assistant/chatter.rb +34 -27
- data/lib/chatgpt_assistant/config.rb +22 -18
- data/lib/chatgpt_assistant/default_messages.rb +108 -108
- data/lib/chatgpt_assistant/migrations.rb +62 -4
- data/lib/chatgpt_assistant/models.rb +53 -7
- data/lib/chatgpt_assistant/version.rb +1 -1
- data/lib/chatgpt_assistant.rb +25 -13
- data/workflows/deploy.yml +19 -0
- data/workflows/deploy_and_build.yml +19 -0
- metadata +22 -6
@@ -0,0 +1,134 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ChatgptAssistant
|
4
|
+
# Helper for messenger
|
5
|
+
module MessengerHelper
|
6
|
+
def chat_success(chat_id)
|
7
|
+
Message.create(chat_id: chat_id, content: msg.text, role: "user")
|
8
|
+
text = chatter.chat(msg.text, chat_id)
|
9
|
+
mess = parse_message(text, 4096)
|
10
|
+
mess.each { |m| send_message m, msg.chat.id }
|
11
|
+
end
|
12
|
+
|
13
|
+
def respond_with_success
|
14
|
+
user.update(current_chat_id: chat.id)
|
15
|
+
send_message success_messages[:chat_created]
|
16
|
+
end
|
17
|
+
|
18
|
+
def parse_message(message, max_length)
|
19
|
+
if message.length > max_length
|
20
|
+
array = message.scan(/.{1,#{max_length}}/) if max_length.positive?
|
21
|
+
array = ["Something went wrong! Try again later"] if max_length <= 0
|
22
|
+
else
|
23
|
+
array = [message]
|
24
|
+
end
|
25
|
+
array
|
26
|
+
end
|
27
|
+
|
28
|
+
def send_message(text, chat_id = nil)
|
29
|
+
@send_message = bot.respond_to?(:api) ? telegram_send_message(text, chat_id) : discord_send_message(text)
|
30
|
+
end
|
31
|
+
|
32
|
+
def telegram_send_message(text, chat_id)
|
33
|
+
messages = parse_message(text, 4096)
|
34
|
+
messages.each { |m| bot.api.send_message(chat_id: chat_id, text: m) }
|
35
|
+
end
|
36
|
+
|
37
|
+
def discord_send_message(text)
|
38
|
+
messages = parse_message(text, 2000)
|
39
|
+
messages.each { |m| evnt.respond m }
|
40
|
+
end
|
41
|
+
|
42
|
+
def discord_help_message
|
43
|
+
help_messages.join("\n").gsub(" /", discord_prefix)
|
44
|
+
.gsub("register/", "#{discord_prefix}register ")
|
45
|
+
.gsub("login/", "#{discord_prefix}login ")
|
46
|
+
.gsub("new_chat/", "#{discord_prefix}new_chat/")
|
47
|
+
.gsub("sl_chat/", "#{discord_prefix}sl_chat/")
|
48
|
+
end
|
49
|
+
|
50
|
+
def user_logged_message
|
51
|
+
register_user_action("login", user.id)
|
52
|
+
user.update(telegram_id: msg.chat.id)
|
53
|
+
bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:user_logged_in])
|
54
|
+
evnt&.respond success_messages[:user_logged_in] if evnt.present?
|
55
|
+
end
|
56
|
+
|
57
|
+
def invalid_command_error_message
|
58
|
+
register_user_action("invalid_command", user.id) if user
|
59
|
+
register_visitor_action("invalid_command", visitor.id) if visitor&.tel_user.nil?
|
60
|
+
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:invalid_command])
|
61
|
+
evnt&.respond error_messages[:invalid_command]
|
62
|
+
end
|
63
|
+
|
64
|
+
def user_not_found_error_message
|
65
|
+
register_visitor_action("error: user not found", visitor.id)
|
66
|
+
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:user_not_found]) if bot.api.present?
|
67
|
+
evnt&.respond error_messages[:user_not_found] if evnt.present?
|
68
|
+
end
|
69
|
+
|
70
|
+
def user_created_message
|
71
|
+
register_visitor_action("user_created", visitor.id)
|
72
|
+
bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:user_created]) if bot.api.present?
|
73
|
+
evnt&.respond success_messages[:user_created] if evnt.present?
|
74
|
+
end
|
75
|
+
|
76
|
+
def user_creation_error_message
|
77
|
+
register_visitor_action("error: user creation", visitor.id)
|
78
|
+
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:user_creation]) if bot.api.present?
|
79
|
+
evnt&.respond error_messages[:user_creation] if evnt.present?
|
80
|
+
end
|
81
|
+
|
82
|
+
def chat_created_message(chat)
|
83
|
+
register_user_action("chat_created", user.id) if user
|
84
|
+
user&.update(current_chat_id: chat.id)
|
85
|
+
bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:chat_created]) if bot.api.present?
|
86
|
+
end
|
87
|
+
|
88
|
+
def not_logged_in_message
|
89
|
+
register_visitor_action("error: not logged in", visitor.id) if visitor_user?
|
90
|
+
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:user_not_logged_in])
|
91
|
+
evnt&.respond(error_messages[:user_not_logged_in])
|
92
|
+
end
|
93
|
+
|
94
|
+
def wrong_password_error_message
|
95
|
+
register_visitor_action("error: wrong password", visitor.id) if visitor_user?
|
96
|
+
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:password])
|
97
|
+
evnt&.respond(error_messages[:password])
|
98
|
+
end
|
99
|
+
|
100
|
+
def chat_not_found_message
|
101
|
+
register_visitor_action("error: visitors cant select chat", visitor.id) if visitor_user?
|
102
|
+
register_user_action("error: chat not found", user.id) if user
|
103
|
+
bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:chat_not_found])
|
104
|
+
evnt&.respond(error_messages[:chat_not_found])
|
105
|
+
end
|
106
|
+
|
107
|
+
def no_chat_selected_message
|
108
|
+
register_visitor_action("error: visitors cant select chat", visitor.id) if visitor_user?
|
109
|
+
register_user_action("error: no chat selected", user.id) if user
|
110
|
+
bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:no_chat_selected])
|
111
|
+
evnt&.respond(error_messages[:no_chat_selected])
|
112
|
+
end
|
113
|
+
|
114
|
+
def no_messages_founded_message
|
115
|
+
register_visitor_action("error: visitors cant view the chat history", visitor.id) if visitor_user?
|
116
|
+
register_user_action("error: no messages founded", user.id) if user
|
117
|
+
bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:no_messages_founded])
|
118
|
+
evnt&.respond(error_messages[:no_messages_founded])
|
119
|
+
end
|
120
|
+
|
121
|
+
def chat_creation_failed_message
|
122
|
+
register_visitor_action("error: chat creation failed", visitor.id) if visitor_user?
|
123
|
+
register_user_action("error: chat creation failed", user.id) if user
|
124
|
+
bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:chat_creation_failed])
|
125
|
+
evnt&.respond(error_messages[:chat_creation_failed])
|
126
|
+
end
|
127
|
+
|
128
|
+
def user_logged_in_message
|
129
|
+
register_visitor_action("user_logged_in", visitor.id) if visitor
|
130
|
+
bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:user_logged_in])
|
131
|
+
evnt&.respond(success_messages[:user_logged_in])
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ChatgptAssistant
|
4
|
+
# Helper for search
|
5
|
+
module SearchHelper
|
6
|
+
def find_visitor(telegram_id: nil, discord_id: nil)
|
7
|
+
if telegram_id
|
8
|
+
Visitor.find_by(telegram_id: telegram_id)
|
9
|
+
elsif discord_id
|
10
|
+
Visitor.find_by(discord_id: discord_id)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def find_user(telegram_id: nil, discord_id: nil, email: nil)
|
15
|
+
if telegram_id
|
16
|
+
User.find_by(telegram_id: telegram_id)
|
17
|
+
elsif discord_id
|
18
|
+
User.find_by(discord_id: discord_id)
|
19
|
+
elsif email
|
20
|
+
User.find_by(email: email)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def where_user(telegram_id: nil, discord_id: nil, email: nil)
|
25
|
+
if telegram_id
|
26
|
+
User.where(telegram_id: telegram_id)
|
27
|
+
elsif discord_id
|
28
|
+
User.where(discord_id: discord_id)
|
29
|
+
elsif email
|
30
|
+
User.where(email: email)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,89 @@
|
|
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_audio_info
|
19
|
+
bot.api.get_file(file_id: telegram_audio.file_id)
|
20
|
+
end
|
21
|
+
|
22
|
+
def telegram_audio_url
|
23
|
+
"https://api.telegram.org/file/bot#{telegram_token}/#{telegram_audio_info["result"]["file_path"]}"
|
24
|
+
end
|
25
|
+
|
26
|
+
def telegram_audio
|
27
|
+
msg.audio || msg.voice
|
28
|
+
end
|
29
|
+
|
30
|
+
def telegram_process_ai_voice(user_audio)
|
31
|
+
ai_text = chatter.chat(user_audio["text"], user.current_chat_id)
|
32
|
+
ai_voice = synthesis.synthesize_text(ai_text)
|
33
|
+
{
|
34
|
+
voice: ai_voice,
|
35
|
+
text: ai_text
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
39
|
+
def telegram_send_voice_message(voice: nil, text: nil)
|
40
|
+
messages = parse_message text, 4096
|
41
|
+
bot.api.send_voice(chat_id: msg.chat.id, voice: Faraday::UploadIO.new(voice, "audio/mp3"))
|
42
|
+
messages.each { |message| send_message message, msg.chat.id }
|
43
|
+
end
|
44
|
+
|
45
|
+
def telegram_user_create(visitor_id, email, password)
|
46
|
+
visitor = Visitor.find_by(id: visitor_id)
|
47
|
+
return false unless visitor
|
48
|
+
|
49
|
+
visitor.tel_user.update(telegram_id: nil) if visitor.tel_user.present?
|
50
|
+
user = User.new(telegram_id: visitor.telegram_id, email: email, password: password)
|
51
|
+
user.save ? user.email : user.errors.full_messages
|
52
|
+
end
|
53
|
+
|
54
|
+
def telegram_send_start_message
|
55
|
+
send_message commom_messages[:start], msg.chat.id
|
56
|
+
help_message = help_messages.join("\n").to_s
|
57
|
+
send_message help_message, msg.chat.id
|
58
|
+
send_message commom_messages[:start_helper], msg.chat.id
|
59
|
+
send_message commom_messages[:start_sec_helper], msg.chat.id
|
60
|
+
end
|
61
|
+
|
62
|
+
def telegram_create_chat
|
63
|
+
text = msg.text
|
64
|
+
title = text.split("/").last
|
65
|
+
chat = Chat.new(user_id: user.id, status: 0, title: title)
|
66
|
+
chat.save ? chat_created_message(chat) : chat_creation_failed_message
|
67
|
+
end
|
68
|
+
|
69
|
+
def telegram_user_history
|
70
|
+
user.current_chat.messages.last(10).map { |m| "#{m.role}: #{m.content}\nat: #{m.created_at}" }
|
71
|
+
end
|
72
|
+
|
73
|
+
def telegram_text_or_audio?
|
74
|
+
msg.respond_to?(:text) || msg.respond_to?(:audio) || msg.respond_to?(:voice)
|
75
|
+
end
|
76
|
+
|
77
|
+
def telegram_message_has_text?
|
78
|
+
msg.text.present?
|
79
|
+
end
|
80
|
+
|
81
|
+
def telegram_message_has_audio?
|
82
|
+
msg.audio.present? || msg.voice.present?
|
83
|
+
end
|
84
|
+
|
85
|
+
def telegram_actions?
|
86
|
+
msg.text.include?("new_chat/") || msg.text.include?("sl_chat/") || msg.text.include?("login/") || msg.text.include?("register/")
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,29 @@
|
|
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
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ChatgptAssistant
|
4
|
+
# Helper for visit
|
5
|
+
module VisitHelper
|
6
|
+
def telegram_visited?(chat_id)
|
7
|
+
visitor = Visitor.find_by(telegram_id: chat_id, name: msg.from.first_name)
|
8
|
+
if visitor.nil?
|
9
|
+
Visitor.create(telegram_id: chat_id, name: msg.from.first_name)
|
10
|
+
else
|
11
|
+
visitor
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def discord_visited?(user_id)
|
16
|
+
visitor = Visitor.find_by(discord_id: user_id, name: evnt.user.name)
|
17
|
+
if visitor.nil?
|
18
|
+
Visitor.create(discord_id: user_id, name: evnt.user.name)
|
19
|
+
else
|
20
|
+
visitor
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|