chatgpt_assistant 0.1.6 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- 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
data/lib/bots/telegram_bot.rb
DELETED
@@ -1,180 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative "helpers/telegram_helper"
|
4
|
-
require_relative "helpers/telegram_voice_helper"
|
5
|
-
|
6
|
-
module ChatgptAssistant
|
7
|
-
# This class is responsible for the telegram bot features
|
8
|
-
class TelegramBot < ApplicationBot
|
9
|
-
def start
|
10
|
-
bot.listen do |message|
|
11
|
-
@msg = message
|
12
|
-
@visitor = telegram_visited?(@msg.chat.id)
|
13
|
-
next unless telegram_text_or_audio?
|
14
|
-
|
15
|
-
text_events if telegram_message_has_text?
|
16
|
-
audio_event if telegram_message_has_audio?
|
17
|
-
end
|
18
|
-
rescue StandardError => e
|
19
|
-
Error.create(message: e.message, backtrace: e.backtrace)
|
20
|
-
retry
|
21
|
-
end
|
22
|
-
|
23
|
-
private
|
24
|
-
|
25
|
-
include TelegramHelper
|
26
|
-
include TelegramVoiceHelper
|
27
|
-
|
28
|
-
attr_accessor :msg, :visitor, :chat, :chat_id
|
29
|
-
|
30
|
-
def text_events
|
31
|
-
case msg.text
|
32
|
-
when "/start"
|
33
|
-
start_event
|
34
|
-
when "/help"
|
35
|
-
help_event
|
36
|
-
when "/hist"
|
37
|
-
hist_event
|
38
|
-
when "/list"
|
39
|
-
list_event
|
40
|
-
when "/stop"
|
41
|
-
stop_event
|
42
|
-
when nil
|
43
|
-
raise NilError
|
44
|
-
else
|
45
|
-
action_events
|
46
|
-
end
|
47
|
-
rescue NilError => e
|
48
|
-
send_message e.message, msg.chat.id
|
49
|
-
end
|
50
|
-
|
51
|
-
def start_event
|
52
|
-
telegram_send_start_message
|
53
|
-
end
|
54
|
-
|
55
|
-
def help_event
|
56
|
-
help_messages.each { |m| send_message m, msg.chat.id }
|
57
|
-
end
|
58
|
-
|
59
|
-
def hist_event
|
60
|
-
raise UserNotLoggedInError if user.nil?
|
61
|
-
raise NoChatSelectedError if user.current_chat.nil?
|
62
|
-
raise NoMessagesFoundedError if user.current_chat.messages.count.zero?
|
63
|
-
|
64
|
-
user.chat_history.each do |m|
|
65
|
-
send_message m, msg.chat.id
|
66
|
-
end
|
67
|
-
rescue NoChatSelectedError, UserNotLoggedInError, NoMessagesFoundedError => e
|
68
|
-
send_message e.message, msg.chat.id
|
69
|
-
end
|
70
|
-
|
71
|
-
def list_event
|
72
|
-
raise UserNotLoggedInError if user.nil?
|
73
|
-
raise NoChatsFoundedError if user.chats.count.zero?
|
74
|
-
|
75
|
-
send_message common_messages[:chat_list], msg.chat.id
|
76
|
-
chats_str = ""
|
77
|
-
user.chats.each_with_index { |c, i| chats_str += "Chat #{i + 1} - #{c.title}\n" }
|
78
|
-
send_message chats_str, msg.chat.id
|
79
|
-
rescue NoChatsFoundedError, UserNotLoggedInError => e
|
80
|
-
send_message e.message, msg.chat.id
|
81
|
-
end
|
82
|
-
|
83
|
-
def action_events
|
84
|
-
return auth_events if auth_event?
|
85
|
-
return new_chat_event if msg.text.include?("new_chat/")
|
86
|
-
return select_chat_event if msg.text.include?("sl_chat/")
|
87
|
-
return telegram_chat_event unless telegram_actions?
|
88
|
-
|
89
|
-
raise InvalidCommandError
|
90
|
-
rescue InvalidCommandError => e
|
91
|
-
send_message e.message, msg.chat.id
|
92
|
-
end
|
93
|
-
|
94
|
-
def auth_event?
|
95
|
-
msg.text.include?("login/") || msg.text.include?("register/") || msg.text.include?("sign_out/")
|
96
|
-
end
|
97
|
-
|
98
|
-
def auth_events
|
99
|
-
return login_event if msg.text.include?("login/")
|
100
|
-
return register_event if msg.text.include?("register/")
|
101
|
-
return sign_out_event if msg.text.include?("sign_out/")
|
102
|
-
end
|
103
|
-
|
104
|
-
def login_event
|
105
|
-
raise UserLoggedInError if user
|
106
|
-
|
107
|
-
user_info = msg.text.split("/").last
|
108
|
-
email, password = user_info.split(":")
|
109
|
-
case telegram_user_auth(email, password, msg.chat.id)
|
110
|
-
when "user not found"
|
111
|
-
raise UserNotFoundError
|
112
|
-
when "wrong password"
|
113
|
-
raise WrongPasswordError
|
114
|
-
when email
|
115
|
-
user_logged_in_message
|
116
|
-
end
|
117
|
-
rescue UserNotFoundError, WrongPasswordError, UserLoggedInError => e
|
118
|
-
send_message e.message, msg.chat.id
|
119
|
-
end
|
120
|
-
|
121
|
-
def register_event
|
122
|
-
user_info = msg.text.split("/").last
|
123
|
-
raise NoRegisterInfoError if user_info.nil?
|
124
|
-
raise UserLoggedInError if user
|
125
|
-
|
126
|
-
email, password = user_info.split(":")
|
127
|
-
raise NoRegisterInfoError if email.nil? || password.nil?
|
128
|
-
|
129
|
-
RegisterJob.perform_async(email, password, visitor.telegram_id)
|
130
|
-
rescue NoRegisterInfoError, UserLoggedInError => e
|
131
|
-
send_message e.message, msg.chat.id
|
132
|
-
end
|
133
|
-
|
134
|
-
def sign_out_event
|
135
|
-
raise UserNotLoggedInError if user.nil?
|
136
|
-
|
137
|
-
user.update(telegram_id: nil)
|
138
|
-
send_message success_messages[:user_logged_out], msg.chat.id
|
139
|
-
rescue UserNotLoggedInError => e
|
140
|
-
send_message e.message, msg.chat.id
|
141
|
-
end
|
142
|
-
|
143
|
-
def new_chat_event
|
144
|
-
raise UserNotLoggedInError if user.nil?
|
145
|
-
|
146
|
-
NewChatJob.perform_async(msg.text.split("/").last, user.id, msg.chat.id)
|
147
|
-
rescue UserNotLoggedInError => e
|
148
|
-
send_message e.message, msg.chat.id
|
149
|
-
end
|
150
|
-
|
151
|
-
def select_chat_event
|
152
|
-
raise UserNotLoggedInError if user.nil?
|
153
|
-
|
154
|
-
title = msg.text.split("/").last
|
155
|
-
chat = user.chat_by_title(title)
|
156
|
-
raise ChatNotFoundError if chat.nil?
|
157
|
-
|
158
|
-
raise ChatNotFoundError unless user.update(current_chat_id: chat.id)
|
159
|
-
|
160
|
-
send_message success_messages[:chat_selected], msg.chat.id
|
161
|
-
rescue UserNotLoggedInError, ChatNotFoundError => e
|
162
|
-
send_message e.message, msg.chat.id
|
163
|
-
end
|
164
|
-
|
165
|
-
def audio_event
|
166
|
-
raise UserNotLoggedInError if user.nil?
|
167
|
-
raise NoChatSelectedError if user.current_chat.nil?
|
168
|
-
|
169
|
-
user_audio = transcribe_file(telegram_audio_url)
|
170
|
-
message = Message.new(content: user_audio[:text], chat_id: user.current_chat_id, role: "user")
|
171
|
-
raise MessageNotSavedError unless message.save
|
172
|
-
|
173
|
-
ai_response = telegram_process_ai_voice(user_audio[:file])
|
174
|
-
telegram_send_voice_message(voice: ai_response[:voice], text: ai_response[:text])
|
175
|
-
delete_file ai_response[:voice]
|
176
|
-
rescue UserNotLoggedInError, NoChatSelectedError, MessageNotSavedError => e
|
177
|
-
send_message e.message, msg.chat.id
|
178
|
-
end
|
179
|
-
end
|
180
|
-
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|