chatgpt_assistant 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.env_prod_sample +1 -1
- data/.env_sample +1 -1
- data/.rubocop.yml +16 -5
- data/Dockerfile +1 -4
- data/README.md +14 -11
- data/exe/chatgpt_assistant +2 -2
- data/lib/bots/application_bot.rb +22 -91
- data/lib/bots/discord_bot.rb +102 -241
- data/lib/bots/helpers/audio_helper.rb +18 -0
- data/lib/bots/helpers/authentication_helper.rb +47 -0
- data/lib/bots/helpers/discord_helper.rb +102 -0
- data/lib/bots/helpers/discord_voice_helper.rb +50 -0
- data/lib/bots/helpers/file_helper.rb +10 -0
- data/lib/bots/helpers/logger_action_helper.rb +14 -0
- data/lib/bots/helpers/messenger_helper.rb +132 -0
- data/lib/bots/helpers/search_helper.rb +34 -0
- data/lib/bots/helpers/telegram_helper.rb +89 -0
- data/lib/bots/helpers/validation_helper.rb +29 -0
- data/lib/bots/helpers/visit_helper.rb +24 -0
- data/lib/bots/telegram_bot.rb +120 -235
- data/lib/chatgpt_assistant/audio_recognition.rb +33 -32
- data/lib/chatgpt_assistant/audio_synthesis.rb +80 -80
- data/lib/chatgpt_assistant/chatter.rb +34 -27
- data/lib/chatgpt_assistant/config.rb +5 -1
- data/lib/chatgpt_assistant/default_messages.rb +108 -106
- data/lib/chatgpt_assistant/migrations.rb +62 -4
- data/lib/chatgpt_assistant/models.rb +56 -8
- data/lib/chatgpt_assistant/version.rb +1 -1
- data/lib/chatgpt_assistant.rb +25 -13
- data/workflows/deploy.yml +19 -0
- data/workflows/deploy_and_build.yml +19 -0
- metadata +20 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c735d019dd75e4679918ae36b75d61978f78cf4b7ca11c8530915cc738cb7ec6
|
4
|
+
data.tar.gz: 73bdda5095040e37e3bb270c287ec8549fe156749a35b28ecca592c5205b17c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ebf7e87f62c820eb5988dfe6a0635d385a2329bdb7b690e5ee15bac182ab162f8e502b111d14fb4c66aa6923a702e57b7f8ae5fef351582ff5b1a9a1bd7bf8f
|
7
|
+
data.tar.gz: 78367d4df7a05b0290889fb854a925abd239b277e8fe9f7236db9c6715405c989f40ed0d732c0db05e3abd6e63c70099c96317fe5f3c13213f53aa26d5cf84f8
|
data/.env_prod_sample
CHANGED
@@ -3,7 +3,7 @@ LANGUAGE=en # or pt currently
|
|
3
3
|
MODE=aws # or ibm
|
4
4
|
DISCORD_PREFIX=gpt!
|
5
5
|
|
6
|
-
POSTGRES_DB=
|
6
|
+
POSTGRES_DB=chatgpt_assistant_production # or your database
|
7
7
|
POSTGRES_USER=postgres # or your user
|
8
8
|
POSTGRES_PASSWORD=postgres # or your password
|
9
9
|
|
data/.env_sample
CHANGED
@@ -3,7 +3,7 @@ LANGUAGE=en # or pt currently
|
|
3
3
|
MODE=aws # or ibm
|
4
4
|
DISCORD_PREFIX=dgpt!
|
5
5
|
|
6
|
-
POSTGRES_DB=
|
6
|
+
POSTGRES_DB=chatgpt_assistant_development # or your database
|
7
7
|
POSTGRES_USER=postgres # or your user
|
8
8
|
POSTGRES_PASSWORD=postgres # or your password
|
9
9
|
|
data/.rubocop.yml
CHANGED
@@ -12,15 +12,26 @@ Style/StringLiteralsInInterpolation:
|
|
12
12
|
Layout/LineLength:
|
13
13
|
Max: 160
|
14
14
|
|
15
|
-
|
15
|
+
Metrics/ModuleLength:
|
16
|
+
Max: 120
|
17
|
+
|
16
18
|
Metrics/MethodLength:
|
17
19
|
Max: 200
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
+
Metrics/BlockLength:
|
22
|
+
Max: 200
|
21
23
|
|
22
24
|
Metrics/ClassLength:
|
23
|
-
|
25
|
+
Max: 300
|
24
26
|
|
25
27
|
Metrics/AbcSize:
|
26
|
-
Max:
|
28
|
+
Max: 35
|
29
|
+
|
30
|
+
Layout/AccessModifierIndentation:
|
31
|
+
EnforcedStyle: indent
|
32
|
+
|
33
|
+
Layout/IndentationWidth:
|
34
|
+
Width: 2
|
35
|
+
|
36
|
+
Layout/IndentationConsistency:
|
37
|
+
EnforcedStyle: indented_internal_methods
|
data/Dockerfile
CHANGED
@@ -5,11 +5,8 @@ COPY Gemfile /chatgpt_assistant/Gemfile
|
|
5
5
|
COPY Gemfile.lock /chatgpt_assistant/Gemfile.lock
|
6
6
|
RUN bundle install
|
7
7
|
|
8
|
-
# Opus Installation for voice messages
|
9
|
-
|
10
8
|
RUN apt-get install -y wget
|
11
9
|
RUN wget https://archive.mozilla.org/pub/opus/opus-1.3.1.tar.gz
|
12
10
|
RUN tar -xvf opus-1.3.1.tar.gz
|
13
11
|
RUN cd opus-1.3.1 && ./configure && make && make install
|
14
|
-
RUN rm -rf opus-1.3.1.tar.gz opus-1.3.1
|
15
|
-
|
12
|
+
RUN rm -rf opus-1.3.1.tar.gz opus-1.3.1
|
data/README.md
CHANGED
@@ -86,17 +86,20 @@ After starting the Docker Compose services, you can use the features of the gem
|
|
86
86
|
|
87
87
|
## Discord Bot Commands
|
88
88
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
- !
|
93
|
-
- !
|
94
|
-
- !
|
95
|
-
- !
|
96
|
-
- !
|
97
|
-
- !
|
98
|
-
- !
|
99
|
-
- !
|
89
|
+
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
|
100
103
|
|
101
104
|
## Telegram Bot Commands
|
102
105
|
|
data/exe/chatgpt_assistant
CHANGED
@@ -31,7 +31,7 @@ end
|
|
31
31
|
end
|
32
32
|
|
33
33
|
%w[deploy_and_build.sh deploy.sh].each do |file|
|
34
|
-
`cp #{gem_dir}
|
34
|
+
`cp #{gem_dir}/#{file} #{path}`
|
35
35
|
end
|
36
36
|
|
37
37
|
%w[/.bundle/ /.yardoc /_yardoc/ /coverage/ /doc/ /pkg/ /spec/reports/ /tmp/ /db_data /db_data/*
|
@@ -42,7 +42,7 @@ end
|
|
42
42
|
`cd #{path} && git init`
|
43
43
|
|
44
44
|
`mkdir -p #{path}/.github/workflows`
|
45
|
-
`cp #{gem_dir}
|
45
|
+
`cp #{gem_dir}/workflows/*.yml #{path}/.github/workflows`
|
46
46
|
|
47
47
|
puts "Done! Now you can run 'cd #{path} && bundle install'"
|
48
48
|
puts "Build the docker image with 'rake compose:build'"
|
data/lib/bots/application_bot.rb
CHANGED
@@ -1,8 +1,25 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative "helpers/messenger_helper"
|
4
|
+
require_relative "helpers/authentication_helper"
|
5
|
+
require_relative "helpers/visit_helper"
|
6
|
+
require_relative "helpers/audio_helper"
|
7
|
+
require_relative "helpers/logger_action_helper"
|
8
|
+
require_relative "helpers/search_helper"
|
9
|
+
require_relative "helpers/validation_helper"
|
10
|
+
require_relative "helpers/file_helper"
|
3
11
|
module ChatgptAssistant
|
4
12
|
# This class is responsible to contain the shared variables between the bot classes
|
5
13
|
class ApplicationBot
|
14
|
+
include MessengerHelper
|
15
|
+
include AuthenticationHelper
|
16
|
+
include VisitHelper
|
17
|
+
include AudioHelper
|
18
|
+
include LoggerActionHelper
|
19
|
+
include SearchHelper
|
20
|
+
include ValidationHelper
|
21
|
+
include FileHelper
|
22
|
+
|
6
23
|
def initialize(config)
|
7
24
|
@config = config
|
8
25
|
default_msg = DefaultMessages.new(@config.language)
|
@@ -19,100 +36,14 @@ module ChatgptAssistant
|
|
19
36
|
@help_messages = default_msg.help_messages
|
20
37
|
end
|
21
38
|
|
22
|
-
def chatter
|
23
|
-
@chatter ||= Chatter.new(openai_api_key)
|
24
|
-
end
|
25
|
-
|
26
|
-
def audio_recognition
|
27
|
-
@audio_recognition ||= AudioRecognition.new(openai_api_key)
|
28
|
-
end
|
29
|
-
|
30
|
-
def audio_synthesis
|
31
|
-
@audio_synthesis ||= AudioSynthesis.new(config)
|
32
|
-
end
|
33
|
-
|
34
|
-
def transcribed_text
|
35
|
-
audio_recognition.transcribe_audio(audio_url)
|
36
|
-
end
|
37
|
-
|
38
|
-
def find_useremail(email)
|
39
|
-
User.find_by(email: email)
|
40
|
-
end
|
41
|
-
|
42
|
-
def find_userdiscord(discord_id)
|
43
|
-
User.find_by(discord_id: discord_id)
|
44
|
-
end
|
45
|
-
|
46
|
-
def find_usertelegram(telegram_id)
|
47
|
-
User.find_by(telegram_id: telegram_id)
|
48
|
-
end
|
49
|
-
|
50
|
-
def valid_password?(user, password)
|
51
|
-
return false if password.nil?
|
52
|
-
|
53
|
-
salt = user.password_salt
|
54
|
-
password_hash = user.password_hash
|
55
|
-
|
56
|
-
BCrypt::Engine.hash_secret(password, salt) == password_hash
|
57
|
-
end
|
58
|
-
|
59
|
-
def auth_userdiscord(email, password, discord_id)
|
60
|
-
user = find_useremail(email)
|
61
|
-
return "user not found" unless user
|
62
|
-
return "wrong password" if password.nil?
|
63
|
-
|
64
|
-
valid_password?(user, password) ? user_disc_access(discord_id, user.email) : "wrong password"
|
65
|
-
end
|
66
|
-
|
67
|
-
def user_disc_access(discord_id, user_email)
|
68
|
-
last_access = find_userdiscord(discord_id)
|
69
|
-
new_access = find_useremail(user_email)
|
70
|
-
last_acess.update(discord_id: nil) if last_access && (last_access != new_access)
|
71
|
-
new_access.update(discord_id: discord_id)
|
72
|
-
new_access
|
73
|
-
end
|
74
|
-
|
75
|
-
def discord_user_create(discord_id, email, password, name)
|
76
|
-
user = User.new(discord_id: discord_id, email: email, password_hash: password, name: name)
|
77
|
-
last_access = find_userdiscord(discord_id)
|
78
|
-
last_access&.update(discord_id: nil)
|
79
|
-
user.save
|
80
|
-
end
|
81
|
-
|
82
|
-
def auth_usertelegram(email, password, telegram_id)
|
83
|
-
user = find_useremail(email)
|
84
|
-
return "user not found" unless user
|
85
|
-
return "wrong password" if password.nil?
|
86
|
-
|
87
|
-
valid_password?(user, password) ? user_tele_access(telegram_id, user.email) : "wrong password"
|
88
|
-
end
|
89
|
-
|
90
|
-
def user_tele_access(telegram_id, user_email)
|
91
|
-
last_access = find_usertelegram(telegram_id)
|
92
|
-
new_access = find_useremail(user_email)
|
93
|
-
last_acess.update(telegram_id: nil) if last_access && (last_access != new_access)
|
94
|
-
new_access.update(telegram_id: telegram_id)
|
95
|
-
new_access
|
96
|
-
end
|
97
|
-
|
98
|
-
def telegram_user_create(telegram_id, email, password, name)
|
99
|
-
user = User.new(telegram_id: telegram_id, email: email, password_hash: password, name: name)
|
100
|
-
last_access = find_usertelegram(telegram_id)
|
101
|
-
last_access&.update(telegram_id: nil)
|
102
|
-
user.save
|
103
|
-
end
|
104
|
-
|
105
|
-
def delete_all_voice_files
|
106
|
-
Dir.glob("voice/*").each do |file|
|
107
|
-
next if [".keep", "voice/.keep"].include?(file)
|
108
|
-
|
109
|
-
File.delete(file)
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
39
|
attr_reader :openai_api_key, :telegram_token, :database, :default_msg,
|
114
40
|
:mode, :config, :discord_token, :discord_client_id,
|
115
41
|
:discord_prefix, :commom_messages, :error_messages, :success_messages,
|
116
42
|
:help_messages
|
43
|
+
attr_accessor :msg, :evnt, :bot, :audio_url, :visitor, :user, :chat, :chat_id
|
44
|
+
|
45
|
+
def chatter
|
46
|
+
@chatter ||= Chatter.new(openai_api_key)
|
47
|
+
end
|
117
48
|
end
|
118
49
|
end
|
data/lib/bots/discord_bot.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative "helpers/discord_helper"
|
4
|
+
require_relative "helpers/discord_voice_helper"
|
5
|
+
|
3
6
|
module ChatgptAssistant
|
4
7
|
# This class is responsible to handle the discord bot
|
5
8
|
class DiscordBot < ApplicationBot
|
@@ -21,274 +24,132 @@ module ChatgptAssistant
|
|
21
24
|
|
22
25
|
private
|
23
26
|
|
24
|
-
|
25
|
-
|
27
|
+
include DiscordHelper
|
28
|
+
include DiscordVoiceHelper
|
26
29
|
|
27
|
-
|
28
|
-
|
29
|
-
token: discord_token,
|
30
|
-
client_id: discord_client_id,
|
31
|
-
prefix: discord_prefix
|
32
|
-
)
|
33
|
-
end
|
30
|
+
attr_reader :message
|
31
|
+
attr_accessor :evnt, :user, :chats, :chat
|
34
32
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
33
|
+
def start_event
|
34
|
+
bot.command :start do |event|
|
35
|
+
@evnt = event
|
36
|
+
@user = event.user
|
37
|
+
start_action
|
38
|
+
end
|
40
39
|
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def start_action
|
44
|
-
evnt.respond commom_messages[:start_helper].gsub("register/", "gpt!register ")
|
45
|
-
evnt.respond commom_messages[:start_sec_helper].gsub("login/", "gpt!login ")
|
46
|
-
end
|
47
40
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
41
|
+
def login_event
|
42
|
+
bot.command :login do |event|
|
43
|
+
@message = event.message.content.split(" ")[1]
|
44
|
+
@evnt = event
|
45
|
+
message.nil? ? event.respond(commom_messages[:login]) : login_action
|
46
|
+
end
|
53
47
|
end
|
54
|
-
end
|
55
48
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
when "wrong password"
|
63
|
-
evnt.respond error_messages[:wrong_password]
|
64
|
-
when find_useremail(user_email)
|
65
|
-
evnt.respond success_messages[:user_logged_in]
|
49
|
+
def register_event
|
50
|
+
bot.command :register do |event|
|
51
|
+
@message = event.message.content.split(" ")[1]
|
52
|
+
@evnt = event
|
53
|
+
message.nil? ? event.respond(commom_messages[:register]) : register_action
|
54
|
+
end
|
66
55
|
end
|
67
|
-
end
|
68
56
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
57
|
+
def list_event
|
58
|
+
bot.command :list do |event|
|
59
|
+
@evnt = event
|
60
|
+
@user = find_user(discord_id: event.user.id)
|
61
|
+
event.respond error_messages[:user_not_logged_in] if user.nil?
|
62
|
+
event.respond error_messages[:chat_not_found] if user.chats.empty? && user
|
63
|
+
list_action if user && !user.chats.empty?
|
64
|
+
end
|
74
65
|
end
|
75
|
-
end
|
76
|
-
|
77
|
-
def register_action
|
78
|
-
user_email = message.split(":")[0]
|
79
|
-
user_password = message.split(":")[1]
|
80
|
-
find_useremail(user_email).nil? ? create_user_action(user_email, user_password) : evnt.respond(error_messages[:user_already_exists])
|
81
|
-
end
|
82
|
-
|
83
|
-
def create_user_action(mail, pass)
|
84
|
-
id = evnt.user.id
|
85
|
-
name = evnt.user.username
|
86
|
-
discord_user_create(id, mail, pass, name) ? evnt.respond(success_messages[:user_created]) : evnt.respond(error_messages[:user_not_created])
|
87
|
-
end
|
88
|
-
|
89
|
-
def list_event
|
90
|
-
bot.command :list do |event|
|
91
|
-
@evnt = event
|
92
|
-
@user = find_userdiscord(event.user.id)
|
93
|
-
event.respond error_messages[:user_not_logged_in] if user.nil?
|
94
|
-
event.respond error_messages[:chat_not_found] if user.chats.empty? && user
|
95
|
-
list_action if user && !user.chats.empty?
|
96
|
-
end
|
97
|
-
end
|
98
66
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
@user = find_userdiscord(event.user.id)
|
109
|
-
event.respond error_messages[:user_not_logged_in] if user.nil?
|
110
|
-
title = event.message.content.split(" ")[1..].join(" ")
|
111
|
-
@chat = user.chat_by_title(title)
|
112
|
-
event.respond error_messages[:chat_not_found] if chat.nil? && user
|
113
|
-
hist_action if user && chat
|
67
|
+
def hist_event
|
68
|
+
bot.command :hist do |event|
|
69
|
+
@evnt = event
|
70
|
+
event.respond error_messages[:user_not_logged_in] if user.nil?
|
71
|
+
title = event.message.content.split(" ")[1..].join(" ")
|
72
|
+
@chat = user.chat_by_title(title)
|
73
|
+
event.respond error_messages[:chat_not_found] if chat.nil? && user
|
74
|
+
hist_action if user && chat
|
75
|
+
end
|
114
76
|
end
|
115
|
-
end
|
116
77
|
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
78
|
+
def help_event
|
79
|
+
bot.command :help do |event|
|
80
|
+
@evnt = event
|
81
|
+
help_action
|
82
|
+
end
|
121
83
|
end
|
122
|
-
"This is the end of the chat history"
|
123
|
-
end
|
124
84
|
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
85
|
+
def new_chat_event
|
86
|
+
bot.command :new_chat do |event|
|
87
|
+
@evnt = event
|
88
|
+
@user = find_user(discord_id: event.user.id)
|
89
|
+
event.respond error_messages[:user_not_logged_in] if user.nil?
|
90
|
+
create_chat_action if user
|
91
|
+
end
|
129
92
|
end
|
130
|
-
end
|
131
93
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
evnt.respond message
|
139
|
-
end
|
94
|
+
def sl_chat_event
|
95
|
+
bot.command :sl_chat do |event|
|
96
|
+
@evnt = event
|
97
|
+
chat_to_select = event.message.content.split(" ")[1..].join(" ")
|
98
|
+
@user = find_user(discord_id: event.user.id)
|
99
|
+
event.respond error_messages[:user_not_logged_in] if user.nil?
|
140
100
|
|
141
|
-
|
142
|
-
|
143
|
-
@evnt = event
|
144
|
-
@user = find_userdiscord(event.user.id)
|
145
|
-
event.respond error_messages[:user_not_logged_in] if user.nil?
|
146
|
-
create_chat_action if user
|
101
|
+
sl_chat_action(chat_to_select) if user
|
102
|
+
end
|
147
103
|
end
|
148
|
-
end
|
149
104
|
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
evnt.respond success_messages[:chat_created]
|
159
|
-
end
|
160
|
-
|
161
|
-
def sl_chat_event
|
162
|
-
bot.command :sl_chat do |event|
|
163
|
-
@evnt = event
|
164
|
-
chat_to_select = event.message.content.split(" ")[1..].join(" ")
|
165
|
-
@user = find_userdiscord(event.user.id)
|
166
|
-
event.respond error_messages[:user_not_logged_in] if user.nil?
|
167
|
-
|
168
|
-
sl_chat_action(chat_to_select) if user
|
105
|
+
def ask_event
|
106
|
+
bot.command :ask do |event|
|
107
|
+
@evnt = event
|
108
|
+
@message = event.message.content.split(" ")[1..].join(" ")
|
109
|
+
@user = find_user(discord_id: event.user.id)
|
110
|
+
event.respond error_messages[:user_not_logged_in] if user.nil?
|
111
|
+
ask_action if user
|
112
|
+
end
|
169
113
|
end
|
170
|
-
end
|
171
|
-
|
172
|
-
def sl_chat_action(chat_to_select)
|
173
|
-
@chat = user.chat_by_title(chat_to_select)
|
174
|
-
evnt.respond error_messages[:chat_not_found] if chat.nil?
|
175
|
-
user.update(current_chat_id: chat.id) if chat
|
176
|
-
evnt.respond success_messages[:chat_selected] if chat
|
177
|
-
end
|
178
114
|
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
115
|
+
def voice_connect_event
|
116
|
+
bot.command :connect do |event|
|
117
|
+
@evnt = event
|
118
|
+
@user = find_user(discord_id: event.user.id)
|
119
|
+
@chat = Chat.where(id: user.current_chat_id).last
|
120
|
+
voice_connect_checker_action
|
121
|
+
voice_connection_checker_action
|
122
|
+
discord_voice_bot_connect
|
123
|
+
end
|
186
124
|
end
|
187
|
-
end
|
188
|
-
|
189
|
-
def ask_action
|
190
|
-
@chat = Chat.where(id: user.current_chat_id).last
|
191
|
-
evnt.respond error_messages[:chat_not_found] if chat.nil?
|
192
|
-
@message = Message.new(chat_id: chat.id, content: message, role: "user") if chat
|
193
|
-
(message.save ? answer_action : evnt.respond(error_messages[:message_not_saved])) if chat
|
194
|
-
end
|
195
|
-
|
196
|
-
def answer_action
|
197
|
-
response = chatter.chat(message.content, chat.id)
|
198
|
-
evnt.respond response
|
199
|
-
end
|
200
125
|
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
bot.voice_connect(event.user.voice_channel) if bot_disconnected?
|
209
|
-
bot_connected? ? "Connected to voice channel" : "Error connecting to voice channel"
|
126
|
+
def disconnect_event
|
127
|
+
bot.command :disconnect do |event|
|
128
|
+
@evnt = event
|
129
|
+
@user = find_user(discord_id: event.user.id)
|
130
|
+
disconnect_checker_action
|
131
|
+
disconnect_action if user && event.user.voice_channel && event.voice
|
132
|
+
end
|
210
133
|
end
|
211
|
-
end
|
212
|
-
|
213
|
-
def voice_connect_checker_action
|
214
|
-
evnt.respond error_messages[:user_not_logged_in] if user.nil?
|
215
|
-
evnt.respond error_messages[:chat_not_found] if user && chat.nil?
|
216
|
-
end
|
217
134
|
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
user && evnt.user.voice_channel && evnt.voice && !chat.nil?
|
229
|
-
end
|
230
|
-
|
231
|
-
def disconnect_event
|
232
|
-
bot.command :disconnect do |event|
|
233
|
-
@evnt = event
|
234
|
-
@user = find_userdiscord(event.user.id)
|
235
|
-
disconnect_checker_action
|
236
|
-
disconnect_action if user && event.user.voice_channel && event.voice
|
135
|
+
def speak_event
|
136
|
+
bot.command :speak do |event|
|
137
|
+
@evnt = event
|
138
|
+
@message = event.message.content.split(" ")[1..].join(" ")
|
139
|
+
@user = find_user(discord_id: event.user.id)
|
140
|
+
@chat = user.current_chat
|
141
|
+
speak_connect_checker_action
|
142
|
+
speak_connection_checker_action
|
143
|
+
ask_to_speak_action if user && event.user.voice_channel && event.voice && !chat.nil?
|
144
|
+
end
|
237
145
|
end
|
238
|
-
end
|
239
146
|
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
def disconnect_action
|
247
|
-
bot.voice_destroy(event.user.voice_channel)
|
248
|
-
"Disconnected from voice channel"
|
249
|
-
end
|
250
|
-
|
251
|
-
def speak_event
|
252
|
-
bot.command :speak do |event|
|
253
|
-
@evnt = event
|
254
|
-
@message = event.message.content.split(" ")[1..].join(" ")
|
255
|
-
@user = find_userdiscord(event.user.id)
|
256
|
-
@chat = user.current_chat
|
257
|
-
speak_connect_checker_action
|
258
|
-
speak_connection_checker_action
|
259
|
-
ask_to_speak_action if user && event.user.voice_channel && event.voice && !chat.nil?
|
147
|
+
def bot_init
|
148
|
+
at_exit { bot.stop }
|
149
|
+
bot.run
|
150
|
+
rescue StandardError => e
|
151
|
+
Error.create(message: e.message, backtrace: e.backtrace)
|
152
|
+
retry
|
260
153
|
end
|
261
|
-
end
|
262
|
-
|
263
|
-
def speak_connect_checker_action
|
264
|
-
evnt.respond error_messages[:user_not_logged_in] if user.nil?
|
265
|
-
evnt.respond error_messages[:chat_not_found] if user && event.user.voice_channel && event.voice && chat.nil?
|
266
|
-
end
|
267
|
-
|
268
|
-
def speak_connection_checker_action
|
269
|
-
evnt.respond error_messages[:user_not_in_voice_channel] if event.user.voice_channel.nil? && user
|
270
|
-
evnt.respond error_messages[:bot_not_in_voice_channel] if !event.voice && user
|
271
|
-
end
|
272
|
-
|
273
|
-
def ask_to_speak_action
|
274
|
-
Message.create(chat_id: chat.id, content: message, role: "user")
|
275
|
-
response = chatter.chat(message, chat.id)
|
276
|
-
audio_path = audio_synthesis.synthesize_text(response)
|
277
|
-
speak_answer_action(audio_path, response)
|
278
|
-
end
|
279
|
-
|
280
|
-
def speak_answer_action(audio_path, response)
|
281
|
-
evnt.respond response
|
282
|
-
evnt.voice.play_file(audio_path)
|
283
|
-
delete_all_voice_files
|
284
|
-
"OK"
|
285
|
-
end
|
286
|
-
|
287
|
-
def bot_init
|
288
|
-
at_exit { bot.stop }
|
289
|
-
bot.run
|
290
|
-
rescue StandardError
|
291
|
-
start
|
292
|
-
end
|
293
154
|
end
|
294
155
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ChatgptAssistant
|
4
|
+
# Helper for audio
|
5
|
+
module AudioHelper
|
6
|
+
def recognition
|
7
|
+
@recognition ||= AudioRecognition.new(openai_api_key)
|
8
|
+
end
|
9
|
+
|
10
|
+
def synthesis
|
11
|
+
@synthesis ||= AudioSynthesis.new(config)
|
12
|
+
end
|
13
|
+
|
14
|
+
def transcribe_file(url)
|
15
|
+
recognition.transcribe_audio(url)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|