chatgpt_assistant 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChatgptAssistant
4
+ # Helper for discord
5
+ module DiscordHelper
6
+ def bot
7
+ @bot ||= Discordrb::Commands::CommandBot.new(
8
+ token: discord_token,
9
+ client_id: discord_client_id,
10
+ prefix: discord_prefix
11
+ )
12
+ end
13
+
14
+ def start_action
15
+ send_message commom_messages[:start_helper].gsub("register/", "gpt!register ")
16
+ send_message commom_messages[:start_sec_helper].gsub("login/", "gpt!login ")
17
+ end
18
+
19
+ def login_action
20
+ user_email, user_password = message.split(":")
21
+ case discord_user_auth(user_email, user_password, evnt.user.id)
22
+ when "user not found"
23
+ send_message error_messages[:user_not_found]
24
+ when "wrong password"
25
+ puts "wrong password"
26
+ send_message error_messages[:wrong_password]
27
+ when find_user(email: user_email).email
28
+ send_message success_messages[:user_logged_in]
29
+ end
30
+ end
31
+
32
+ def create_user_action(mail, pass)
33
+ id = evnt.user.id
34
+ return send_message(success_messages[:user_created]) if discord_user_create(id, mail, pass)
35
+
36
+ send_message(error_messages[:user_not_created])
37
+ end
38
+
39
+ def register_action
40
+ user_email = message.split(":")[0]
41
+ user_password = message.split(":")[1]
42
+ return create_user_action(user_email, user_password) if find_user(email: user_email).nil?
43
+
44
+ send_message(error_messages[:user_already_exists])
45
+ end
46
+
47
+ def list_action
48
+ chats_title = user.chats.map(&:title)
49
+ send_message commom_messages[:chat_list]
50
+ send_message chats_title.join("\n")
51
+ end
52
+
53
+ def hist_action
54
+ messages = Message.where(chat_id: chat.id).order(:created_at)
55
+ messages.each { |m| send_message "#{m.role} - #{m.content}\n#{m.created_at.strftime("%d/%m/%Y %H:%M")}" }
56
+ "This is the end of the chat history"
57
+ end
58
+
59
+ def help_action
60
+ message = discord_help_message
61
+ send_message message
62
+ end
63
+
64
+ def ask_action
65
+ @chat = Chat.where(id: user.current_chat_id).last
66
+ send_message error_messages[:chat_not_found] if chat.nil?
67
+ @message = Message.new(chat_id: chat.id, content: message, role: "user") if chat
68
+ (message.save ? answer_action : send_message(error_messages[:message_not_saved])) if chat
69
+ end
70
+
71
+ def sl_chat_action(chat_to_select)
72
+ @chat = user.chat_by_title(chat_to_select)
73
+ send_message error_messages[:chat_not_found] if chat.nil?
74
+ user.update(current_chat_id: chat.id) if chat
75
+ send_message success_messages[:chat_selected] if chat
76
+ end
77
+
78
+ def create_chat_action
79
+ chat_title = evnt.message.content.split(" ")[1..].join(" ")
80
+ @chat = Chat.new(user_id: user.id, title: chat_title, status: 0)
81
+ chat.save ? respond_with_success : send_message(error_messages[:chat_creation])
82
+ end
83
+
84
+ def answer_action
85
+ response = chatter.chat(message.content, chat.id)
86
+ send_message response
87
+ end
88
+
89
+ def disconnect_checker_action
90
+ send_message error_messages[:user_not_logged_in] if user.nil?
91
+ send_message error_messages[:user_not_in_voice_channel] if evnt.user.voice_channel.nil? && user
92
+ send_message error_messages[:user_not_connected] if !evnt.voice && user
93
+ end
94
+
95
+ def discord_user_create(discord_id, email, password)
96
+ user = User.new(discord_id: discord_id, email: email, password: password)
97
+ last_access = find_user(discord_id: discord_id)
98
+ last_access&.update(discord_id: nil)
99
+ user.save
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChatgptAssistant
4
+ # Helper for voice actions
5
+ module DiscordVoiceHelper
6
+ def voice_connect_checker_action
7
+ send_message error_messages[:user_not_logged_in] if user.nil?
8
+ send_message error_messages[:chat_not_found] if user && chat.nil?
9
+ end
10
+
11
+ def voice_connection_checker_action
12
+ send_message error_messages[:user_not_in_voice_channel] if evnt.user.voice_channel.nil? && user
13
+ send_message error_messages[:bot_already_connected] if evnt.voice && user
14
+ end
15
+
16
+ def speak_connect_checker_action
17
+ send_message error_messages[:user_not_logged_in] if user.nil?
18
+ send_message error_messages[:chat_not_found] if user && evnt.user.voice_channel && evnt.voice && chat.nil?
19
+ end
20
+
21
+ def speak_connection_checker_action
22
+ send_message error_messages[:user_not_in_voice_channel] if evnt.user.voice_channel.nil? && user
23
+ send_message error_messages[:bot_not_in_voice_channel] if !evnt.voice && user
24
+ end
25
+
26
+ def ask_to_speak_action
27
+ Message.create(chat_id: chat.id, content: message, role: "user")
28
+ response = chatter.chat(message, chat.id)
29
+ audio_path = synthesis.synthesize_text(response)
30
+ speak_answer_action(audio_path, response)
31
+ end
32
+
33
+ def speak_answer_action(audio_path, response)
34
+ send_message response
35
+ evnt.voice.play_file(audio_path)
36
+ delete_file audio_path
37
+ "OK"
38
+ end
39
+
40
+ def discord_voice_bot_connect
41
+ bot.voice_connect(evnt.user.voice_channel) if discord_voice_bot_disconnected?
42
+ discord_voice_bot_connected? ? "Connected to voice channel" : "Error connecting to voice channel"
43
+ end
44
+
45
+ def disconnect_action
46
+ bot.voice_destroy(evnt.user.voice_channel)
47
+ "Disconnected from voice channel"
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChatgptAssistant
4
+ # Helper for files
5
+ module FileHelper
6
+ def delete_file(file)
7
+ File.delete file
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChatgptAssistant
4
+ # Helper for logger action
5
+ module LoggerActionHelper
6
+ def register_visitor_action(action, visitor_id)
7
+ VisitorAction.create(visitor_id: visitor_id, action: action, description: msg.text)
8
+ end
9
+
10
+ def register_user_action(action, user_id)
11
+ UserAction.create(user_id: user_id, action: action, description: msg.text)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,132 @@
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
+ bot.api.send_message(chat_id: chat_id, text: text)
34
+ end
35
+
36
+ def discord_send_message(text)
37
+ evnt.respond text
38
+ end
39
+
40
+ def discord_help_message
41
+ help_messages.join("\n").gsub(" /", discord_prefix)
42
+ .gsub("register/", "#{discord_prefix}register ")
43
+ .gsub("login/", "#{discord_prefix}login ")
44
+ .gsub("new_chat/", "#{discord_prefix}new_chat/")
45
+ .gsub("sl_chat/", "#{discord_prefix}sl_chat/")
46
+ end
47
+
48
+ def user_logged_message
49
+ register_user_action("login", user.id)
50
+ user.update(telegram_id: msg.chat.id)
51
+ bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:user_logged_in])
52
+ evnt&.respond success_messages[:user_logged_in] if evnt.present?
53
+ end
54
+
55
+ def invalid_command_error_message
56
+ register_user_action("invalid_command", user.id) if user
57
+ register_visitor_action("invalid_command", visitor.id) if visitor&.tel_user.nil?
58
+ bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:invalid_command])
59
+ evnt&.respond error_messages[:invalid_command]
60
+ end
61
+
62
+ def user_not_found_error_message
63
+ register_visitor_action("error: user not found", visitor.id)
64
+ bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:user_not_found]) if bot.api.present?
65
+ evnt&.respond error_messages[:user_not_found] if evnt.present?
66
+ end
67
+
68
+ def user_created_message
69
+ register_visitor_action("user_created", visitor.id)
70
+ bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:user_created]) if bot.api.present?
71
+ evnt&.respond success_messages[:user_created] if evnt.present?
72
+ end
73
+
74
+ def user_creation_error_message
75
+ register_visitor_action("error: user creation", visitor.id)
76
+ bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:user_creation]) if bot.api.present?
77
+ evnt&.respond error_messages[:user_creation] if evnt.present?
78
+ end
79
+
80
+ def chat_created_message(chat)
81
+ register_user_action("chat_created", user.id) if user
82
+ user&.update(current_chat_id: chat.id)
83
+ bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:chat_created]) if bot.api.present?
84
+ end
85
+
86
+ def not_logged_in_message
87
+ register_visitor_action("error: not logged in", visitor.id) if visitor_user?
88
+ bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:user_not_logged_in])
89
+ evnt&.respond(error_messages[:user_not_logged_in])
90
+ end
91
+
92
+ def wrong_password_error_message
93
+ register_visitor_action("error: wrong password", visitor.id) if visitor_user?
94
+ bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:password])
95
+ evnt&.respond(error_messages[:password])
96
+ end
97
+
98
+ def chat_not_found_message
99
+ register_visitor_action("error: visitors cant select chat", visitor.id) if visitor_user?
100
+ register_user_action("error: chat not found", user.id) if user
101
+ bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:chat_not_found])
102
+ evnt&.respond(error_messages[:chat_not_found])
103
+ end
104
+
105
+ def no_chat_selected_message
106
+ register_visitor_action("error: visitors cant select chat", visitor.id) if visitor_user?
107
+ register_user_action("error: no chat selected", user.id) if user
108
+ bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:no_chat_selected])
109
+ evnt&.respond(error_messages[:no_chat_selected])
110
+ end
111
+
112
+ def no_messages_founded_message
113
+ register_visitor_action("error: visitors cant view the chat history", visitor.id) if visitor_user?
114
+ register_user_action("error: no messages founded", user.id) if user
115
+ bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:no_messages_founded])
116
+ evnt&.respond(error_messages[:no_messages_founded])
117
+ end
118
+
119
+ def chat_creation_failed_message
120
+ register_visitor_action("error: chat creation failed", visitor.id) if visitor_user?
121
+ register_user_action("error: chat creation failed", user.id) if user
122
+ bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:chat_creation_failed])
123
+ evnt&.respond(error_messages[:chat_creation_failed])
124
+ end
125
+
126
+ def user_logged_in_message
127
+ register_visitor_action("user_logged_in", visitor.id) if visitor
128
+ bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:user_logged_in])
129
+ evnt&.respond(success_messages[:user_logged_in])
130
+ end
131
+ end
132
+ 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