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
data/README.md CHANGED
@@ -6,15 +6,18 @@
6
6
 
7
7
  This Ruby gem provides an easy way to initialize a client for Telegram and Discord bots using the ChatGPT API, audio transcription, IBM Cloud Text to Speech, and AWS Polly, creating an assistant that can answer questions in text and voice and have a conversation with the user.
8
8
 
9
- #### Languages supported currently: en, pt - contributions are welcome!
10
- You can contribute by adding your language to the DefaultMessages class in lib/chatgpt_assistant/default_messages.rb. You can also add your language to the list of languages in the README.md file.
11
- Pull request from language/YOUR_LANGUAGE to main, remember to name your pull request as "Add YOUR_LANGUAGE support".
9
+ #### Discord Voice Bot temporary disabled due connection issues
10
+ #### Telegram voice messages are working fine
11
+ #### Languages supported currently: en, pt
12
+
13
+ #### Registration and Account Confirmation are avaiable on telegram only, sign in with the same credentials on discord after registering on telegram.
12
14
 
13
15
  Other contributions are welcome too!
14
16
  Remember to give a star to this repo if you like it!
15
17
  ## Requirements
16
18
 
17
- - Ruby > 2.6.0
19
+ - Ruby > 2.6.0 - for the gem who can create bots projects in your machine
20
+ - Ruby 3.2.2 - inside the docker container or run the bots in your machine
18
21
  - Docker
19
22
  - Docker Compose
20
23
  - PostgreSQL
@@ -63,6 +66,11 @@ Remember to edit docker-compose.prod.yml when deploying to production.
63
66
  rake compose:up && sudo docker compose run --rm telegram exe/chatgpt_bot migrate
