chatgpt_assistant 0.1.5 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/.env_sample +17 -8
  3. data/.rubocop.yml +17 -6
  4. data/Dockerfile +11 -5
  5. data/Gemfile +36 -22
  6. data/Gemfile.lock +143 -102
  7. data/README.md +25 -27
  8. data/Rakefile +1 -0
  9. data/docker-compose.yml +25 -3
  10. data/exe/chatgpt_assistant +1 -1
  11. data/exe/chatgpt_bot +3 -0
  12. data/lib/{bots → chatgpt_assistant/bots}/application_bot.rb +15 -11
  13. data/lib/chatgpt_assistant/bots/discord/actions.rb +104 -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 +42 -0
  22. data/lib/chatgpt_assistant/bots/helpers/messenger_helper.rb +16 -0
  23. data/lib/{bots → chatgpt_assistant/bots}/helpers/search_helper.rb +3 -3
  24. data/lib/chatgpt_assistant/bots/jobs/new_chat_job.rb +17 -0
  25. data/lib/chatgpt_assistant/bots/jobs/register_job.rb +16 -0
  26. data/lib/chatgpt_assistant/bots/jobs/voice_connect_job.rb +14 -0
  27. data/lib/chatgpt_assistant/bots/mailers/account_mailer.rb +30 -0
  28. data/lib/chatgpt_assistant/bots/mailers/application_mailer.rb +21 -0
  29. data/lib/chatgpt_assistant/bots/services/new_chat_service.rb +59 -0
  30. data/lib/chatgpt_assistant/bots/services/register_service.rb +45 -0
  31. data/lib/chatgpt_assistant/bots/services/voice_connect_service.rb +29 -0
  32. data/lib/chatgpt_assistant/bots/telegram/actions.rb +54 -0
  33. data/lib/chatgpt_assistant/bots/telegram/auth.rb +40 -0
  34. data/lib/chatgpt_assistant/bots/telegram/bot.rb +17 -0
  35. data/lib/chatgpt_assistant/bots/telegram/chat_actions.rb +30 -0
  36. data/lib/chatgpt_assistant/bots/telegram/events.rb +120 -0
  37. data/lib/chatgpt_assistant/bots/telegram/events_controller.rb +48 -0
  38. data/lib/chatgpt_assistant/bots/telegram/permissions.rb +48 -0
  39. data/lib/chatgpt_assistant/bots/telegram/validations.rb +55 -0
  40. data/lib/chatgpt_assistant/bots/telegram/voice_actions.rb +36 -0
  41. data/lib/chatgpt_assistant/bots/telegram_bot.rb +44 -0
  42. data/lib/chatgpt_assistant/chatter.rb +10 -2
  43. data/lib/chatgpt_assistant/config.rb +27 -2
  44. data/lib/chatgpt_assistant/default_messages.rb +79 -31
  45. data/lib/chatgpt_assistant/error.rb +228 -0
  46. data/lib/chatgpt_assistant/migrations.rb +5 -0
  47. data/lib/chatgpt_assistant/models.rb +38 -13
  48. data/lib/chatgpt_assistant/sidekiq.rb +7 -0
  49. data/lib/chatgpt_assistant/sidekiq.yml +10 -0
  50. data/lib/chatgpt_assistant/version.rb +1 -1
  51. data/lib/chatgpt_assistant.rb +8 -15
  52. data/prompts-data/{awesome-chatgpt-prompts.csv → awesome-en-prompts.csv} +164 -164
  53. data/prompts-data/awesome-pt-prompts.csv +164 -0
  54. metadata +40 -21
  55. data/.env_prod_sample +0 -18
  56. data/docker-compose.prod.yml +0 -34
  57. data/lib/bots/discord_bot.rb +0 -185
  58. data/lib/bots/helpers/authentication_helper.rb +0 -47
  59. data/lib/bots/helpers/discord_helper.rb +0 -124
  60. data/lib/bots/helpers/discord_voice_helper.rb +0 -50
  61. data/lib/bots/helpers/logger_action_helper.rb +0 -7
  62. data/lib/bots/helpers/messenger_helper.rb +0 -134
  63. data/lib/bots/helpers/telegram_helper.rb +0 -77
  64. data/lib/bots/helpers/telegram_voice_helper.rb +0 -33
  65. data/lib/bots/helpers/validation_helper.rb +0 -38
  66. data/lib/bots/helpers/visit_helper.rb +0 -24
  67. data/lib/bots/telegram_bot.rb +0 -149
  68. /data/lib/{bots → chatgpt_assistant/bots}/helpers/audio_helper.rb +0 -0
  69. /data/lib/{bots → chatgpt_assistant/bots}/helpers/file_helper.rb +0 -0
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChatgptAssistant
4
+ module Bots
5
+ module Discord
6
+ module VoiceCheckers
7
+ def voice_connect_checker_action
8
+ send_message error_messages[:user_not_logged_in] if user.nil?
9
+ send_message error_messages[:chat_not_found] if user && chat.nil?
10
+ end
11
+
12
+ def voice_connection_checker_action
13
+ send_message error_messages[:user_not_in_voice_channel] if evnt.user.voice_channel.nil? && user
14
+ send_message error_messages[:bot_already_connected] if evnt.voice && user
15
+ end
16
+
17
+ def speak_connect_checker_action
18
+ send_message error_messages[:user_not_logged_in] if user.nil?
19
+ send_message error_messages[:chat_not_found] if user && evnt.user.voice_channel && evnt.voice && chat.nil?
20
+ end
21
+
22
+ def speak_connection_checker_action
23
+ send_message error_messages[:user_not_in_voice_channel] if evnt.user.voice_channel.nil? && user
24
+ send_message error_messages[:bot_not_in_voice_channel] if !evnt.voice && user
25
+ end
26
+
27
+ def disconnect_checker_action
28
+ send_message error_messages[:user_not_logged_in] if user.nil?
29
+ send_message error_messages[:user_not_in_voice_channel] if evnt.user.voice_channel.nil? && user
30
+ send_message error_messages[:user_not_connected] if !evnt.voice && user
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "discord/bot"
4
+ require_relative "discord/auth"
5
+ require_relative "discord/actions"
6
+ require_relative "discord/chat_actions"
7
+ require_relative "discord/events"
8
+ require_relative "discord/voice_actions"
9
+ require_relative "discord/voice_checkers"
10
+
11
+ module ChatgptAssistant
12
+ # This class is responsible to handle the discord bot
13
+ class DiscordBot < ApplicationBot
14
+ def start
15
+ start_event
16
+ login_event
17
+ list_event
18
+ hist_event
19
+ help_event
20
+ new_chat_event
21
+ sl_chat_event
22
+ ask_event
23
+ voice_connect_event
24
+ voice_disconnect_event
25
+ private_message_event
26
+ bot_init
27
+ end
28
+
29
+ private
30
+
31
+ attr_reader :message
32
+ attr_accessor :evnt, :user, :chats, :chat
33
+
34
+ include Bots::Discord::Bot
35
+ include Bots::Discord::Auth
36
+ include Bots::Discord::Actions
37
+ include Bots::Discord::ChatActions
38
+ include Bots::Discord::Events
39
+ include Bots::Discord::VoiceActions
40
+ include Bots::Discord::VoiceCheckers
41
+ end
42
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChatgptAssistant
4
+ # Helper for messenger
5
+ module MessengerHelper
6
+ def parse_message(message, max_length)
7
+ if message.length > max_length
8
+ array = message.scan(/.{1,#{max_length}}/) if max_length.positive?
9
+ array = ["Something went wrong! Try again later"] if max_length <= 0
10
+ else
11
+ array = [message]
12
+ end
13
+ array
14
+ end
15
+ end
16
+ 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
@@ -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, name, chat_id)
13
+ RegisterService.new(email, password, name, 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,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "application_mailer"
4
+
5
+ module ChatgptAssistant
6
+ # This class is responsible for the register mailer
7
+ class AccountMailer < ApplicationMailer
8
+ def register_email(user, token)
9
+ @user = user
10
+ @token = token
11
+ mail(to: user.email, subject: "Welcome to ChatGPT Assistant",
12
+ content_type: "text/html",
13
+ body: register_html)
14
+ end
15
+
16
+ private
17
+
18
+ attr_reader :user, :token
19
+
20
+ def register_html
21
+ "<h1>Welcome to ChatGPT Assistant</h1> \n
22
+ <p>Hi #{user.name}</p> \n
23
+ <p>You are now registered to ChatGPT Assistant</p> \n
24
+ <p>You can now start using the bot</p> \n
25
+ <p>To start using the bot, copy and paste the following link in TELEGRAM bot:</p> \n
26
+ <p>confirm/* #{user.name}:#{token}</p> \n
27
+ "
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Gem module
4
+ module ChatgptAssistant
5
+ ActionMailer::Base.raise_delivery_errors = true
6
+ ActionMailer::Base.delivery_method = :smtp
7
+ ActionMailer::Base.smtp_settings = {
8
+ address: ENV.fetch("SMTP_ADDRESS", nil),
9
+ port: ENV.fetch("SMTP_PORT", nil),
10
+ domain: ENV.fetch("SMTP_DOMAIN", nil),
11
+ user_name: ENV.fetch("SMTP_USER_NAME", nil),
12
+ password: ENV.fetch("SMTP_PASSWORD", nil),
13
+ authentication: ENV.fetch("SMTP_AUTHENTICATION", nil),
14
+ enable_starttls_auto: ENV.fetch("SMTP_ENABLE_STARTTLS_AUTO", nil) == "true"
15
+ }
16
+
17
+ # This class is responsible for the application mailer
18
+ class ApplicationMailer < ActionMailer::Base
19
+ default from: "support@chat.outerspacecoding.com"
20
+ end
21
+ end
@@ -0,0 +1,59 @@
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 actors
18
+ @actors ||= AwesomeChatgptActors::CastControl.actors
19
+ end
20
+
21
+ def actor
22
+ @actor_name = actors[@actor_mode.to_i - 1] if @actor_mode
23
+ @actor = AwesomeChatgptActors::Actor.new(role: @actor_name, language: @config.language) if @actor_name
24
+ end
25
+
26
+ def chat
27
+ @chat = Chat.new(title: @chat_title, user_id: @user_id, status: 0, actor: @actor_name, prompt: @actor.prompt) if !@actor.nil? && @actor_name
28
+ @chat = Chat.new(title: @chat_title, user_id: @user_id, status: 0) unless @actor_name
29
+ end
30
+
31
+ def parse_title
32
+ return @chat_title.strip! unless @chat_title.include?("actor:")
33
+
34
+ @actor_mode = @chat_title.split("actor:")[1].strip
35
+ @chat_title = @chat_title.split("actor:")[0].strip
36
+ end
37
+
38
+ def call
39
+ raise NilError if @chat_title.nil?
40
+ raise NilError if @user_id.nil?
41
+ raise NilError if @chat_id.nil?
42
+
43
+ parse_title
44
+
45
+ raise InvalidModeError unless (@actor_mode.to_i >= 1 && @actor_mode.to_i <= actors.size + 1) || @actor_mode.nil?
46
+ raise InvalidChatTitleError if @chat_title.nil? || @chat_title.empty?
47
+ raise ChatAlreadyExistsError if Chat.find_by(title: @chat_title)
48
+
49
+ if chat.save
50
+ telegram_async.send_message("Chat created succesfully", @chat_id)
51
+ else
52
+ raise ChatAlreadyExistsError if Chat.find_by(title: @chat_title)
53
+ raise NilError if chat.errors[:title].any?
54
+ end
55
+ rescue ChatAlreadyExistsError, NilError => e
56
+ telegram_async.send_message(e.message, @chat_id)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,45 @@
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, name, chat_id)
7
+ @name = name
8
+ @email = email
9
+ @password = password
10
+ @chat_id = chat_id
11
+ @config = Config.new
12
+ @config.db_connection
13
+ end
14
+
15
+ def call
16
+ return if user_already_exists?
17
+
18
+ @user = User.new(email: @email, password: @password, telegram_id: @chat_id, name: @name)
19
+ @user.save ? success_message : error_message
20
+ rescue UserAlreadyExistsError, WrongEmailError, WrongPasswordError => e
21
+ telegram_async.send_message(e.message, @chat_id)
22
+ end
23
+
24
+ private
25
+
26
+ def telegram_async
27
+ TelegramBot.new(@config)
28
+ end
29
+
30
+ def success_message
31
+ confirmation_token = @user.encrypt_token
32
+ telegram_async.send_message("Registration successful! Check your email for confirmation.", @chat_id)
33
+ AccountMailer.register_email(@user, confirmation_token).deliver_now
34
+ end
35
+
36
+ def error_message
37
+ raise WrongEmailError if @user.errors[:email].any?
38
+ raise WrongPasswordError if @user.errors[:password].any?
39
+ end
40
+
41
+ def user_already_exists?
42
+ raise UserAlreadyExistsError if User.find_by(email: @email)
43
+ end
44
+ end
45
+ 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
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChatgptAssistant
4
+ module Bots
5
+ module Telegram
6
+ module Actions
7
+ def telegram_user_create(visitor_id, email, password)
8
+ visitor = Visitor.find_by(id: visitor_id)
9
+ return false unless visitor
10
+
11
+ visitor.tel_user.update(telegram_id: nil) if visitor.tel_user.present?
12
+ user = User.new(telegram_id: visitor.telegram_id, email: email, password: password)
13
+ user.save ? user.email : user.errors.full_messages
14
+ end
15
+
16
+ def telegram_send_start_message
17
+ send_message common_messages[:start], msg.chat.id
18
+ help_message = help_messages.join("\n").to_s
19
+ send_message help_message, msg.chat.id
20
+ send_message common_messages[:start_helper], msg.chat.id
21
+ send_message common_messages[:start_sec_helper], msg.chat.id
22
+ end
23
+
24
+ def telegram_create_chat
25
+ text = msg.text
26
+ title = text.split("/").last
27
+ mode = nil
28
+ if title.include?(":")
29
+ mode = title.split(":").last
30
+ title = title.split(":").first
31
+ end
32
+ actor_modes = AwesomeChatgptActors::CastControl.actors
33
+ return send_message "invalid mode", msg.chat.id unless (mode.to_i >= 1 && mode.to_i <= actor_modes.size + 1) || mode.nil?
34
+ return send_message "invalid chat title", msg.chat.id if title.nil? || title.empty?
35
+ return send_message "You already have a chat with this title", msg.chat.id if user.chat_by_title(title)
36
+
37
+ actor_name = actor_modes[mode.to_i - 1] if mode
38
+ actor = AwesomeChatgptActors::Actor.new(prompt_type: actor_name) if actor_name
39
+ chat = Chat.new(user_id: user.id, status: 0, title: title, actor: actor_name, prompt: actor.prompt) if actor
40
+ chat = Chat.new(user_id: user.id, status: 0, title: title) unless actor
41
+ return send_message "Something went wrong", msg.chat.id unless chat
42
+
43
+ chat.save ? chat_created_message(chat) : send_message(chat_id: msg.chat.id, text: error_messages[:chat_creation_failed])
44
+ end
45
+
46
+ def chat_created_message(chat)
47
+ bot.api.send_message(chat_id: msg.chat.id, text: "Intructions sended to actor:\n#{chat.prompt}") unless chat.actor.nil?
48
+ bot.api.send_message(chat_id: msg.chat.id, text: "Response from assistant:\n#{chat.messages[1].content}") unless chat.actor.nil?
49
+ bot.api.send_message(chat_id: msg.chat.id, text: success_messages[:chat_created]) if bot.respond_to?(:api)
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChatgptAssistant
4
+ module Bots
5
+ module Telegram
6
+ module Auth
7
+ def authenticate_user(email, password)
8
+ case telegram_user_auth(email, password, msg.chat.id)
9
+ when "user not found"
10
+ raise UserNotFoundError
11
+ when "wrong password"
12
+ raise WrongPasswordError
13
+ when email
14
+ send_message(chat_id: msg.chat.id, text: success_messages[:user_logged_in])
15
+ end
16
+ end
17
+
18
+ def telegram_user_auth(email, password, telegram_id)
19
+ return "wrong password" if password.nil?
20
+
21
+ visitor_access = find_visitor(telegram_id: telegram_id)
22
+ return "something went wrong" unless visitor_access
23
+
24
+ new_access = find_user(email: email)
25
+ return "user not found" unless new_access
26
+
27
+ user.valid_password?(password) ? telegram_user_access(visitor_access, new_access) : "wrong password"
28
+ end
29
+
30
+ def telegram_user_access(visitor, new_access)
31
+ other_access = where_user(telegram_id: visitor.telegram_id)
32
+ other_access&.each { |access| access.update(telegram_id: nil) } if other_access&.class == Array
33
+ other_access&.update(telegram_id: nil) if other_access&.class == User
34
+ new_access.update(telegram_id: visitor.telegram_id)
35
+ new_access.email
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChatgptAssistant
4
+ module Bots
5
+ module Telegram
6
+ module Bot
7
+ def bot
8
+ @bot ||= ::Telegram::Bot::Client.new(telegram_token, logger: Logger.new($stderr))
9
+ end
10
+
11
+ def user
12
+ @user = find_user(telegram_id: msg.chat.id)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChatgptAssistant
4
+ module Bots
5
+ module Telegram
6
+ module ChatActions
7
+ def chat_if_exists
8
+ chat = Chat.find_by(user_id: user.id, id: user.current_chat_id)
9
+ chat ? chat_success(chat.id) : send_message(chat_id: msg.chat.id, text: error_messages[:no_chat_selected])
10
+ end
11
+
12
+ def chat_success(chat_id)
13
+ user_message = Message.new(chat_id: chat_id, content: msg.text, role: "user")
14
+ if user_message&.save
15
+ text = chatter.chat(msg.text, chat_id, error_messages[:something_went_wrong])
16
+ mess = parse_message(text, 4096)
17
+ mess.each { |m| send_message m, msg.chat.id }
18
+ else
19
+ send_message error_messages[:something_went_wrong], msg.chat.id
20
+ end
21
+ end
22
+
23
+ def send_message(text, chat_id)
24
+ messages = parse_message(text, 4096)
25
+ messages.each { |m| bot.api.send_message(chat_id: chat_id, text: m) }
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChatgptAssistant
4
+ module Bots
5
+ module Telegram
6
+ module Events
7
+ def start_event
8
+ telegram_send_start_message
9
+ end
10
+
11
+ def help_event
12
+ help_messages.each { |m| send_message m, msg.chat.id }
13
+ end
14
+
15
+ def hist_event
16
+ hist_allowed?
17
+
18
+ user.chat_history.each do |m|
19
+ send_message m, msg.chat.id
20
+ end
21
+ rescue NoChatSelectedError, UserNotLoggedInError, NoMessagesFoundedError, AccountNotVerifiedError => e
22
+ send_message e.message, msg.chat.id
23
+ end
24
+
25
+ def list_event
26
+ list_allowed?
27
+
28
+ send_message common_messages[:chat_list], msg.chat.id
29
+ chats_str = ""
30
+ user.chats.each_with_index { |c, i| chats_str += "Chat #{i + 1} - #{c.title}\n" }
31
+ send_message chats_str, msg.chat.id
32
+ rescue NoChatsFoundedError, UserNotLoggedInError, AccountNotVerifiedError => e
33
+ send_message e.message, msg.chat.id
34
+ end
35
+
36
+ def confirm_account_event
37
+ raise UserNotLoggedInError if user.nil?
38
+
39
+ user_info = msg.text.split("/* ").last
40
+ name, token = user_info.split(":")
41
+ send_message "#{name} - #{token}", msg.chat.id
42
+ if user_confirmed?
43
+ send_message success_messages[:account_confirmed], msg.chat.id
44
+ else
45
+ send_message error_messages[:account_not_confirmed], msg.chat.id
46
+ end
47
+ rescue UserNotLoggedInError => e
48
+ send_message e.message, msg.chat.id
49
+ end
50
+
51
+ def login_event
52
+ raise UserLoggedInError if user
53
+
54
+ user_info = msg.text.split("/").last
55
+ email, password = user_info.split(":")
56
+ authenticate_user(email, password)
57
+ rescue UserNotFoundError, WrongPasswordError, UserLoggedInError => e
58
+ send_message e.message, msg.chat.id
59
+ end
60
+
61
+ def register_event
62
+ user_info = msg.text.split("/").last
63
+ email, password = register_allowed?(user_info)
64
+ name = msg.from.first_name || "Anonymous"
65
+
66
+ RegisterJob.perform_async(email, password, name, visitor.telegram_id)
67
+ rescue NoRegisterInfoError, UserLoggedInError => e
68
+ send_message e.message, msg.chat.id
69
+ end
70
+
71
+ def sign_out_event
72
+ raise UserNotLoggedInError if user.nil?
73
+
74
+ user.update(telegram_id: nil)
75
+ send_message success_messages[:user_logged_out], msg.chat.id
76
+ rescue UserNotLoggedInError => e
77
+ send_message e.message, msg.chat.id
78
+ end
79
+
80
+ def new_chat_event
81
+ common_allowed?
82
+ NewChatJob.perform_async(msg.text.split("/").last, user.id, msg.chat.id)
83
+ rescue UserNotLoggedInError, AccountNotVerifiedError => e
84
+ send_message e.message, msg.chat.id
85
+ end
86
+
87
+ def select_chat_event
88
+ common_allowed?
89
+ title = msg.text.split("/").last
90
+ chat = user.chat_by_title(title)
91
+ select_allowed?(chat)
92
+
93
+ send_message success_messages[:chat_selected], msg.chat.id
94
+ rescue UserNotLoggedInError, ChatNotFoundError, AccountNotVerifiedError => e
95
+ send_message e.message, msg.chat.id
96
+ end
97
+
98
+ def audio_event
99
+ audio_allowed?
100
+ user_audio = transcribe_file(telegram_audio_url)
101
+ message = Message.new(content: user_audio[:text], chat_id: user.current_chat_id, role: "user")
102
+ raise MessageNotCreatedError unless message.save
103
+
104
+ ai_response = telegram_process_ai_voice(user_audio[:file])
105
+ telegram_send_voice_message(voice: ai_response[:voice], text: ai_response[:text])
106
+ delete_file ai_response[:voice]
107
+ rescue UserNotLoggedInError, NoChatSelectedError, MessageNotCreatedError, AccountNotVerifiedError => e
108
+ send_message e.message, msg.chat.id
109
+ end
110
+
111
+ def telegram_chat_event
112
+ common_allowed?
113
+ chat_if_exists
114
+ rescue UserNotLoggedInError, AccountNotVerifiedError => e
115
+ send_message e.message, msg.chat.id
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChatgptAssistant
4
+ module Bots
5
+ module Telegram
6
+ module EventsController
7
+ def auth_events
8
+ return login_event if msg.text.include?("login/")
9
+ return register_event if msg.text.include?("register/")
10
+ return sign_out_event if msg.text.include?("sign_out/")
11
+ return confirm_account_event if msg.text.include?("confirm/* ")
12
+ end
13
+
14
+ def action_events
15
+ return auth_events if auth_event?
16
+ return new_chat_event if msg.text.include?("new_chat/")
17
+ return select_chat_event if msg.text.include?("sl_chat/")
18
+ return telegram_chat_event unless telegram_actions?
19
+
20
+ raise InvalidCommandError
21
+ rescue InvalidCommandError => e
22
+ send_message e.message, msg.chat.id
23
+ end
24
+
25
+ def text_events
26
+ case msg.text
27
+ when "/start"
28
+ start_event
29
+ when "/help"
30
+ help_event
31
+ when "/hist"
32
+ hist_event
33
+ when "/list"
34
+ list_event
35
+ when "/stop"
36
+ stop_event
37
+ when nil
38
+ raise NilError
39
+ else
40
+ action_events
41
+ end
42
+ rescue NilError => e
43
+ send_message e.message, msg.chat.id
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end