chatgpt_assistant 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.env_sample +16 -7
- data/.rubocop.yml +14 -3
- data/Dockerfile +11 -5
- data/Gemfile +25 -23
- data/Gemfile.lock +123 -85
- data/README.md +15 -18
- data/Rakefile +1 -0
- data/docker-compose.yml +8 -4
- data/exe/chatgpt_assistant +1 -1
- data/lib/{bots → chatgpt_assistant/bots}/application_bot.rb +0 -6
- data/lib/chatgpt_assistant/bots/discord/actions.rb +104 -0
- data/lib/chatgpt_assistant/bots/discord/auth.rb +36 -0
- data/lib/chatgpt_assistant/bots/discord/bot.rb +29 -0
- data/lib/chatgpt_assistant/bots/discord/chat_actions.rb +33 -0
- data/lib/chatgpt_assistant/bots/discord/events.rb +138 -0
- data/lib/chatgpt_assistant/bots/discord/validations.rb +38 -0
- data/lib/chatgpt_assistant/bots/discord/voice_actions.rb +33 -0
- data/lib/chatgpt_assistant/bots/discord/voice_checkers.rb +35 -0
- data/lib/chatgpt_assistant/bots/discord_bot.rb +42 -0
- data/lib/chatgpt_assistant/bots/helpers/messenger_helper.rb +16 -0
- data/lib/{bots → chatgpt_assistant/bots}/jobs/register_job.rb +2 -2
- data/lib/chatgpt_assistant/bots/mailers/account_mailer.rb +30 -0
- data/lib/chatgpt_assistant/bots/mailers/application_mailer.rb +21 -0
- data/lib/chatgpt_assistant/bots/services/new_chat_service.rb +59 -0
- data/lib/chatgpt_assistant/bots/services/register_service.rb +45 -0
- data/lib/chatgpt_assistant/bots/telegram/actions.rb +54 -0
- data/lib/chatgpt_assistant/bots/telegram/auth.rb +40 -0
- data/lib/chatgpt_assistant/bots/telegram/bot.rb +17 -0
- data/lib/chatgpt_assistant/bots/telegram/chat_actions.rb +30 -0
- data/lib/chatgpt_assistant/bots/telegram/events.rb +120 -0
- data/lib/chatgpt_assistant/bots/telegram/events_controller.rb +48 -0
- data/lib/chatgpt_assistant/bots/telegram/permissions.rb +48 -0
- data/lib/chatgpt_assistant/bots/telegram/validations.rb +55 -0
- data/lib/chatgpt_assistant/bots/telegram/voice_actions.rb +36 -0
- data/lib/chatgpt_assistant/bots/telegram_bot.rb +44 -0
- data/lib/chatgpt_assistant/chatter.rb +10 -2
- data/lib/chatgpt_assistant/config.rb +4 -1
- data/lib/chatgpt_assistant/default_messages.rb +10 -4
- data/lib/chatgpt_assistant/error.rb +9 -0
- data/lib/chatgpt_assistant/migrations.rb +5 -0
- data/lib/chatgpt_assistant/models.rb +20 -0
- data/lib/chatgpt_assistant/version.rb +1 -1
- data/lib/chatgpt_assistant.rb +5 -3
- metadata +34 -24
- data/.env_prod_sample +0 -18
- data/docker-compose.prod.yml +0 -34
- data/lib/bots/discord_bot.rb +0 -182
- data/lib/bots/helpers/authentication_helper.rb +0 -48
- data/lib/bots/helpers/discord_helper.rb +0 -124
- data/lib/bots/helpers/discord_voice_helper.rb +0 -50
- data/lib/bots/helpers/messenger_helper.rb +0 -133
- data/lib/bots/helpers/telegram_helper.rb +0 -73
- data/lib/bots/helpers/telegram_voice_helper.rb +0 -33
- data/lib/bots/helpers/validation_helper.rb +0 -38
- data/lib/bots/helpers/visit_helper.rb +0 -28
- data/lib/bots/services/new_chat_service.rb +0 -34
- data/lib/bots/services/register_service.rb +0 -36
- data/lib/bots/telegram_bot.rb +0 -180
- /data/lib/{bots → chatgpt_assistant/bots}/helpers/audio_helper.rb +0 -0
- /data/lib/{bots → chatgpt_assistant/bots}/helpers/file_helper.rb +0 -0
- /data/lib/{bots → chatgpt_assistant/bots}/helpers/search_helper.rb +0 -0
- /data/lib/{bots → chatgpt_assistant/bots}/jobs/new_chat_job.rb +0 -0
- /data/lib/{bots → chatgpt_assistant/bots}/jobs/voice_connect_job.rb +0 -0
- /data/lib/{bots → chatgpt_assistant/bots}/services/voice_connect_service.rb +0 -0
@@ -43,7 +43,8 @@ module ChatgptAssistant
|
|
43
43
|
chat_selected: "Chat selecionado com sucesso!",
|
44
44
|
user_logged_in: "Login realizado com sucesso!",
|
45
45
|
user_logged_out: "Logout realizado com sucesso!",
|
46
|
-
voice_channel_connected: "O bot entrou no canal de voz com sucesso!"
|
46
|
+
voice_channel_connected: "O bot entrou no canal de voz com sucesso!",
|
47
|
+
account_confirmed: "Conta confirmada com sucesso!"
|
47
48
|
}
|
48
49
|
end
|
49
50
|
|
@@ -77,7 +78,9 @@ module ChatgptAssistant
|
|
77
78
|
text_length_too_long: "O texto de resposta é muito longo. Tente diminuir a quantidade de respostas na mesma mensagem.",
|
78
79
|
|
79
80
|
invalid_command: "Comando inválido. Tente novamente.",
|
80
|
-
something_went_wrong: "Algo deu errado. Tente novamente mais tarde."
|
81
|
+
something_went_wrong: "Algo deu errado. Tente novamente mais tarde.",
|
82
|
+
account_not_verified: "Sua conta não foi verificada. Verifique sua caixa de email.",
|
83
|
+
account_not_confirmed: "Sua conta não foi confirmada. Verifique sua caixa de email."
|
81
84
|
}
|
82
85
|
end
|
83
86
|
|
@@ -127,7 +130,8 @@ module ChatgptAssistant
|
|
127
130
|
chat_selected: "Chat selected successfully!",
|
128
131
|
user_logged_in: "Login successfully!",
|
129
132
|
user_logged_out: "Logout successfully!",
|
130
|
-
voice_channel_connected: "The bot entered the voice channel successfully!"
|
133
|
+
voice_channel_connected: "The bot entered the voice channel successfully!",
|
134
|
+
account_confirmed: "Your account has been verified!"
|
131
135
|
}
|
132
136
|
end
|
133
137
|
|
@@ -161,7 +165,9 @@ module ChatgptAssistant
|
|
161
165
|
text_length_too_long: "The response text is too long. Try to reduce the number of responses in the same message.",
|
162
166
|
|
163
167
|
invalid_command: "Invalid command. Try again.",
|
164
|
-
something_went_wrong: "Something went wrong. Try again later."
|
168
|
+
something_went_wrong: "Something went wrong. Try again later.",
|
169
|
+
account_not_verified: "Your account is not verified. Please check your email and verify your account.",
|
170
|
+
account_not_confirmed: "Something went wrong. Please check your email and verify your account. Maybe you pass the wrong code."
|
165
171
|
}
|
166
172
|
end
|
167
173
|
|
@@ -208,6 +208,15 @@ module ChatgptAssistant
|
|
208
208
|
include LoadError
|
209
209
|
end
|
210
210
|
|
211
|
+
# Account Not Verified Error
|
212
|
+
class AccountNotVerifiedError < StandardError
|
213
|
+
def initialize(message = load_error_context[:account_not_verified])
|
214
|
+
super(message)
|
215
|
+
end
|
216
|
+
|
217
|
+
include LoadError
|
218
|
+
end
|
219
|
+
|
211
220
|
# Something Went Wrong Error
|
212
221
|
class SomethingWentWrongError < StandardError
|
213
222
|
def initialize(message = load_error_context[:something_went_wrong])
|
@@ -27,15 +27,20 @@ class UserMigration < ActiveRecord::Migration[5.2]
|
|
27
27
|
create_table :users do |t|
|
28
28
|
t.string :telegram_id, foreign_key: true, class_name: "Visitor"
|
29
29
|
t.string :discord_id, foreign_key: true, class_name: "Visitor"
|
30
|
+
t.string :name, limit: 100
|
30
31
|
t.string :email, null: false, limit: 100
|
32
|
+
t.string :phone, limit: 100
|
31
33
|
t.string :password_hash, null: false, limit: 100
|
32
34
|
t.string :password_salt, null: false, limit: 100
|
35
|
+
t.string :token, null: false, limit: 100, default: ""
|
36
|
+
t.string :openai_token, null: false, limit: 100, default: ""
|
33
37
|
t.integer :current_chat_id, null: false, default: 0
|
34
38
|
t.integer :role, null: false, default: 0
|
35
39
|
t.integer :open_chats, null: false, default: 0
|
36
40
|
t.integer :closed_chats, null: false, default: 0
|
37
41
|
t.integer :total_chats, null: false, default: 0
|
38
42
|
t.integer :total_messages, null: false, default: 0
|
43
|
+
t.boolean :active, null: false, default: false
|
39
44
|
t.timestamps
|
40
45
|
end
|
41
46
|
end
|
@@ -44,6 +44,26 @@ class User < ActiveRecord::Base
|
|
44
44
|
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
|
45
45
|
end
|
46
46
|
|
47
|
+
def encrypt_token
|
48
|
+
confirmation = { email: email, password_hash: password_hash, time: created_at }
|
49
|
+
BCrypt::Engine.hash_secret(JSON.parse(confirmation.to_json), password_salt)
|
50
|
+
end
|
51
|
+
|
52
|
+
def valid_password?(password)
|
53
|
+
BCrypt::Engine.hash_secret(password, password_salt) == password_hash
|
54
|
+
end
|
55
|
+
|
56
|
+
def confirm_account(hash)
|
57
|
+
confirmation = { email: email, password_hash: password_hash, time: created_at }
|
58
|
+
secret = BCrypt::Engine.hash_secret(JSON.parse(confirmation.to_json), password_salt)
|
59
|
+
return false unless secret == hash
|
60
|
+
|
61
|
+
self.token = secret
|
62
|
+
self.active = true
|
63
|
+
save
|
64
|
+
true
|
65
|
+
end
|
66
|
+
|
47
67
|
def current_chat
|
48
68
|
chats.find(current_chat_id)
|
49
69
|
end
|
data/lib/chatgpt_assistant.rb
CHANGED
@@ -8,9 +8,11 @@ require_relative "chatgpt_assistant/version"
|
|
8
8
|
require_relative "chatgpt_assistant/config"
|
9
9
|
require_relative "chatgpt_assistant/models"
|
10
10
|
require_relative "chatgpt_assistant/error"
|
11
|
-
require_relative "bots/application_bot"
|
12
|
-
require_relative "bots/telegram_bot"
|
13
|
-
require_relative "bots/discord_bot"
|
11
|
+
require_relative "chatgpt_assistant/bots/application_bot"
|
12
|
+
require_relative "chatgpt_assistant/bots/telegram_bot"
|
13
|
+
require_relative "chatgpt_assistant/bots/discord_bot"
|
14
|
+
require_relative "chatgpt_assistant/bots/mailers/application_mailer"
|
15
|
+
require_relative "chatgpt_assistant/bots/mailers/account_mailer"
|
14
16
|
require "awesome_chatgpt_actors"
|
15
17
|
require "streamio-ffmpeg"
|
16
18
|
require "aws-sdk-polly"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chatgpt_assistant
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- JesusGautamah
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-07-17 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |-
|
14
14
|
This gem has the intention to facilitate the creation of chatbots with Chatgpt,
|
@@ -22,7 +22,6 @@ executables:
|
|
22
22
|
extensions: []
|
23
23
|
extra_rdoc_files: []
|
24
24
|
files:
|
25
|
-
- ".env_prod_sample"
|
26
25
|
- ".env_sample"
|
27
26
|
- ".rspec"
|
28
27
|
- ".rubocop.yml"
|
@@ -40,33 +39,44 @@ files:
|
|
40
39
|
- SECURITY.md
|
41
40
|
- deploy.sh
|
42
41
|
- deploy_and_build.sh
|
43
|
-
- docker-compose.prod.yml
|
44
42
|
- docker-compose.yml
|
45
43
|
- exe/chatgpt_assistant
|
46
44
|
- exe/chatgpt_bot
|
47
|
-
- lib/bots/application_bot.rb
|
48
|
-
- lib/bots/discord_bot.rb
|
49
|
-
- lib/bots/helpers/audio_helper.rb
|
50
|
-
- lib/bots/helpers/authentication_helper.rb
|
51
|
-
- lib/bots/helpers/discord_helper.rb
|
52
|
-
- lib/bots/helpers/discord_voice_helper.rb
|
53
|
-
- lib/bots/helpers/file_helper.rb
|
54
|
-
- lib/bots/helpers/messenger_helper.rb
|
55
|
-
- lib/bots/helpers/search_helper.rb
|
56
|
-
- lib/bots/helpers/telegram_helper.rb
|
57
|
-
- lib/bots/helpers/telegram_voice_helper.rb
|
58
|
-
- lib/bots/helpers/validation_helper.rb
|
59
|
-
- lib/bots/helpers/visit_helper.rb
|
60
|
-
- lib/bots/jobs/new_chat_job.rb
|
61
|
-
- lib/bots/jobs/register_job.rb
|
62
|
-
- lib/bots/jobs/voice_connect_job.rb
|
63
|
-
- lib/bots/services/new_chat_service.rb
|
64
|
-
- lib/bots/services/register_service.rb
|
65
|
-
- lib/bots/services/voice_connect_service.rb
|
66
|
-
- lib/bots/telegram_bot.rb
|
67
45
|
- lib/chatgpt_assistant.rb
|
68
46
|
- lib/chatgpt_assistant/audio_recognition.rb
|
69
47
|
- lib/chatgpt_assistant/audio_synthesis.rb
|
48
|
+
- lib/chatgpt_assistant/bots/application_bot.rb
|
49
|
+
- lib/chatgpt_assistant/bots/discord/actions.rb
|
50
|
+
- lib/chatgpt_assistant/bots/discord/auth.rb
|
51
|
+
- lib/chatgpt_assistant/bots/discord/bot.rb
|
52
|
+
- lib/chatgpt_assistant/bots/discord/chat_actions.rb
|
53
|
+
- lib/chatgpt_assistant/bots/discord/events.rb
|
54
|
+
- lib/chatgpt_assistant/bots/discord/validations.rb
|
55
|
+
- lib/chatgpt_assistant/bots/discord/voice_actions.rb
|
56
|
+
- lib/chatgpt_assistant/bots/discord/voice_checkers.rb
|
57
|
+
- lib/chatgpt_assistant/bots/discord_bot.rb
|
58
|
+
- lib/chatgpt_assistant/bots/helpers/audio_helper.rb
|
59
|
+
- lib/chatgpt_assistant/bots/helpers/file_helper.rb
|
60
|
+
- lib/chatgpt_assistant/bots/helpers/messenger_helper.rb
|
61
|
+
- lib/chatgpt_assistant/bots/helpers/search_helper.rb
|
62
|
+
- lib/chatgpt_assistant/bots/jobs/new_chat_job.rb
|
63
|
+
- lib/chatgpt_assistant/bots/jobs/register_job.rb
|
64
|
+
- lib/chatgpt_assistant/bots/jobs/voice_connect_job.rb
|
65
|
+
- lib/chatgpt_assistant/bots/mailers/account_mailer.rb
|
66
|
+
- lib/chatgpt_assistant/bots/mailers/application_mailer.rb
|
67
|
+
- lib/chatgpt_assistant/bots/services/new_chat_service.rb
|
68
|
+
- lib/chatgpt_assistant/bots/services/register_service.rb
|
69
|
+
- lib/chatgpt_assistant/bots/services/voice_connect_service.rb
|
70
|
+
- lib/chatgpt_assistant/bots/telegram/actions.rb
|
71
|
+
- lib/chatgpt_assistant/bots/telegram/auth.rb
|
72
|
+
- lib/chatgpt_assistant/bots/telegram/bot.rb
|
73
|
+
- lib/chatgpt_assistant/bots/telegram/chat_actions.rb
|
74
|
+
- lib/chatgpt_assistant/bots/telegram/events.rb
|
75
|
+
- lib/chatgpt_assistant/bots/telegram/events_controller.rb
|
76
|
+
- lib/chatgpt_assistant/bots/telegram/permissions.rb
|
77
|
+
- lib/chatgpt_assistant/bots/telegram/validations.rb
|
78
|
+
- lib/chatgpt_assistant/bots/telegram/voice_actions.rb
|
79
|
+
- lib/chatgpt_assistant/bots/telegram_bot.rb
|
70
80
|
- lib/chatgpt_assistant/chatter.rb
|
71
81
|
- lib/chatgpt_assistant/config.rb
|
72
82
|
- lib/chatgpt_assistant/default_messages.rb
|
data/.env_prod_sample
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
ENV_TYPE=production
|
2
|
-
LANGUAGE=en # or pt currently
|
3
|
-
MODE=aws # or ibm
|
4
|
-
DISCORD_PREFIX=gpt!
|
5
|
-
|
6
|
-
POSTGRES_DB=chatgpt_assistant_production # or your database
|
7
|
-
POSTGRES_USER=postgres # or your user
|
8
|
-
POSTGRES_PASSWORD=postgres # or your password
|
9
|
-
|
10
|
-
IBM_API_KEY=Your IBM API Key
|
11
|
-
IBM_URL=Your IBM Text to Speech URL
|
12
|
-
TELEGRAM_TOKEN=Your Telegram Bot Token
|
13
|
-
OPENAI_API_KEY=Your OpenAI API Key
|
14
|
-
AWS_ACCESS_KEY=Your AWS Access Key
|
15
|
-
AWS_SECRET_ACCESS_KEY=Your AWS Secret Access Key
|
16
|
-
AWS_REGION=Your AWS Region
|
17
|
-
DISCORD_CLIENT_ID=Your Discord Client ID
|
18
|
-
DISCORD_TOKEN=Your Discord Token
|
data/docker-compose.prod.yml
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
version: "3.3"
|
2
|
-
services:
|
3
|
-
db:
|
4
|
-
image: postgres:12.2
|
5
|
-
ports:
|
6
|
-
- "5432:5432"
|
7
|
-
volumes:
|
8
|
-
- ./db_data:/var/lib/postgresql/data
|
9
|
-
env_file:
|
10
|
-
- .env_prod
|
11
|
-
telegram:
|
12
|
-
build: .
|
13
|
-
command: bash -c "exe/chatgpt_bot telegram"
|
14
|
-
volumes:
|
15
|
-
- .:/chatgpt_assistant
|
16
|
-
environment:
|
17
|
-
POSTGRES_HOST: db
|
18
|
-
env_file:
|
19
|
-
- .env_prod
|
20
|
-
depends_on:
|
21
|
-
- db
|
22
|
-
discord:
|
23
|
-
build: .
|
24
|
-
command: bash -c "exe/chatgpt_bot discord"
|
25
|
-
volumes:
|
26
|
-
- .:/chatgpt_assistant
|
27
|
-
environment:
|
28
|
-
POSTGRES_HOST: db
|
29
|
-
env_file:
|
30
|
-
- .env_prod
|
31
|
-
depends_on:
|
32
|
-
- db
|
33
|
-
volumes:
|
34
|
-
postgres:
|
data/lib/bots/discord_bot.rb
DELETED
@@ -1,182 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative "helpers/discord_helper"
|
4
|
-
require_relative "helpers/discord_voice_helper"
|
5
|
-
|
6
|
-
module ChatgptAssistant
|
7
|
-
# This class is responsible to handle the discord bot
|
8
|
-
class DiscordBot < ApplicationBot
|
9
|
-
def start
|
10
|
-
start_event
|
11
|
-
login_event
|
12
|
-
register_event
|
13
|
-
list_event
|
14
|
-
hist_event
|
15
|
-
help_event
|
16
|
-
new_chat_event
|
17
|
-
sl_chat_event
|
18
|
-
ask_event
|
19
|
-
private_message_event
|
20
|
-
bot_init
|
21
|
-
end
|
22
|
-
|
23
|
-
private
|
24
|
-
|
25
|
-
include DiscordHelper
|
26
|
-
include DiscordVoiceHelper
|
27
|
-
|
28
|
-
attr_reader :message
|
29
|
-
attr_accessor :evnt, :user, :chats, :chat
|
30
|
-
|
31
|
-
def start_event
|
32
|
-
bot.command :start do |event|
|
33
|
-
@evnt = event
|
34
|
-
@user = event.user
|
35
|
-
start_action
|
36
|
-
"Ok"
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def login_event
|
41
|
-
bot.command :login do |event|
|
42
|
-
@message = event.message.content.split[1]
|
43
|
-
@evnt = event
|
44
|
-
message.nil? ? event.respond(common_messages[:login]) : login_action
|
45
|
-
"OK"
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
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(common_messages[:register]) : register_action
|
54
|
-
"OK"
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
def list_event
|
59
|
-
bot.command :list do |event|
|
60
|
-
@evnt = event
|
61
|
-
@user = find_user(discord_id: event.user.id)
|
62
|
-
valid_for_list_action? ? list_action : ""
|
63
|
-
"OK"
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
def hist_event
|
68
|
-
bot.command :hist do |event|
|
69
|
-
@evnt = event
|
70
|
-
@user = find_user(discord_id: event.user.id)
|
71
|
-
@chat = user.current_chat
|
72
|
-
event.respond error_messages[:chat_not_found] if chat.nil? && user
|
73
|
-
hist_action if user && chat
|
74
|
-
"OK"
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
def help_event
|
79
|
-
bot.command :help do |event|
|
80
|
-
@evnt = event
|
81
|
-
help_action
|
82
|
-
"OK"
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
def new_chat_event
|
87
|
-
bot.command :new_chat do |event|
|
88
|
-
@evnt = event
|
89
|
-
@user = find_user(discord_id: event.user.id)
|
90
|
-
event.respond error_messages[:user_not_logged_in] if user.nil?
|
91
|
-
create_chat_action if user
|
92
|
-
"OK"
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
def sl_chat_event
|
97
|
-
bot.command :sl_chat do |event|
|
98
|
-
@evnt = event
|
99
|
-
chat_to_select = event.message.content.split[1..].join(" ")
|
100
|
-
@user = find_user(discord_id: event.user.id)
|
101
|
-
event.respond error_messages[:user_not_logged_in] if user.nil?
|
102
|
-
|
103
|
-
sl_chat_action(chat_to_select) if user
|
104
|
-
"OK"
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
def ask_event
|
109
|
-
bot.command :ask do |event|
|
110
|
-
@evnt = event
|
111
|
-
@message = event.message.content.split[1..].join(" ")
|
112
|
-
@user = find_user(discord_id: event.user.id)
|
113
|
-
event.respond error_messages[:user_not_logged_in] if user.nil?
|
114
|
-
ask_action if user
|
115
|
-
"OK"
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
def voice_connect_event
|
120
|
-
bot.command :connect do |event|
|
121
|
-
@evnt = event
|
122
|
-
@user = find_user(discord_id: event.user.id)
|
123
|
-
if user&.current_chat_id.nil?
|
124
|
-
event.respond error_messages[:no_chat_selected]
|
125
|
-
elsif user&.current_chat_id
|
126
|
-
@chat = Chat.where(id: user.current_chat_id).last
|
127
|
-
voice_connect_checker_action
|
128
|
-
voice_connection_checker_action
|
129
|
-
VoiceConnectJob.perform_async(event.user.voice_channel.id)
|
130
|
-
else
|
131
|
-
event.respond error_messages[:user_not_logged_in]
|
132
|
-
end
|
133
|
-
"OK"
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
def disconnect_event
|
138
|
-
bot.command :disconnect do |event|
|
139
|
-
@evnt = event
|
140
|
-
@user = find_user(discord_id: event.user.id)
|
141
|
-
disconnect_checker_action
|
142
|
-
disconnect_action if user && event.user.voice_channel && event.voice
|
143
|
-
"OK"
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
def speak_event
|
148
|
-
bot.command :speak do |event|
|
149
|
-
@evnt = event
|
150
|
-
@message = event.message.content.split[1..].join(" ")
|
151
|
-
@user = find_user(discord_id: event.user.id)
|
152
|
-
@chat = user.current_chat
|
153
|
-
speak_connect_checker_action
|
154
|
-
speak_connection_checker_action
|
155
|
-
ask_to_speak_action if user && event.user.voice_channel && event.voice && !chat.nil?
|
156
|
-
"OK"
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
def private_message_event
|
161
|
-
bot.message do |event|
|
162
|
-
@evnt = event
|
163
|
-
@visitor = discord_visited?(@evnt.user.id)
|
164
|
-
next if discord_next_action?
|
165
|
-
|
166
|
-
@message = event.message.content
|
167
|
-
@user = find_user(discord_id: event.user.id)
|
168
|
-
@chat = user.current_chat if user
|
169
|
-
private_message_action if user && !chat.nil?
|
170
|
-
"OK"
|
171
|
-
end
|
172
|
-
end
|
173
|
-
|
174
|
-
def bot_init
|
175
|
-
at_exit { bot.stop }
|
176
|
-
bot.run
|
177
|
-
rescue StandardError => e
|
178
|
-
Error.create(message: e.message, backtrace: e.backtrace)
|
179
|
-
retry
|
180
|
-
end
|
181
|
-
end
|
182
|
-
end
|
@@ -1,48 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ChatgptAssistant
|
4
|
-
# Helper for authentication
|
5
|
-
module AuthenticationHelper
|
6
|
-
def valid_password?(password, hash, salt)
|
7
|
-
BCrypt::Engine.hash_secret(password, salt) == hash
|
8
|
-
end
|
9
|
-
|
10
|
-
def telegram_user_auth(email, password, telegram_id)
|
11
|
-
return "wrong password" if password.nil?
|
12
|
-
|
13
|
-
visitor_access = find_visitor(telegram_id: telegram_id)
|
14
|
-
return "something went wrong" unless visitor_access
|
15
|
-
|
16
|
-
new_access = find_user(email: email)
|
17
|
-
return "user not found" unless new_access
|
18
|
-
|
19
|
-
hash = new_access.password_hash
|
20
|
-
salt = new_access.password_salt
|
21
|
-
valid_password?(password, hash, salt) ? telegram_user_access(visitor_access, new_access) : "wrong password"
|
22
|
-
end
|
23
|
-
|
24
|
-
def telegram_user_access(visitor, new_access)
|
25
|
-
other_access = where_user(telegram_id: visitor.telegram_id)
|
26
|
-
other_access&.each { |access| access.update(telegram_id: nil) } if other_access&.class == Array
|
27
|
-
other_access&.update(telegram_id: nil) if other_access&.class == User
|
28
|
-
new_access.update(telegram_id: visitor.telegram_id)
|
29
|
-
new_access.email
|
30
|
-
end
|
31
|
-
|
32
|
-
def discord_user_auth(email, password, discord_id)
|
33
|
-
user = find_user(email: email)
|
34
|
-
return "user not found" unless user
|
35
|
-
return "wrong passwords" if password.nil?
|
36
|
-
|
37
|
-
valid_password?(password, user.password_hash, user.password_salt) ? discord_user_access(discord_id, user.email) : "wrong password"
|
38
|
-
end
|
39
|
-
|
40
|
-
def discord_user_access(discord_id, user_email)
|
41
|
-
other_access = where_user(discord_id: discord_id)
|
42
|
-
other_access&.each { |access| access.update(discord_id: nil) }
|
43
|
-
user = find_user(email: user_email)
|
44
|
-
user.update(discord_id: discord_id)
|
45
|
-
user.email
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
@@ -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
|