chatgpt_assistant 0.1.6 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/.env_sample +35 -10
  3. data/.rubocop.yml +14 -3
  4. data/Dockerfile +11 -5
  5. data/Gemfile +25 -23
  6. data/Gemfile.lock +123 -85
  7. data/README.md +22 -22
  8. data/Rakefile +1 -0
  9. data/docker-compose.yml +8 -4
  10. data/exe/chatgpt_assistant +1 -1
  11. data/lib/chatgpt_assistant/audio_synthesis.rb +54 -15
  12. data/lib/{bots → chatgpt_assistant/bots}/application_bot.rb +0 -6
  13. data/lib/chatgpt_assistant/bots/discord/actions.rb +103 -0
  14. data/lib/chatgpt_assistant/bots/discord/auth.rb +36 -0
  15. data/lib/chatgpt_assistant/bots/discord/bot.rb +29 -0
  16. data/lib/chatgpt_assistant/bots/discord/chat_actions.rb +33 -0
  17. data/lib/chatgpt_assistant/bots/discord/events.rb +138 -0
  18. data/lib/chatgpt_assistant/bots/discord/validations.rb +38 -0
  19. data/lib/chatgpt_assistant/bots/discord/voice_actions.rb +33 -0
  20. data/lib/chatgpt_assistant/bots/discord/voice_checkers.rb +35 -0
  21. data/lib/chatgpt_assistant/bots/discord_bot.rb +41 -0
  22. data/lib/chatgpt_assistant/bots/helpers/messenger_helper.rb +16 -0
  23. data/lib/{bots → chatgpt_assistant/bots}/jobs/register_job.rb +2 -2
  24. data/lib/chatgpt_assistant/bots/mailers/account_mailer.rb +30 -0
  25. data/lib/chatgpt_assistant/bots/mailers/application_mailer.rb +21 -0
  26. data/lib/chatgpt_assistant/bots/services/new_chat_service.rb +59 -0
  27. data/lib/chatgpt_assistant/bots/services/register_service.rb +45 -0
  28. data/lib/chatgpt_assistant/bots/telegram/actions.rb +54 -0
  29. data/lib/chatgpt_assistant/bots/telegram/auth.rb +40 -0
  30. data/lib/chatgpt_assistant/bots/telegram/bot.rb +17 -0
  31. data/lib/chatgpt_assistant/bots/telegram/chat_actions.rb +30 -0
  32. data/lib/chatgpt_assistant/bots/telegram/events.rb +121 -0
  33. data/lib/chatgpt_assistant/bots/telegram/events_controller.rb +48 -0
  34. data/lib/chatgpt_assistant/bots/telegram/permissions.rb +48 -0
  35. data/lib/chatgpt_assistant/bots/telegram/validations.rb +55 -0
  36. data/lib/chatgpt_assistant/bots/telegram/voice_actions.rb +36 -0
  37. data/lib/chatgpt_assistant/bots/telegram_bot.rb +44 -0
  38. data/lib/chatgpt_assistant/chatter.rb +10 -2
  39. data/lib/chatgpt_assistant/config.rb +7 -1
  40. data/lib/chatgpt_assistant/default_messages.rb +10 -4
  41. data/lib/chatgpt_assistant/error.rb +9 -0
  42. data/lib/chatgpt_assistant/migrations.rb +5 -0
  43. data/lib/chatgpt_assistant/models.rb +20 -0
  44. data/lib/chatgpt_assistant/version.rb +1 -1
  45. data/lib/chatgpt_assistant.rb +5 -3
  46. metadata +35 -25
  47. data/.env_prod_sample +0 -18
  48. data/docker-compose.prod.yml +0 -34
  49. data/lib/bots/discord_bot.rb +0 -182
  50. data/lib/bots/helpers/authentication_helper.rb +0 -48
  51. data/lib/bots/helpers/discord_helper.rb +0 -124
  52. data/lib/bots/helpers/discord_voice_helper.rb +0 -50
  53. data/lib/bots/helpers/messenger_helper.rb +0 -133
  54. data/lib/bots/helpers/telegram_helper.rb +0 -73
  55. data/lib/bots/helpers/telegram_voice_helper.rb +0 -33
  56. data/lib/bots/helpers/validation_helper.rb +0 -38
  57. data/lib/bots/helpers/visit_helper.rb +0 -28
  58. data/lib/bots/services/new_chat_service.rb +0 -34
  59. data/lib/bots/services/register_service.rb +0 -36
  60. data/lib/bots/telegram_bot.rb +0 -180
  61. /data/lib/{bots → chatgpt_assistant/bots}/helpers/audio_helper.rb +0 -0
  62. /data/lib/{bots → chatgpt_assistant/bots}/helpers/file_helper.rb +0 -0
  63. /data/lib/{bots → chatgpt_assistant/bots}/helpers/search_helper.rb +0 -0
  64. /data/lib/{bots → chatgpt_assistant/bots}/jobs/new_chat_job.rb +0 -0
  65. /data/lib/{bots → chatgpt_assistant/bots}/jobs/voice_connect_job.rb +0 -0
  66. /data/lib/{bots → chatgpt_assistant/bots}/services/voice_connect_service.rb +0 -0
@@ -1,124 +0,0 @@
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 common_messages[:start_helper].gsub("register/", "gpt!register ")
16
- send_message common_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 common_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 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
-
78
- def sl_chat_action(chat_to_select)
79
- @chat = user.chat_by_title(chat_to_select)
80
- send_message error_messages[:chat_not_found] if chat.nil?
81
- user.update(current_chat_id: chat.id) if chat
82
- send_message success_messages[:chat_selected] if chat
83
- end
84
-
85
- def create_chat_action
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])
104
- end
105
-
106
- def answer_action
107
- response = chatter.chat(message.content, chat.id, error_messages[:something_went_wrong])
108
- send_message response
109
- end
110
-
111
- def disconnect_checker_action
112
- send_message error_messages[:user_not_logged_in] if user.nil?
113
- send_message error_messages[:user_not_in_voice_channel] if evnt.user.voice_channel.nil? && user
114
- send_message error_messages[:user_not_connected] if !evnt.voice && user
115
- end
116
-
117
- def discord_user_create(discord_id, email, password)
118
- user = User.new(discord_id: discord_id, email: email, password: password)
119
- last_access = find_user(discord_id: discord_id)
120
- last_access&.update(discord_id: nil)
121
- user.save
122
- end
123
- end
124
- end
@@ -1,50 +0,0 @@
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, error_messages[:something_went_wrong])
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
@@ -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