chatgpt_assistant 0.1.4 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.env_sample +1 -1
  3. data/.rubocop.yml +3 -3
  4. data/Gemfile +19 -5
  5. data/Gemfile.lock +64 -50
  6. data/README.md +14 -13
  7. data/Rakefile +2 -0
  8. data/docker-compose.prod.yml +1 -1
  9. data/docker-compose.yml +18 -0
  10. data/exe/chatgpt_bot +3 -0
  11. data/lib/bots/application_bot.rb +15 -5
  12. data/lib/bots/discord_bot.rb +42 -15
  13. data/lib/bots/helpers/authentication_helper.rb +7 -6
  14. data/lib/bots/helpers/discord_helper.rb +29 -7
  15. data/lib/bots/helpers/discord_voice_helper.rb +1 -1
  16. data/lib/bots/helpers/messenger_helper.rb +35 -36
  17. data/lib/bots/helpers/search_helper.rb +3 -3
  18. data/lib/bots/helpers/telegram_helper.rb +19 -35
  19. data/lib/bots/helpers/telegram_voice_helper.rb +33 -0
  20. data/lib/bots/helpers/validation_helper.rb +9 -0
  21. data/lib/bots/helpers/visit_helper.rb +4 -0
  22. data/lib/bots/jobs/new_chat_job.rb +17 -0
  23. data/lib/bots/jobs/register_job.rb +16 -0
  24. data/lib/bots/jobs/voice_connect_job.rb +14 -0
  25. data/lib/bots/services/new_chat_service.rb +34 -0
  26. data/lib/bots/services/register_service.rb +36 -0
  27. data/lib/bots/services/voice_connect_service.rb +29 -0
  28. data/lib/bots/telegram_bot.rb +72 -54
  29. data/lib/chatgpt_assistant/chatter.rb +17 -6
  30. data/lib/chatgpt_assistant/config.rb +23 -3
  31. data/lib/chatgpt_assistant/default_messages.rb +72 -30
  32. data/lib/chatgpt_assistant/error.rb +219 -0
  33. data/lib/chatgpt_assistant/migrations.rb +3 -30
  34. data/lib/chatgpt_assistant/models.rb +24 -29
  35. data/lib/chatgpt_assistant/sidekiq.rb +7 -0
  36. data/lib/chatgpt_assistant/sidekiq.yml +10 -0
  37. data/lib/chatgpt_assistant/version.rb +1 -1
  38. data/lib/chatgpt_assistant.rb +4 -12
  39. data/prompts-data/awesome-en-prompts.csv +164 -0
  40. data/prompts-data/awesome-pt-prompts.csv +164 -0
  41. metadata +15 -4
  42. data/lib/bots/helpers/logger_action_helper.rb +0 -14
@@ -23,7 +23,8 @@ module ChatgptAssistant
23
23
 
24
24
  def telegram_user_access(visitor, new_access)
25
25
  other_access = where_user(telegram_id: visitor.telegram_id)
26
- other_access&.each { |access| access.update(telegram_id: nil) }
26
+ other_access&.each { |access| access.update(telegram_id: nil) } if other_access&.class == Array
27
+ other_access&.update(telegram_id: nil) if other_access&.class == User
27
28
  new_access.update(telegram_id: visitor.telegram_id)
28
29
  new_access.email
29
30
  end
@@ -37,11 +38,11 @@ module ChatgptAssistant
37
38
  end
38
39
 
39
40
  def discord_user_access(discord_id, user_email)
40
- last_access = find_user(discord_id: discord_id)
41
- new_access = find_user(email: user_email)
42
- last_acess.update(discord_id: nil) if last_access&.email != new_access&.email
43
- new_access&.update(discord_id: discord_id)
44
- new_access.email
41
+ other_access = where_user(discord_id: discord_id)
42
+ other_access&.each { |access| access.update(discord_id: nil) }
43
+ user = find_user(email: user_email)
44
+ user.update(discord_id: discord_id)
45
+ user.email
45
46
  end