64
67
  ```
65
68
 
69
+ ## Install [Awesome Chatgpt Actors](https://github.com/JesusGautamah/awesome-chatgpt-actors)
70
+ ```bash
71
+ rake chatgpt_actors:install
72
+ ```
73
+
66
74
  ## Usage
67
75
 
68
76
  You can start the Docker Compose services required for the gem using the rake tasks provided by the Lucy Dockerunner gem. These tasks include compose:up, compose:down, compose:status, compose:shell, compose:restart, and others listed previously.
@@ -87,39 +95,29 @@ After starting the Docker Compose services, you can use the features of the gem
87
95
  ## Discord Bot Commands
88
96
 
89
97
  The discord commands prefix can be changed in the .env file.
90
- The default prefix is dgpt!
91
-
92
- - dgpt!start - shows the welcome message
93
- - dgpt!help - shows the help message
94
- - dgpt!login email:password - logs in the user
95
- - dgpt!register email:password - registers a new user
96
- - dgpt!list - lists the user created chatbots
97
- - dgpt!sl_chat CHAT TITLE - starts a chat with the chatbot with the given title
98
- - dgpt!new_chat CHAT TITLE - creates a new chatbot with the given title
99
- - dgpt!ask TEXT - sends a text to the chatbot
100
- - dgpt!connect - connects the chat bot to the current channel
101
- - dgpt!disconnect - disconnects the chat bot from the current channel
102
- - dgpt!speak TEXT - sends a text to the chatbot and gets the response in voice
98
+ The default prefix is !!
99
+
100
+ - !!start - shows the welcome message
101
+ - !!help - shows the help message
102
+ - !!login email:password - logs in the user. Remember to use private messages to register or login and share a server with right permissions with the bot to message it.
103
+ - !!list - lists the user created chatbots
104
+ - !!sl_chat CHAT TITLE - starts a chat with the chatbot with the given title
105
+ - !!new_chat CHAT TITLE - creates a new chatbot with the given title
106
+ - !!ask TEXT - sends a text to the chatbot
103
107
 
104
108
  ## Telegram Bot Commands
105
109
 
106
110
  - /start - shows the welcome message
107
111
  - /help - shows the help message
108
- - login/email:password - logs in the user
109
- - register/email:password - registers a new user
112
+ - login/email:password - logs in the user. Remember to use private messages to register or login.
113
+ - register/email:password - registers a new user, email is not verfied yet, so you can use any email, the password is encrypted with bcrypt and stored in the database with salt. Remember to use private messages to register or login.
114
+ - confirm/* name:token - confirms the user account. Remember to use private messages to register or login
110
115
  - list - lists the user created chatbots
111
116
  - sl_chat/CHAT TITLE - starts a chat with the chatbot with the given title
112
117
  - new_chat/CHAT TITLE - creates a new chatbot with the given title
113
118
  - TEXT - sends a text to the chatbot
114
119
  - VOICE_MESSAGE or AUDIO FILE - sends a voice message to the chatbot and returns the response in voice
115
120
 
116
- ## Recommended Actions runner
117
-
118
- You can run Github Actions workflows locally using [act](https://github.com/nektos/act)
119
-
120
- We recommend Act installation to run Github Actions workflows locally
121
- as we use it to deploy the bot in a server via ssh.
122
-
123
121
  ## Contributing
124
122
 
125
123
  A good way to contribute is add your language to DefaultMessages class in lib/chatgpt_assistant/default_messages.rb. You can also add your language to the list of languages in the README.md file.
@@ -138,7 +136,7 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/JesusG
138
136
  [![Activity map](https://images.repography.com/33522702/JesusGautamah/chatgpt_assistant/recent-activity/y6ZDduNWHwPzbnUFsmdGrJ99Q1vyEKGOBWFOBvzGjnM/1rZM2QrF0__3eUfUXFe6jDraYjHvypniTqDWhCequ-U_map.svg)](https://github.com/JesusGautamah/chatgpt_assistant/commits)
139
137
 
140
138
  ## Contributors
141
- - [Jesus Gautamah](https://github.com/JesusGautamah)
139
+ - [Jesus Gautamah](https://github.com/JesusGautamah)
142
140
 
143
141
  ## License
144
142
 
data/Rakefile CHANGED
@@ -4,6 +4,7 @@
4
4
  require "rspec/core/rake_task"
5
5
  require "lucy_dockerunner"
6
6
  require "awesome_chatgpt_actors"
7
+ require "dotenv/load"
7
8
 
8
9
  RSpec::Core::RakeTask.new(:spec)
9
10
 
data/docker-compose.yml CHANGED
@@ -1,5 +1,11 @@
1
1
  version: "3.3"
2
2
  services:
3
+ redis:
4
+ image: redis:6-alpine
5
+ ports:
6
+ - "6379:6379"
7
+ volumes:
8
+ - ./redis_data:/data
3
9
  db:
4
10
  image: postgres:12.2
5
11
  ports:
@@ -12,23 +18,39 @@ services:
12
18
  build: .
13
19
  command: bash -c "exe/chatgpt_bot telegram"
14
20
  volumes:
15
- - .:/chatgpt_assistant
21
+ - .:/assistant
16
22
  environment:
17
23
  POSTGRES_HOST: db
18
24
  env_file:
19
25
  - .env
20
26
  depends_on:
21
27
  - db
28
+ - redis
29
+ - sidekiq
22
30
  discord:
23
31
  build: .
24
32
  command: bash -c "exe/chatgpt_bot discord"
25
33
  volumes:
26
- - .:/chatgpt_assistant
34
+ - .:/assistant
35
+ environment:
36
+ POSTGRES_HOST: db
37
+ env_file:
38
+ - .env
39
+ depends_on:
40
+ - db
41
+ - redis
42
+ - sidekiq
43
+ sidekiq:
44
+ build: .
45
+ command: bash -c "exe/chatgpt_bot sidekiq"
46
+ volumes:
47
+ - .:/assistant
27
48
  environment:
28
49
  POSTGRES_HOST: db
29
50
  env_file:
30
51
  - .env
31
52
  depends_on:
32
53
  - db
54
+ - redis
33
55
  volumes:
34
- postgres:
56
+ postgres:
@@ -26,7 +26,7 @@ gem_dir = Gem::Specification.find_by_name("chatgpt_assistant").gem_dir
26
26
  `cp #{gem_dir}/#{file} #{path}`
27
27
  end
28
28
 
29
- %w[docker-compose.yml docker-compose.prod.yml Dockerfile LICENSE LICENSE.txt].each do |file|
29
+ %w[docker-compose.yml Dockerfile LICENSE LICENSE.txt].each do |file|
30
30
  `cp #{gem_dir}/#{file} #{path}`
31
31
  end
32
32
 
