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
@@ -1,185 +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
- voice_connect_event
20
- disconnect_event
21
- speak_event
22
- private_message_event
23
- bot_init
24
- end
25
-
26
- private
27
-
28
- include DiscordHelper
29
- include DiscordVoiceHelper
30
-
31
- attr_reader :message
32
- attr_accessor :evnt, :user, :chats, :chat
33
-
34
- def start_event
35
- bot.command :start do |event|
36
- @evnt = event
37
- @user = event.user
38
- start_action
39
- "Ok"
40
- end
41
- end
42
-
43
- def login_event
44
- bot.command :login do |event|
45
- @message = event.message.content.split[1]
46
- @evnt = event
47
- message.nil? ? event.respond(commom_messages[:login]) : login_action
48
- "OK"
49
- end
50
- end
51
-
52
- def register_event
53
- bot.command :register do |event|
54
- @message = event.message.content.split[1]
55
- @evnt = event
56
- message.nil? ? event.respond(commom_messages[:register]) : register_action
57
- "OK"
58
- end
59
- end
60
-
61
- def list_event
62
- bot.command :list do |event|
63
- @evnt = event
64
- @user = find_user(discord_id: event.user.id)
65
- valid_for_list_action? ? list_action : ""
66
- "OK"
67
- end
68
- end
69
-
70
- def hist_event
71
- bot.command :hist do |event|
72
- @evnt = event
73
- event.respond error_messages[:user_not_logged_in] if user.nil?
74
- title = event.message.content.split[1..].join(" ")
75
- @chat = user.chat_by_title(title)
76
- event.respond error_messages[:chat_not_found] if chat.nil? && user
77
- hist_action if user && chat
78
- "OK"
79
- end
80
- end
81
-
82
- def help_event
83
- bot.command :help do |event|
84
- @evnt = event
85
- help_action
86
- "OK"
87
- end
88
- end
89
-
90
- def new_chat_event
91
- bot.command :new_chat do |event|
92
- @evnt = event
93
- @user = find_user(discord_id: event.user.id)
94
- event.respond error_messages[:user_not_logged_in] if user.nil?
95
- create_chat_action if user
96
- "OK"
97
- end
98
- end
99
-
100
- def sl_chat_event
101
- bot.command :sl_chat do |event|
102
- @evnt = event
103
- chat_to_select = event.message.content.split[1..].join(" ")
104
- @user = find_user(discord_id: event.user.id)
105
- event.respond error_messages[:user_not_logged_in] if user.nil?
106
-
107
- sl_chat_action(chat_to_select) if user
108
- "OK"
109
- end
110
- end
111
-
112
- def ask_event
113
- bot.command :ask do |event|
114
- @evnt = event
115
- @message = event.message.content.split[1..].join(" ")
116
- @user = find_user(discord_id: event.user.id)
117
- event.respond error_messages[:user_not_logged_in] if user.nil?
118
- ask_action if user
119
- "OK"
120
- end
121
- end
122
-
123
- def voice_connect_event
124
- bot.command :connect do |event|
125
- @evnt = event
126
- @user = find_user(discord_id: event.user.id)
127
- if user&.current_chat_id.nil?
128
- event.respond error_messages[:no_chat_selected]
129
- elsif user&.current_chat_id
130
- @chat = Chat.where(id: user.current_chat_id).last
131
- voice_connect_checker_action
132
- voice_connection_checker_action
133
- discord_voice_bot_connect
134
- else
135
- event.respond error_messages[:user_not_logged_in]
136
- end
137
- "OK"
138
- end
139
- end
140
-
141
- def disconnect_event
142
- bot.command :disconnect do |event|
143
- @evnt = event
144
- @user = find_user(discord_id: event.user.id)
145
- disconnect_checker_action
146
- disconnect_action if user && event.user.voice_channel && event.voice
147
- "OK"
148
- end
149
- end
150
-
151
- def speak_event
152
- bot.command :speak do |event|
153
- @evnt = event
154
- @message = event.message.content.split[1..].join(" ")
155
- @user = find_user(discord_id: event.user.id)
156
- @chat = user.current_chat
157
- speak_connect_checker_action
158
- speak_connection_checker_action
159
- ask_to_speak_action if user && event.user.voice_channel && event.voice && !chat.nil?
160
- "OK"
161
- end
162
- end
163
-
164
- def private_message_event
165
- bot.message do |event|
166
- @evnt = event
167
- next if discord_next_action?
168
-
169
- @message = event.message.content
170
- @user = find_user(discord_id: event.user.id)
171
- @chat = user.current_chat if user
172
- private_message_action if user && !chat.nil?
173
- "OK"
174
- end
175
- end
176
-
177
- def bot_init
178
- at_exit { bot.stop }
179
- bot.run
180
- rescue StandardError => e
181
- Error.create(message: e.message, backtrace: e.backtrace)
182
- retry
183
- end
184
- end
185
- end
@@ -1,47 +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(_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) }
27
- new_access.update(telegram_id: visitor.telegram_id)
28
- new_access.email
29
- end
30
-
31
- def discord_user_auth(email, password, discord_id)
32
- user = find_user(email: email)
33
- return "user not found" unless user
34
- return "wrong passwords" if password.nil?
35
-
36
- valid_password?(password, user.password_hash, user.password_salt) ? discord_user_access(discord_id, user.email) : "wrong password"
37
- end
38
-
39
- def discord_user_access(discord_id, user_email)
40
- other_access = where_user(discord_id: discord_id)
41
- other_access&.each { |access| access.update(discord_id: nil) }
42
- user = find_user(email: user_email)
43
- user.update(discord_id: discord_id)
44
- user.email
45
- end
46
- end
47
- 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 commom_messages[:start_helper].gsub("register/", "gpt!register ")
16
- send_message commom_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 commom_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(prompt_type: actor_name) 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
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ChatgptAssistant
4
- # Helper for logger action
5
- module LoggerActionHelper
6
- end
7
- end
@@ -1,134 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ChatgptAssistant
4
- # Helper for messenger
5
- module MessengerHelper
6
- def chat_success(chat_id)
7
- user_message = Message.new(chat_id: chat_id, content: msg.text, role: "user")
8
- if user_message.save
9
- text = chatter.chat(msg.text, chat_id, error_messages[:something_went_wrong])
10
- mess = parse_message(text, 4096)
11
- mess.each { |m| send_message m, msg.chat.id }
12
- else
13
- send_message error_messages[:something_went_wrong], msg.chat.id
14
- end
15
- end
16
-
17
- def respond_with_success(chat)
18
- user.update(current_chat_id: chat.id)
19
- send_message success_messages[:chat_created]
20
- end
21
-
22
- def parse_message(message, max_length)
23
- if message.length > max_length
24
- array = message.scan(/.{1,#{max_length}}/) if max_length.positive?
25
- array = ["Something went wrong! Try again later"] if max_length <= 0
26
- else
27
- array = [message]
28
- end
29
- array
30
- end
31
-
32
- def send_message(text, chat_id = nil)
33
- @send_message = bot.respond_to?(:api) ? telegram_send_message(text, chat_id) : discord_send_message(text)
34
- end
35
-
36
- def telegram_send_message(text, chat_id)
37
- messages = parse_message(text, 4096)
38
- messages.each { |m| bot.api.send_message(chat_id: chat_id, text: m) }
39
- end
40
-
41
- def discord_send_message(text)
42
- messages = parse_message(text, 2000)
43
- messages.each { |m| evnt.respond m }
44
- end
45
-
46
- def discord_help_message
47
- help_messages.join("\n").gsub(" /", discord_prefix)
48
- .gsub("register/", "#{discord_prefix}register ")
49
- .gsub("login/", "#{discord_prefix}login ")
50
- .gsub("new_chat/", "#{discord_prefix}new_chat/")
51
- .gsub("sl_chat/", "#{discord_prefix}sl_chat/")
52
- end
53
-
54
- def user_logged_message
55
- user.update(telegram_id: msg.chat.id)
56
- bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:user_logged_in])
57
- evnt&.respond success_messages[:user_logged_in] if evnt.present?
58
- end
59
-
60
- def invalid_command_error_message
61
- bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:invalid_command])
62
- evnt&.respond error_messages[:invalid_command]
63
- end
64
-
65
- def user_not_found_error_message
66
- bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:user_not_found]) if bot.api.present?
67
- evnt&.respond error_messages[:user_not_found] if evnt.present?
68
- end
69
-
70
- def user_created_message
71
- bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:user_created]) if bot.api.present?
72
- evnt&.respond success_messages[:user_created] if evnt.present?
73
- end
74
-
75
- def user_creation_error_message
76
- bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:user_creation]) if bot.api.present?
77
- evnt&.respond error_messages[:user_creation] if evnt.present?
78
- end
79
-
80
- def chat_created_message(chat)
81
- user&.update(current_chat_id: chat.id)
82
- return telegram_created(chat) if bot.respond_to?(:api)
83
-
84
- discord_created(chat)
85
- end
86
-
87
- def telegram_created(chat)
88
- bot.api.send_message(chat_id: msg.chat.id, text: "Intructions sended to actor:\n#{chat.prompt}") unless chat.actor.nil?
89
- bot.api.send_message(chat_id: msg.chat.id, text: "Response from assistant:\n#{chat.messages[1].content}") unless chat.actor.nil?
90
- bot.api.send_message(chat_id: msg.chat.id, text: success_messages[:chat_created]) if bot.api.present?
91
- end
92
-
93
- def discord_created(chat)
94
- evnt.respond "Intructions sended to actor:\n#{chat.prompt}" unless chat.actor.nil?
95
- evnt.respond "Response from assistant:\n#{chat.messages[1].content}" unless chat.actor.nil?
96
- evnt.respond success_messages[:chat_created] if evnt.present?
97
- end
98
-
99
- def not_logged_in_message
100
- bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:user_not_logged_in])
101
- evnt&.respond(error_messages[:user_not_logged_in])
102
- end
103
-
104
- def wrong_password_error_message
105
- bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:password])
106
- evnt&.respond(error_messages[:password])
107
- end
108
-
109
- def chat_not_found_message
110
- bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:chat_not_found])
111
- evnt&.respond(error_messages[:chat_not_found])
112
- end
113
-
114
- def no_chat_selected_message
115
- bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:no_chat_selected])
116
- evnt&.respond(error_messages[:no_chat_selected])
117
- end
118
-
119
- def no_messages_founded_message
120
- bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:no_messages_founded])
121
- evnt&.respond(error_messages[:no_messages_founded])
122
- end
123
-
124
- def chat_creation_failed_message
125
- bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:chat_creation_failed])
126
- evnt&.respond(error_messages[:chat_creation_failed])
127
- end
128
-
129
- def user_logged_in_message
130
- bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:user_logged_in])
131
- evnt&.respond(success_messages[:user_logged_in])
132
- end
133
- end
134
- end
@@ -1,77 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ChatgptAssistant
4
- # Helper for telegram
5
- module TelegramHelper
6
- def user
7
- @user = find_user(telegram_id: msg.chat.id)
8
- end
9
-
10
- def bot
11
- @bot ||= Telegram::Bot::Client.new(telegram_token)
12
- end
13
-
14
- def telegram_chat_event
15
- user ? chat_if_exists : not_logged_in_message
16
- end
17
-
18
- def telegram_user_create(visitor_id, email, password)
19
- visitor = Visitor.find_by(id: visitor_id)
20
- return false unless visitor
21
-
22
- visitor.tel_user.update(telegram_id: nil) if visitor.tel_user.present?
23
- user = User.new(telegram_id: visitor.telegram_id, email: email, password: password)
24
- user.save ? user.email : user.errors.full_messages
25
- end
26
-
27
- def telegram_send_start_message
28
- send_message commom_messages[:start], msg.chat.id
29
- help_message = help_messages.join("\n").to_s
30
- send_message help_message, msg.chat.id
31
- send_message commom_messages[:start_helper], msg.chat.id
32
- send_message commom_messages[:start_sec_helper], msg.chat.id
33
- end
34
-
35
- def telegram_create_chat
36
- text = msg.text
37
- title = text.split("/").last
38
- mode = nil
39
- if title.include?(":")
40
- mode = title.split(":").last
41
- title = title.split(":").first
42
- end
43
- actor_modes = AwesomeChatgptActors::CastControl.actors
44
- return send_message "invalid mode", msg.chat.id unless (mode.to_i >= 1 && mode.to_i <= actor_modes.size + 1) || mode.nil?
45
- return send_message "invalid chat title", msg.chat.id if title.nil? || title.empty?
46
- return send_message "You already have a chat with this title", msg.chat.id if user.chat_by_title(title)
47
-
48
- actor_name = actor_modes[mode.to_i - 1] if mode
49
- actor = AwesomeChatgptActors::Actor.new(prompt_type: actor_name) if actor_name
50
- chat = Chat.new(user_id: user.id, status: 0, title: title, actor: actor_name, prompt: actor.prompt) if actor
51
- chat = Chat.new(user_id: user.id, status: 0, title: title) unless actor
52
- return send_message "Something went wrong", msg.chat.id unless chat
53
-
54
- chat.save ? chat_created_message(chat) : chat_creation_failed_message
55
- end
56
-
57
- def telegram_user_history
58
- user.current_chat.messages.last(10).map { |m| "#{m.role}: #{m.content}\nat: #{m.created_at}" }
59
- end
60
-
61
- def telegram_text_or_audio?
62
- msg.respond_to?(:text) || msg.respond_to?(:audio) || msg.respond_to?(:voice)
63
- end
64
-
65
- def telegram_message_has_text?
66
- msg.text.present?
67
- end
68
-
69
- def telegram_message_has_audio?
70
- msg.audio.present? || msg.voice.present?
71
- end
72
-
73
- def telegram_actions?
74
- msg.text.include?("new_chat/") || msg.text.include?("sl_chat/") || msg.text.include?("login/") || msg.text.include?("register/")
75
- end
76
- end
77
- end
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ChatgptAssistant
4
- # Telegram Voice Helper
5
- module TelegramVoiceHelper
6
- def telegram_audio_info
7
- bot.api.get_file(file_id: telegram_audio.file_id)
8
- end
9
-
10
- def telegram_audio_url
11
- "https://api.telegram.org/file/bot#{telegram_token}/#{telegram_audio_info["result"]["file_path"]}"
12
- end
13
-
14
- def telegram_audio
15
- msg.audio || msg.voice
16
- end
17
-
18
- def telegram_process_ai_voice(user_audio)
19
- ai_text = chatter.chat(user_audio["text"], user.current_chat_id, error_messages[:something_went_wrong])
20
- ai_voice = synthesis.synthesize_text(ai_text)
21
- {
22
- voice: ai_voice,
23
- text: ai_text
24
- }
25
- end
26
-
27
- def telegram_send_voice_message(voice: nil, text: nil)
28
- messages = parse_message text, 4096
29
- bot.api.send_voice(chat_id: msg.chat.id, voice: Faraday::UploadIO.new(voice, "audio/mp3"))
30
- messages.each { |message| send_message message, msg.chat.id }
31
- end
32
- end
33
- end