46
47
  end
47
48
  end
@@ -12,8 +12,8 @@ module ChatgptAssistant
12
12
  end
13
13
 
14
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 ")
15
+ send_message common_messages[:start_helper].gsub("register/", "gpt!register ")
16
+ send_message common_messages[:start_sec_helper].gsub("login/", "gpt!login ")
17
17
  end
18
18
 
19
19
  def login_action
@@ -46,7 +46,7 @@ module ChatgptAssistant
46
46
 
47
47
  def list_action
48
48
  chats_title = user.chats.map(&:title)
49
- send_message commom_messages[:chat_list]
49
+ send_message common_messages[:chat_list]
50
50
  send_message chats_title.join("\n")
51
51
  end
52
52
 
@@ -68,6 +68,13 @@ module ChatgptAssistant
68
68
  (message.save ? answer_action : send_message(error_messages[:message_not_saved])) if chat
69
69
  end
70
70
 
71
+ def private_message_action
72
+ @chat = Chat.where(id: user.current_chat_id).last
73
+ send_message error_messages[:chat_not_found] if chat.nil?
74
+ @message = Message.new(chat_id: chat.id, content: message, role: "user") if chat
75
+ (message.save ? answer_action : send_message(error_messages[:message_not_saved])) if chat
76
+ end
77
+
71
78
  def sl_chat_action(chat_to_select)
72
79
  @chat = user.chat_by_title(chat_to_select)
73
80
  send_message error_messages[:chat_not_found] if chat.nil?
@@ -76,13 +83,28 @@ module ChatgptAssistant
76
83
  end
77
84
 
78
85
  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])
86
+ title = evnt.message.content.split[1..].join(" ")
87
+ mode = nil
88
+ if title.include? ":"
89
+ mode = title.split(":").last.to_i
90
+ title = title.split(":").first
91
+ end
92
+ actors = AwesomeChatgptActors::CastControl.actors
93
+ return send_message "invalid mode" unless (mode.to_i >= 1 && mode.to_i <= actors.size + 1) || mode.nil?
94
+ return send_message "invalid chat title" if title.nil? || title.empty?
95
+ return send_message "chat title already exists" if user.chat_by_title(title)
96
+
97
+ actor_name = actors[mode.to_i - 1] if mode
98
+ actor = AwesomeChatgptActors::Actor.new(role: actor_name, language: config.language) if actor_name
99
+ chat = Chat.new(user_id: user.id, status: 0, title: title, actor: actor_name, prompt: actor.prompt) if actor
100
+ chat = Chat.new(user_id: user.id, status: 0, title: title) unless actor
101
+ return send_message "Something went wrong", msg.chat.id unless chat
102
+
103
+ chat.save ? chat_created_message(chat) : send_message(error_messages[:chat_creation])
82
104
  end
83
105
 
84
106
  def answer_action
85
- response = chatter.chat(message.content, chat.id)
107
+ response = chatter.chat(message.content, chat.id, error_messages[:something_went_wrong])
86
108
  send_message response
87
109
  end
88
110
 
@@ -25,7 +25,7 @@ module ChatgptAssistant
25
25
 
26
26
  def ask_to_speak_action
27
27
  Message.create(chat_id: chat.id, content: message, role: "user")
28
- response = chatter.chat(message, chat.id)
28
+ response = chatter.chat(message, chat.id, error_messages[:something_went_wrong])
29
29
  audio_path = synthesis.synthesize_text(response)
30
30
  speak_answer_action(audio_path, response)
31
31
  end
@@ -4,13 +4,17 @@ module ChatgptAssistant
4
4
  # Helper for messenger
5
5
  module MessengerHelper
6
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 }
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
11
15
  end
12
16
 
13
- def respond_with_success
17
+ def respond_with_success(chat)
14
18
  user.update(current_chat_id: chat.id)