data/exe/chatgpt_bot CHANGED
@@ -11,6 +11,9 @@ when "telegram"
11
11
  ChatgptAssistant::Main.new("telegram").start
12
12
  when "discord"
13
13
  ChatgptAssistant::Main.new("discord").start
14
+ when "sidekiq"
15
+ # start sidekiq with the lib/chatgpt_assistant/sidekiq.rb
16
+ system "bundle exec sidekiq -r ./lib/chatgpt_assistant/sidekiq.rb -C ./lib/chatgpt_assistant/sidekiq.yml"
14
17
  when "migrate"
15
18
  ChatgptAssistant::Config.new.migrate
16
19
  end
@@ -1,23 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # helpers
3
4
  require_relative "helpers/messenger_helper"
4
- require_relative "helpers/authentication_helper"
5
- require_relative "helpers/visit_helper"
6
5
  require_relative "helpers/audio_helper"
7
- require_relative "helpers/logger_action_helper"
8
6
  require_relative "helpers/search_helper"
9
- require_relative "helpers/validation_helper"
10
7
  require_relative "helpers/file_helper"
8
+
9
+ # jobs
10
+ require_relative "jobs/register_job"
11
+ require_relative "jobs/new_chat_job"
12
+ require_relative "jobs/voice_connect_job"
13
+
14
+ # services
15
+ require_relative "services/register_service"
16
+ require_relative "services/new_chat_service"
17
+ require_relative "services/voice_connect_service"
18
+
11
19
  module ChatgptAssistant
12
- # This class is responsible to contain the shared variables between the bot classes
20
+ # This class is responsible to contain the shared attributes and methods
13
21
  class ApplicationBot
14
22
  include MessengerHelper
15
- include AuthenticationHelper
16
- include VisitHelper
17
23
  include AudioHelper
18
- include LoggerActionHelper
19
24
  include SearchHelper
20
- include ValidationHelper
21
25
  include FileHelper
22
26
 
23
27
  def initialize(config)
@@ -30,7 +34,7 @@ module ChatgptAssistant
30
34
  @discord_prefix = @config.discord_prefix
31
35
  @database = @config.db_connection
32
36
  @mode = @config.mode
33
- @commom_messages = default_msg.commom_messages
37
+ @common_messages = default_msg.common_messages
34
38
  @error_messages = default_msg.error_messages
35
39
  @success_messages = default_msg.success_messages
36
40
  @help_messages = default_msg.help_messages
@@ -38,7 +42,7 @@ module ChatgptAssistant
38
42
 
39
43
  attr_reader :openai_api_key, :telegram_token, :database, :default_msg,
40
44
  :mode, :config, :discord_token, :discord_client_id,
41
- :discord_prefix, :commom_messages, :error_messages, :success_messages,
45
+ :discord_prefix, :common_messages, :error_messages, :success_messages,
42
46
  :help_messages
43
47
  attr_accessor :msg, :evnt, :bot, :audio_url, :visitor, :user, :chat, :chat_id
