chatgpt_assistant 0.1.6 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.env_sample +35 -10
- data/.rubocop.yml +14 -3
- data/Dockerfile +11 -5
- data/Gemfile +25 -23
- data/Gemfile.lock +123 -85
- data/README.md +22 -22
- data/Rakefile +1 -0
- data/docker-compose.yml +8 -4
- data/exe/chatgpt_assistant +1 -1
- data/lib/chatgpt_assistant/audio_synthesis.rb +54 -15
- data/lib/{bots → chatgpt_assistant/bots}/application_bot.rb +0 -6
- data/lib/chatgpt_assistant/bots/discord/actions.rb +103 -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 +41 -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 +121 -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 +7 -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 +35 -25
- 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
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "telegram/bot"
|
4
|
+
require_relative "telegram/auth"
|
5
|
+
require_relative "telegram/actions"
|
6
|
+
require_relative "telegram/events"
|
7
|
+
require_relative "telegram/chat_actions"
|
8
|
+
require_relative "telegram/validations"
|
9
|
+
require_relative "telegram/voice_actions"
|
10
|
+
require_relative "telegram/permissions"
|
11
|
+
require_relative "telegram/events_controller"
|
12
|
+
|
13
|
+
module ChatgptAssistant
|
14
|
+
# This class is responsible for the telegram bot features
|
15
|
+
class TelegramBot < ApplicationBot
|
16
|
+
def start
|
17
|
+
bot.listen do |message|
|
18
|
+
@msg = message
|
19
|
+
@visitor = telegram_visited?(@msg.chat.id)
|
20
|
+
next unless telegram_text_or_audio?
|
21
|
+
|
22
|
+
text_events if telegram_message_has_text?
|
23
|
+
audio_event if telegram_message_has_audio?
|
24
|
+
end
|
25
|
+
rescue StandardError => e
|
26
|
+
Error.create(message: e.message, backtrace: e.backtrace)
|
27
|
+
retry
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
attr_accessor :msg, :visitor, :chat, :chat_id
|
33
|
+
|
34
|
+
include Bots::Telegram::Bot
|
35
|
+
include Bots::Telegram::Auth
|
36
|
+
include Bots::Telegram::Actions
|
37
|
+
include Bots::Telegram::Events
|
38
|
+
include Bots::Telegram::ChatActions
|
39
|
+
include Bots::Telegram::Validations
|
40
|
+
include Bots::Telegram::VoiceActions
|
41
|
+
include Bots::Telegram::Permissions
|
42
|
+
include Bots::Telegram::EventsController
|
43
|
+
end
|
44
|
+
end
|
@@ -17,7 +17,7 @@ module ChatgptAssistant
|
|
17
17
|
@response = request(message)
|
18
18
|
@json = JSON.parse(response.body)
|
19
19
|
|
20
|
-
return no_response_error if json["choices"].empty?
|
20
|
+
return no_response_error if json["choices"] && json["choices"].empty?
|
21
21
|
return bot_offline_error if response.status != 200
|
22
22
|
|
23
23
|
text = json["choices"][0]["message"]["content"]
|
@@ -57,7 +57,8 @@ module ChatgptAssistant
|
|
57
57
|
end
|
58
58
|
|
59
59
|
def request_params(message)
|
60
|
-
|
60
|
+
messages_from_chat = Message.where(chat_id: chat_id)
|
61
|
+
messages = messages_from_chat.order(id: :asc).last(10)
|
61
62
|
messages = if messages.empty?
|
62
63
|
[{ role: "user", content: message }]
|
63
64
|
else
|
@@ -66,6 +67,13 @@ module ChatgptAssistant
|
|
66
67
|
content: mess.content }
|
67
68
|
end
|
68
69
|
end
|
70
|
+
|
71
|
+
first_message = messages_from_chat.order(id: :asc).first
|
72
|
+
system_message = first_message if first_message&.role == "system"
|
73
|
+
if system_message && Message.where(chat_id: chat_id).count > 10
|
74
|
+
messages.unshift({ role: system_message.role,
|
75
|
+
content: system_message.content })
|
76
|
+
end
|
69
77
|
{
|
70
78
|
model: "gpt-3.5-turbo",
|
71
79
|
messages: messages,
|
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require "active_record"
|
4
4
|
require "active_model"
|
5
|
+
require "action_mailer"
|
5
6
|
require_relative "migrations"
|
6
7
|
require "fileutils"
|
7
8
|
|
@@ -26,11 +27,16 @@ module ChatgptAssistant
|
|
26
27
|
@aws_secret_access_key = ENV.fetch("AWS_SECRET_ACCESS_KEY", nil)
|
27
28
|
@aws_region = ENV.fetch("AWS_REGION", nil)
|
28
29
|
@discord_prefix = ENV.fetch("DISCORD_PREFIX", nil)
|
30
|
+
@azure_speech_url = ENV.fetch("AZURE_SPEECH_URL", nil)
|
31
|
+
@azure_speech_key = ENV.fetch("AZURE_SPEECH_KEY", nil)
|
29
32
|
end
|
30
33
|
|
31
34
|
attr_reader :openai_api_key, :telegram_token, :discord_token, :ibm_api_key, :ibm_url,
|
32
35
|
:aws_access_key_id, :aws_secret_access_key, :aws_region, :mode, :language,
|
33
|
-
:discord_client_id, :discord_public_key, :env_type, :discord_prefix
|
36
|
+
:discord_client_id, :discord_public_key, :env_type, :discord_prefix,
|
37
|
+
:azure_speech_url, :azure_speech_key,
|
38
|
+
:smtp_address, :smtp_port, :smtp_domain, :smtp_user_name, :smtp_password,
|
39
|
+
:smtp_authentication, :smtp_enable_starttls_auto
|
34
40
|
|
35
41
|
def db_connection
|
36
42
|
ActiveRecord::Base.establish_connection(
|
@@ -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.8
|
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-23 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
|
@@ -107,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
117
|
- !ruby/object:Gem::Version
|
108
118
|
version: '0'
|
109
119
|
requirements: []
|
110
|
-
rubygems_version: 3.
|
120
|
+
rubygems_version: 3.4.10
|
111
121
|
signing_key:
|
112
122
|
specification_version: 4
|
113
123
|
summary: Easy way chatbot with Chatgpt, Telegram bot, Discord bot, currently using
|
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
|