15
19
  send_message success_messages[:chat_created]
16
20
  end
@@ -48,86 +52,81 @@ module ChatgptAssistant
48
52
  end
49
53
 
50
54
  def user_logged_message
51
- register_user_action("login", user.id)
52
55
  user.update(telegram_id: msg.chat.id)
53
- 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)
54
57
  evnt&.respond success_messages[:user_logged_in] if evnt.present?
55
58
  end
56
59
 
57
60
  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
+ bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:invalid_command]) if bot.respond_to?(:api)
61
62
  evnt&.respond error_messages[:invalid_command]
62
63
  end
63
64
 
64
65
  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?
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
- 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?
71
+ bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:user_created]) if bot.respond_to?(:api)
73
72
  evnt&.respond success_messages[:user_created] if evnt.present?
74
73
  end
75
74
 
76
75
  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?
76
+ bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:user_creation]) if bot.respond_to?(:api)
79
77
  evnt&.respond error_messages[:user_creation] if evnt.present?
80
78
  end
81
79
 
82
80
  def chat_created_message(chat)
83
- register_user_action("chat_created", user.id) if user
84
81
  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?
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?
86
96
  end
87
97
 
88
98
  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])
99
+ bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:user_not_logged_in]) if bot.respond_to?(:api)
91
100
  evnt&.respond(error_messages[:user_not_logged_in])
92
101
  end
93
102
 
94
103
  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])
104
+ bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:password]) if bot.respond_to?(:api)
97
105
  evnt&.respond(error_messages[:password])
98
106
  end
99
107
 
100
108
  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])
109
+ bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:chat_not_found]) if bot.respond_to?(:api)
104
110
  evnt&.respond(error_messages[:chat_not_found])
105
111
  end
106
112
 
107
113
  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])
114
+ bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:no_chat_selected]) if bot.respond_to?(:api)
111
115
  evnt&.respond(error_messages[:no_chat_selected])
112
116
  end
113
117
 
114
118
  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])
119
+ bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:no_messages_founded]) if bot.respond_to?(:api)
118
120
  evnt&.respond(error_messages[:no_messages_founded])
119
121
  end
120
122
 
121
123
  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])
124
+ bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:chat_creation_failed]) if bot.respond_to?(:api)
125
125
  evnt&.respond(error_messages[:chat_creation_failed])
126
126
  end
127
127
 
128
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])
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
@@ -15,33 +15,6 @@ module ChatgptAssistant
15
15
  user ? chat_if_exists : not_logged_in_message
16
16
  end
17
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
18
  def telegram_user_create(visitor_id, email, password)
46
19
  visitor = Visitor.find_by(id: visitor_id)
47
20
  return false unless visitor
@@ -52,22 +25,33 @@ module ChatgptAssistant
52
25
  end
53
26
 
54
27
  def telegram_send_start_message
55
- send_message commom_messages[:start], msg.chat.id
28
+ send_message common_messages[:start], msg.chat.id
56
29
  help_message = help_messages.join("\n").to_s
57
30
  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
31
+ send_message common_messages[:start_helper], msg.chat.id
32
+ send_message common_messages[:start_sec_helper], msg.chat.id
60
33
  end
61
34
 
62
35
  def telegram_create_chat
63
36
  text = msg.text
64
37
  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
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
68
53
 
69
- def telegram_user_history
70
- user.current_chat.messages.last(10).map { |m| "#{m.role}: #{m.content}\nat: #{m.created_at}" }
54
+ chat.save ? chat_created_message(chat) : chat_creation_failed_message
71
55
  end
72
56
 
73
57
  def telegram_text_or_audio?
@@ -0,0 +1,33 @@
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
@@ -25,5 +25,14 @@ module ChatgptAssistant
25
25
  def discord_voice_bot_connected?
26
26
  user && evnt.user.voice_channel && evnt.voice && !chat.nil?
27
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
28
37
  end
29
38
  end
@@ -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