44
48
 
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChatgptAssistant
4
+ module Bots
5
+ module Discord
6
+ module Actions
7
+ def login_action
8
+ user_email, user_password = message.split(":")
9
+ case discord_user_auth(user_email, user_password, evnt.user.id)
10
+ when "user not found"
11
+ send_message error_messages[:user_not_found]
12
+ when "wrong password"
13
+ puts "wrong password"
14
+ send_message error_messages[:wrong_password]
15
+ when find_user(email: user_email).email
16
+ send_message success_messages[:user_logged_in]
17
+ end
18
+ end
19
+
20
+ def start_action
21
+ fields = %w[login start help new_chat sl_chat ask list hist connect disconnect speak]
22
+
23
+ values_hash = {
24
+ login: "Account login, use: #{discord_prefix}login email:password",
25
+ start: "Start the bot, use: #{discord_prefix}start",
26
+ help: "Show this message, use: #{discord_prefix}help",
27
+ new_chat: "Create a new chat, use: #{discord_prefix}new_chat chat_name",
28
+ sl_chat: "Select a chat, use: #{discord_prefix}sl_chat chat_name",
29
+ ask: "Ask a question, use: #{discord_prefix}ask question",
30
+ list: "List all chats, use: #{discord_prefix}list",
31
+ hist: "Show chat history, use: #{discord_prefix}hist",
32
+ connect: "Connect to voice channel, use: #{discord_prefix}connect",
33
+ disconnect: "Disconnect from voice channel, use: #{discord_prefix}disconnect",
34
+ speak: "Speak in voice channel, use: #{discord_prefix}speak question"
35
+ }
36
+
37
+ embed = Discordrb::Webhooks::Embed.new(
38
+ title: "Hello, #{evnt.user.name}!",
39
+ description: "I'm ChatGPT Assistant, see my commands below:",
40
+ color: "00FF00",
41
+ fields: fields.map { |field| { name: field, value: values_hash[field.to_sym] } }
42
+ )
43
+ evnt.respond nil, false, embed
44
+ end
45
+
46
+ def help_action
47
+ start_action
48
+ end
49
+
50
+ def list_action
51
+ chats = user.chats
52
+
53
+ embed = Discordrb::Webhooks::Embed.new(
54
+ title: "Hello, #{evnt.user.name}!",
55
+ description: "Your chats:",
56
+ color: "00FF00",
57
+ fields: chats.map { |field| { name: field.title, value: field.actor || "No prompt" } }
58
+ )
59
+
60
+ evnt.respond nil, false, embed
61
+ end
62
+
63
+ def hist_action
64
+ messages = Message.where(chat_id: chat.id).order(:created_at)
65
+ messages.each { |m| send_message "#{m.role} - #{m.content}\n#{m.created_at.strftime("%d/%m/%Y %H:%M")}" }
66
+ "This is the end of the chat history"
67
+ end
68
+
69
+ def sl_chat_action(chat_to_select)
70
+ @chat = user.chat_by_title(chat_to_select)
71
+ send_message error_messages[:chat_not_found] if chat.nil?
72
+ user.update(current_chat_id: chat.id) if chat
73
+ send_message success_messages[:chat_selected] if chat
74
+ end
75
+
76
+ def create_chat_action
77
+ title = evnt.message.content.split[1..].join(" ")
78
+ mode = nil
79
+ if title.include? ":"
80
+ mode = title.split(":").last.to_i
81
+ title = title.split(":").first
82
+ end
83
+ actors = AwesomeChatgptActors::CastControl.actors
84
+ return send_message "invalid mode" unless (mode.to_i >= 1 && mode.to_i <= actors.size + 1) || mode.nil?
85
+ return send_message "invalid chat title" if title.nil? || title.empty?
86
+ return send_message "chat title already exists" if user.chat_by_title(title)
87
+
88
+ actor_name = actors[mode.to_i - 1] if mode
89
+ actor = AwesomeChatgptActors::Actor.new(role: actor_name, language: config.language) if actor_name
90
+ chat = Chat.new(user_id: user.id, status: 0, title: title, actor: actor_name, prompt: actor.prompt) if actor
91
+ chat = Chat.new(user_id: user.id, status: 0, title: title) unless actor
92
+ return send_message "Something went wrong", msg.chat.id unless chat
93
+
94
+ chat.save ? chat_created_message(chat) : send_message(error_messages[:chat_creation])
95
+ end
96
+
97
+ def discord_created(chat)
98
+ evnt.respond "Intructions sended to actor:\n#{chat.prompt}" unless chat.actor.nil?
99
+ evnt.respond success_messages[:chat_created] if evnt.present?
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChatgptAssistant
4
+ module Bots
5
+ module Discord
6
+ module Auth
7
+ def discord_visited?(user_id)
8
+ return unless evnt
9
+
10
+ visitor = Visitor.find_by(discord_id: user_id, name: evnt.user.name)
11
+ if visitor.nil?
12
+ Visitor.create(discord_id: user_id, name: evnt.user.name)
13
+ else
14
+ visitor
15
+ end
16
+ end
17
+
18
+ def discord_user_auth(email, password, discord_id)
19
+ user = find_user(email: email)
20
+ return "user not found" unless user
21
+ return "wrong passwords" if password.nil?
22
+
23
+ user.valid_password?(password) ? discord_user_access(discord_id, user.email) : "wrong password"
24
+ end
25
+
26
+ def discord_user_access(discord_id, user_email)
27
+ other_access = where_user(discord_id: discord_id)
28
+ other_access&.each { |access| access.update(discord_id: nil) }
29
+ user = find_user(email: user_email)
30
+ user.update(discord_id: discord_id)
31
+ user.email
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChatgptAssistant
4
+ module Bots
5
+ module Discord
6
+ module Bot
7
+ def bot
8
+ @bot ||= Discordrb::Commands::CommandBot.new(
9
+ token: discord_token,
10
+ client_id: discord_client_id,
11
+ prefix: discord_prefix
12
+ )
13
+ end
14
+
15
+ def user
16
+ @user = find_user(discord_id: evnt.user.id)
17
+ end
18
+
19
+ def bot_init
20
+ at_exit { bot.stop }
21
+ bot.run
22
+ rescue StandardError => e
23
+ Error.create(message: e.message, backtrace: e.backtrace)
24
+ retry
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChatgptAssistant
4
+ module Bots
5
+ module Discord
6
+ module ChatActions
7
+ def ask_action
8
+ @chat = Chat.where(id: user.current_chat_id).last
9
+ send_message error_messages[:chat_not_found] if chat.nil?
10
+ @message = Message.new(chat_id: chat.id, content: message, role: "user") if chat
11
+ (message.save ? answer_action : send_message(error_messages[:message_not_saved])) if chat
12
+ end
13
+
14
+ def private_message_action
15
+ @chat = Chat.where(id: user.current_chat_id).last
16
+ send_message error_messages[:chat_not_found] if chat.nil?
17
+ @message = Message.new(chat_id: chat.id, content: message, role: "user") if chat
18
+ (message.save ? answer_action : send_message(error_messages[:message_not_saved])) if chat
19
+ end
20
+
21
+ def answer_action
22
+ response = chatter.chat(message.content, chat.id, error_messages[:something_went_wrong])
23
+ send_message response
24
+ end
25
+
26
+ def send_message(text)
27
+ messages = parse_message(text, 2000)
28
+ messages.each { |m| evnt.respond m }
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,138 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChatgptAssistant
4
+ module Bots
5
+ module Discord
6
+ module Events
7
+ def start_event
8
+ bot.command :start do |event|
9
+ @evnt = event
10
+ @user = event.user
11
+ start_action
12
+ "Ok"
13
+ end
14
+ end
15
+
16
+ def login_event
17
+ bot.command :login do |event|
18
+ @message = event.message.content.split[1]
19
+ @evnt = event
20
+ message.nil? ? event.respond(common_messages[:login]) : login_action
21
+ "OK"
22
+ end
23
+ end
24
+
25
+ def list_event
26
+ bot.command :list do |event|
27
+ @evnt = event
28
+ list_action if valid_for_list_action?
29
+ "OK"
30
+ end
31
+ end
32
+
33
+ def hist_event
34
+ bot.command :hist do |event|
35
+ @evnt = event
36
+ @chat = user.current_chat
37
+ event.respond error_messages[:chat_not_found] if chat.nil? && user
38
+ hist_action if user && chat
39
+ "OK"
40
+ end
41
+ end
42
+
43
+ def help_event
44
+ bot.command :help do |event|
45
+ @evnt = event
46
+ help_action
47
+ "OK"
48
+ end
49
+ end
50
+
51
+ def new_chat_event
52
+ bot.command :new_chat do |event|
53
+ @evnt = event
54
+ event.respond error_messages[:user_not_logged_in] if user.nil?
55
+ event.respond error_messages[:account_not_verified] if user && !user.active?
56
+ create_chat_action if user&.active?
57
+ "OK"
58
+ end
59
+ end
60
+
61
+ def sl_chat_event
62
+ bot.command :sl_chat do |event|
63
+ @evnt = event
64
+ chat_to_select = event.message.content.split[1..].join(" ")
65
+ event.respond error_messages[:user_not_logged_in] if user.nil?
66
+ event.respond error_messages[:account_not_verified] if user && !user.active?
67
+ sl_chat_action(chat_to_select) if user&.active?
68
+ "OK"
69
+ end
70
+ end
71
+
72
+ def ask_event
73
+ bot.command :ask do |event|
74
+ @evnt = event
75
+ @message = event.message.content.split[1..].join(" ")
76
+ event.respond error_messages[:user_not_logged_in] if user.nil?
77
+ event.respond error_messages[:account_not_verified] if user && !user.active?
78
+ ask_action if user&.active?
79
+ "OK"
80
+ end
81
+ end
82
+
83
+ def voice_connect_event
84
+ bot.command :connect do |event|
85
+ @evnt = event
86
+ if user && !user.active?
87
+ event.respond error_messages[:account_not_verified]
88
+ elsif user&.current_chat_id.nil?
89
+ event.respond error_messages[:no_chat_selected]
90
+ elsif user&.current_chat_id
91
+ @chat = Chat.find(user.current_chat_id)
92
+ voice_connect_checker_action
93
+ voice_connection_checker_action
94
+ bot.voice_connect(event.user.voice_channel)
95
+ else
96
+ event.respond error_messages[:user_not_logged_in]
97
+ end
98
+ "OK"
99
+ end
100
+ end
101
+
102
+ def voice_disconnect_event
103
+ bot.command :disconnect do |event|
104
+ @evnt = event
105
+ disconnect_checker_action
106
+ disconnect_action if user && event.user.voice_channel && event.voice
107
+ "OK"
108
+ end
109
+ end
110
+
111
+ def speak_event
112
+ bot.command :speak do |event|
113
+ @evnt = event
114
+ @message = event.message.content.split[1..].join(" ")
115
+ @chat = user.current_chat
116
+ speak_connect_checker_action
117
+ speak_connection_checker_action
118
+ ask_to_speak_action if user && event.user.voice_channel && event.voice && !chat.nil?
119
+ "OK"
120
+ end
121
+ end
122
+
123
+ def private_message_event
124
+ bot.message do |event|
125
+ @evnt = event
126
+ @visitor = discord_visited?(@evnt.user.id)
127
+ next if discord_next_action?
128
+
129
+ @message = event.message.content
130
+ @chat = user.current_chat if user
131
+ private_message_action if user && !chat.nil?
132
+ "OK"
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChatgptAssistant
4
+ module Bots
5
+ module Discord
6
+ module Validations
7
+ def discord_next_action?
8
+ return true if evnt.channel.type != 1 && evnt.channel.name != "ai-spaces"
9
+
10
+ %w[login register start help new_chat sl_chat ask
11
+ list hist connect disconnect speak].each do |action|
12
+ return true if evnt.message.content.include?("#{discord_prefix}#{action}")
13
+ end
14
+ false
15
+ end
16
+
17
+ def discord_voice_bot_disconnected?
18
+ user && evnt.user.voice_channel && !evnt.voice && !chat.nil?
19
+ end
20
+
21
+ def discord_voice_bot_connected?
22
+ user && evnt.user.voice_channel && evnt.voice && !chat.nil?
23
+ end
24
+
25
+ def visitor_user?
26
+ visitor&.dis_user.nil?
27
+ end
28
+
29
+ def valid_for_list_action?
30
+ evnt.respond(error_messages[:user_not_logged_in]) if user.nil?
31
+ evnt.respond(error_messages[:account_not_verified]) unless user.active?
32
+ evnt.respond(error_messages[:chat_not_found]) if user.chats.count.zero? && user.active?
33
+ !user.nil? && user.active? && user.chats.count.positive?
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChatgptAssistant
4
+ module Bots
5
+ module Discord
6
+ module VoiceActions
7
+ def ask_to_speak_action
8
+ Message.create(chat_id: chat.id, content: message, role: "user", discord_message_id: evnt.message.id)
9
+ response = chatter.chat(message, chat.id, error_messages[:something_went_wrong])
10
+ audio_path = synthesis.synthesize_text(response)
11
+ speak_answer_action(audio_path, response)
12
+ end
13
+
14
+ def speak_answer_action(audio_path, response)
15
+ send_message response
16
+ evnt.voice.play_file(audio_path)
17
+ delete_file audio_path
18
+ "OK"
19
+ end
20
+
21
+ def discord_voice_bot_connect
22
+ bot.voice_connect(evnt.user.voice_channel) if discord_voice_bot_disconnected?
23
+ discord_voice_bot_connected? ? "Connected to voice channel" : "Error connecting to voice channel"
24
+ end
25
+
26
+ def disconnect_action
27
+ bot.voice_destroy(evnt.user.voice_channel)
28
+ "Disconnected from voice channel"
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end