chatgpt_assistant 0.1.3 → 0.1.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c735d019dd75e4679918ae36b75d61978f78cf4b7ca11c8530915cc738cb7ec6
4
- data.tar.gz: 73bdda5095040e37e3bb270c287ec8549fe156749a35b28ecca592c5205b17c9
3
+ metadata.gz: 18ba3cf9f452f76a08c75f2e0dfd406ed8c7fe5bd35ac530abce0217ed91c02c
4
+ data.tar.gz: 34f3574731f3ffe82469b49c41e27f531f2d1f75f83989327ee46d3e700f5501
5
5
  SHA512:
6
- metadata.gz: 5ebf7e87f62c820eb5988dfe6a0635d385a2329bdb7b690e5ee15bac182ab162f8e502b111d14fb4c66aa6923a702e57b7f8ae5fef351582ff5b1a9a1bd7bf8f
7
- data.tar.gz: 78367d4df7a05b0290889fb854a925abd239b277e8fe9f7236db9c6715405c989f40ed0d732c0db05e3abd6e63c70099c96317fe5f3c13213f53aa26d5cf84f8
6
+ metadata.gz: 5d4cafcef50cef6a15fcd3d50ed9f98ef77f65f1b2c6611887f27a33a423498f6258dbad2b1352ff0ee5f6b59a8995ba3e062a0ff01185b6a8b8e5ec464deaed
7
+ data.tar.gz: 7081185b589e2ef8566a76e43d513fba382549a577d08f9384315e92601341bf35df1311c6da65b0588d8cf62b53db8c6e7c1e29f52ad2fe7990976397b16a33
data/.rubocop.yml CHANGED
@@ -1,5 +1,6 @@
1
1
  AllCops:
2
2
  TargetRubyVersion: 2.6
3
+ NewCops: enable
3
4
 
4
5
  Style/StringLiterals:
5
6
  Enabled: true
@@ -25,7 +26,7 @@ Metrics/ClassLength:
25
26
  Max: 300
26
27
 
27
28
  Metrics/AbcSize:
28
- Max: 35
29
+ Max: 28
29
30
 
30
31
  Layout/AccessModifierIndentation:
31
32
  EnforcedStyle: indent
data/Dockerfile CHANGED
@@ -1,4 +1,4 @@
1
- FROM ruby:3.2.0
1
+ FROM ruby:3.2.2
2
2
  RUN apt-get update -qq && apt-get install -y nodejs postgresql-client ffmpeg
3
3
  WORKDIR /chatgpt_assistant
4
4
  COPY Gemfile /chatgpt_assistant/Gemfile
data/Gemfile CHANGED
@@ -12,7 +12,7 @@ gem "simplecov", "~> 0.21", group: :test
12
12
 
13
13
  gem "vcr", "~> 6.0", group: :test
14
14
 
15
- gem "rubocop", "~> 1.21"
15
+ gem "rubocop", "~> 1.49"
16
16
 
17
17
  gem "rubocop-rspec", "~> 2.4"
18
18
 
@@ -47,3 +47,5 @@ gem "lucy_dockerunner"
47
47
  gem "ibm_watson"
48
48
 
49
49
  gem "aws-sdk-polly"
50
+
51
+ gem "awesome_chatgpt_actors"
data/Gemfile.lock CHANGED
@@ -42,6 +42,9 @@ GEM
42
42
  addressable (2.8.1)
43
43
  public_suffix (>= 2.0.2, < 6.0)
44
44
  ast (2.4.2)
45
+ awesome_chatgpt_actors (0.1.2)
46
+ csv
47
+ ruby-openai
45
48
  aws-eventstream (1.2.0)
46
49
  aws-partitions (1.735.0)
47
50
  aws-sdk-core (3.171.0)
@@ -65,6 +68,7 @@ GEM
65
68
  concurrent-ruby (1.2.2)
66
69
  connection_pool (2.4.0)
67
70
  crass (1.0.6)
71
+ csv (3.2.6)
68
72
  date (3.3.3)
69
73
  descendants_tracker (0.0.4)
70
74
  thread_safe (~> 0.3, >= 0.3.1)
@@ -115,6 +119,9 @@ GEM
115
119
  http-form_data (2.3.0)
116
120
  http-parser (1.2.3)
117
121
  ffi-compiler (>= 1.0, < 2.0)
122
+ httparty (0.21.0)
123
+ mini_mime (>= 1.0.0)
124
+ multi_xml (>= 0.5.2)
118
125
  i18n (1.12.0)
119
126
  concurrent-ruby (~> 1.0)
120
127
  ibm_cloud_sdk_core (1.1.3)
@@ -148,6 +155,7 @@ GEM
148
155
  mini_portile2 (2.8.1)
149
156
  minitest (5.18.0)
150
157
  multi_json (1.15.0)
158
+ multi_xml (0.6.0)
151
159
  multipart-post (2.3.0)
152
160
  net-imap (0.3.4)
153
161
  date
@@ -167,7 +175,7 @@ GEM
167
175
  opus-ruby (1.0.1)
168
176
  ffi
169
177
  parallel (1.22.1)
170
- parser (3.2.1.0)
178
+ parser (3.2.2.0)
171
179
  ast (~> 2.4.1)
172
180
  pg (1.4.6)
173
181
  public_suffix (5.0.1)
@@ -207,24 +215,26 @@ GEM
207
215
  diff-lcs (>= 1.2.0, < 2.0)
208
216
  rspec-support (~> 3.12.0)
209
217
  rspec-support (3.12.0)
210
- rubocop (1.45.1)
218
+ rubocop (1.49.0)
211
219
  json (~> 2.3)
212
220
  parallel (~> 1.10)
213
221
  parser (>= 3.2.0.0)
214
222
  rainbow (>= 2.2.2, < 4.0)
215
223
  regexp_parser (>= 1.8, < 3.0)
216
224
  rexml (>= 3.2.5, < 4.0)
217
- rubocop-ast (>= 1.24.1, < 2.0)
225
+ rubocop-ast (>= 1.28.0, < 2.0)
218
226
  ruby-progressbar (~> 1.7)
219
227
  unicode-display_width (>= 2.4.0, < 3.0)
220
- rubocop-ast (1.26.0)
228
+ rubocop-ast (1.28.0)
221
229
  parser (>= 3.2.1.0)
222
230
  rubocop-capybara (2.17.1)
223
231
  rubocop (~> 1.41)
224
232
  rubocop-rspec (2.19.0)
225
233
  rubocop (~> 1.33)
226
234
  rubocop-capybara (~> 2.17)
227
- ruby-progressbar (1.11.0)
235
+ ruby-openai (3.7.0)
236
+ httparty (>= 0.18.1)
237
+ ruby-progressbar (1.13.0)
228
238
  ruby2_keywords (0.0.5)
229
239
  rufus-scheduler (3.8.2)
230
240
  fugit (~> 1.1, >= 1.1.6)
@@ -282,6 +292,7 @@ DEPENDENCIES
282
292
  activerecord
283
293
  activerecord-import
284
294
  activesupport
295
+ awesome_chatgpt_actors
285
296
  aws-sdk-polly
286
297
  bcrypt
287
298
  discordrb (~> 3.4)
@@ -292,7 +303,7 @@ DEPENDENCIES
292
303
  rake (~> 13.0)
293
304
  redis
294
305
  rspec (~> 3.0)
295
- rubocop (~> 1.21)
306
+ rubocop (~> 1.49)
296
307
  rubocop-rspec (~> 2.4)
297
308
  sidekiq
298
309
  sidekiq-scheduler
data/Rakefile CHANGED
@@ -3,6 +3,7 @@
3
3
  # require "bundler/gem_tasks"
4
4
  require "rspec/core/rake_task"
5
5
  require "lucy_dockerunner"
6
+ require "awesome_chatgpt_actors"
6
7
 
7
8
  RSpec::Core::RakeTask.new(:spec)
8
9
 
@@ -13,3 +14,4 @@ RuboCop::RakeTask.new
13
14
  task default: %i[spec rubocop]
14
15
 
15
16
  LucyDockerunner.load_tasks
17
+ AwesomeChatgptActors.load_tasks
data/SECURITY.md ADDED
@@ -0,0 +1,15 @@
1
+ # Security Policy
2
+
3
+ ## Supported Versions
4
+
5
+ | Version | Supported |
6
+ | ------- | ------------------ |
7
+ | 0.1.4 | :white_check_mark: |
8
+ | 0.1.3 | :x: |
9
+ | 0.1.2 | :x: |
10
+ | 0.1.1 | :x: |
11
+ | 0.1.0 | :x: |
12
+
13
+ ## Reporting a Vulnerability
14
+
15
+ You can open an security vulnerability issue
data/exe/chatgpt_bot CHANGED
@@ -5,7 +5,7 @@ require_relative "../lib/chatgpt_assistant"
5
5
 
6
6
  puts "Starting Chatgpt Assistant"
7
7
  puts "Bot Type: #{ARGV[0]}"
8
- puts "Mode: #{ENV["MODE"]}"
8
+ puts "Mode: #{ENV.fetch("MODE", nil)}"
9
9
  case ARGV[0]
10
10
  when "telegram"
11
11
  ChatgptAssistant::Main.new("telegram").start
@@ -19,6 +19,7 @@ module ChatgptAssistant
19
19
  voice_connect_event
20
20
  disconnect_event
21
21
  speak_event
22
+ private_message_event
22
23
  bot_init
23
24
  end
24
25
 
@@ -35,22 +36,25 @@ module ChatgptAssistant
35
36
  @evnt = event
36
37
  @user = event.user
37
38
  start_action
39
+ "Ok"
38
40
  end
39
41
  end
40
42
 
41
43
  def login_event
42
44
  bot.command :login do |event|
43
- @message = event.message.content.split(" ")[1]
45
+ @message = event.message.content.split[1]
44
46
  @evnt = event
45
47
  message.nil? ? event.respond(commom_messages[:login]) : login_action
48
+ "OK"
46
49
  end
47
50
  end
48
51
 
49
52
  def register_event
50
53
  bot.command :register do |event|
51
- @message = event.message.content.split(" ")[1]
54
+ @message = event.message.content.split[1]
52
55
  @evnt = event
53
56
  message.nil? ? event.respond(commom_messages[:register]) : register_action
57
+ "OK"
54
58
  end
55
59
  end
56
60
 
@@ -58,9 +62,8 @@ module ChatgptAssistant
58
62
  bot.command :list do |event|
59
63
  @evnt = event
60
64
  @user = find_user(discord_id: event.user.id)
61
- event.respond error_messages[:user_not_logged_in] if user.nil?
62
- event.respond error_messages[:chat_not_found] if user.chats.empty? && user
63
- list_action if user && !user.chats.empty?
65
+ valid_for_list_action? ? list_action : ""
66
+ "OK"
64
67
  end
65
68
  end
66
69
 
@@ -68,10 +71,11 @@ module ChatgptAssistant
68
71
  bot.command :hist do |event|
69
72
  @evnt = event
70
73
  event.respond error_messages[:user_not_logged_in] if user.nil?
71
- title = event.message.content.split(" ")[1..].join(" ")
74
+ title = event.message.content.split[1..].join(" ")
72
75
  @chat = user.chat_by_title(title)
73
76
  event.respond error_messages[:chat_not_found] if chat.nil? && user
74
77
  hist_action if user && chat
78
+ "OK"
75
79
  end
76
80
  end
77
81
 
@@ -79,6 +83,7 @@ module ChatgptAssistant
79
83
  bot.command :help do |event|
80
84
  @evnt = event
81
85
  help_action
86
+ "OK"
82
87
  end
83
88
  end
84
89
 
@@ -88,27 +93,30 @@ module ChatgptAssistant
88
93
  @user = find_user(discord_id: event.user.id)
89
94
  event.respond error_messages[:user_not_logged_in] if user.nil?
90
95
  create_chat_action if user
96
+ "OK"
91
97
  end
92
98
  end
93
99
 
94
100
  def sl_chat_event
95
101
  bot.command :sl_chat do |event|
96
102
  @evnt = event
97
- chat_to_select = event.message.content.split(" ")[1..].join(" ")
103
+ chat_to_select = event.message.content.split[1..].join(" ")
98
104
  @user = find_user(discord_id: event.user.id)
99
105
  event.respond error_messages[:user_not_logged_in] if user.nil?
100
106
 
101
107
  sl_chat_action(chat_to_select) if user
108
+ "OK"
102
109
  end
103
110
  end
104
111
 
105
112
  def ask_event
106
113
  bot.command :ask do |event|
107
114
  @evnt = event
108
- @message = event.message.content.split(" ")[1..].join(" ")
115
+ @message = event.message.content.split[1..].join(" ")
109
116
  @user = find_user(discord_id: event.user.id)
110
117
  event.respond error_messages[:user_not_logged_in] if user.nil?
111
118
  ask_action if user
119
+ "OK"
112
120
  end
113
121
  end
114
122
 
@@ -116,10 +124,17 @@ module ChatgptAssistant
116
124
  bot.command :connect do |event|
117
125
  @evnt = event
118
126
  @user = find_user(discord_id: event.user.id)
119
- @chat = Chat.where(id: user.current_chat_id).last
120
- voice_connect_checker_action
121
- voice_connection_checker_action
122
- discord_voice_bot_connect
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"
123
138
  end
124
139
  end
125
140
 
@@ -129,18 +144,33 @@ module ChatgptAssistant
129
144
  @user = find_user(discord_id: event.user.id)
130
145
  disconnect_checker_action
131
146
  disconnect_action if user && event.user.voice_channel && event.voice
147
+ "OK"
132
148
  end
133
149
  end
134
150
 
135
151
  def speak_event
136
152
  bot.command :speak do |event|
137
153
  @evnt = event
138
- @message = event.message.content.split(" ")[1..].join(" ")
154
+ @message = event.message.content.split[1..].join(" ")
139
155
  @user = find_user(discord_id: event.user.id)
140
156
  @chat = user.current_chat
141
157
  speak_connect_checker_action
142
158
  speak_connection_checker_action
143
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"
144
174
  end
145
175
  end
146
176
 
@@ -18,7 +18,7 @@ module ChatgptAssistant
18
18
 
19
19
  hash = new_access.password_hash
20
20
  salt = new_access.password_salt
21
- valid_password?(password, hash, salt) ? telegram_user_access(visitor_access, new_access) : "wrong password"
21
+ valid_password?(password, hash, salt) ? telegram_user_access(_access, new_access) : "wrong password"
22
22
  end
23
23
 
24
24
  def telegram_user_access(visitor, new_access)
@@ -37,11 +37,11 @@ module ChatgptAssistant
37
37
  end
38
38
 
39
39
  def discord_user_access(discord_id, user_email)
40
- last_access = find_user(discord_id: discord_id)
41
- new_access = find_user(email: user_email)
42
- last_acess.update(discord_id: nil) if last_access&.email != new_access&.email
43
- new_access&.update(discord_id: discord_id)
44
- new_access.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
45
  end
46
46
  end
47
47
  end
@@ -68,6 +68,13 @@ module ChatgptAssistant
68
68
  (message.save ? answer_action : send_message(error_messages[:message_not_saved])) if chat
69
69
  end
70
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
+
71
78
  def sl_chat_action(chat_to_select)
72
79
  @chat = user.chat_by_title(chat_to_select)
73
80
  send_message error_messages[:chat_not_found] if chat.nil?
@@ -76,13 +83,28 @@ module ChatgptAssistant
76
83
  end
77
84
 
78
85
  def create_chat_action
79
- chat_title = evnt.message.content.split(" ")[1..].join(" ")
80
- @chat = Chat.new(user_id: user.id, title: chat_title, status: 0)
81
- chat.save ? respond_with_success : send_message(error_messages[:chat_creation])
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])
82
104
  end
83
105
 
84
106
  def answer_action
85
- response = chatter.chat(message.content, chat.id)
107
+ response = chatter.chat(message.content, chat.id, error_messages[:something_went_wrong])
86
108
  send_message response
87
109
  end
88
110
 
@@ -25,7 +25,7 @@ module ChatgptAssistant
25
25
 
26
26
  def ask_to_speak_action
27
27
  Message.create(chat_id: chat.id, content: message, role: "user")
28
- response = chatter.chat(message, chat.id)
28
+ response = chatter.chat(message, chat.id, error_messages[:something_went_wrong])
29
29
  audio_path = synthesis.synthesize_text(response)
30
30
  speak_answer_action(audio_path, response)
31
31
  end
@@ -3,12 +3,5 @@
3
3
  module ChatgptAssistant
4
4
  # Helper for logger action
5
5
  module LoggerActionHelper
6
- def register_visitor_action(action, visitor_id)
7
- VisitorAction.create(visitor_id: visitor_id, action: action, description: msg.text)
8
- end
9
-
10
- def register_user_action(action, user_id)
11
- UserAction.create(user_id: user_id, action: action, description: msg.text)
12
- end
13
6
  end
14
7
  end
@@ -4,13 +4,17 @@ module ChatgptAssistant
4
4
  # Helper for messenger
5
5
  module MessengerHelper
6
6
  def chat_success(chat_id)
7
- Message.create(chat_id: chat_id, content: msg.text, role: "user")
8
- text = chatter.chat(msg.text, chat_id)
9
- mess = parse_message(text, 4096)
10
- mess.each { |m| send_message m, msg.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
11
15
  end
12
16
 
13
- def respond_with_success
17
+ def respond_with_success(chat)
14
18
  user.update(current_chat_id: chat.id)
15
19
  send_message success_messages[:chat_created]
16
20
  end
@@ -30,11 +34,13 @@ module ChatgptAssistant
30
34
  end
31
35
 
32
36
  def telegram_send_message(text, chat_id)
33
- bot.api.send_message(chat_id: chat_id, text: text)
37
+ messages = parse_message(text, 4096)
38
+ messages.each { |m| bot.api.send_message(chat_id: chat_id, text: m) }
34
39
  end
35
40
 
36
41
  def discord_send_message(text)
37
- evnt.respond text
42
+ messages = parse_message(text, 2000)
43
+ messages.each { |m| evnt.respond m }
38
44
  end
39
45
 
40
46
  def discord_help_message
@@ -46,85 +52,81 @@ module ChatgptAssistant
46
52
  end
47
53
 
48
54
  def user_logged_message
49
- register_user_action("login", user.id)
50
55
  user.update(telegram_id: msg.chat.id)
51
56
  bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:user_logged_in])
52
57
  evnt&.respond success_messages[:user_logged_in] if evnt.present?
53
58
  end
54
59
 
55
60
  def invalid_command_error_message
56
- register_user_action("invalid_command", user.id) if user
57
- register_visitor_action("invalid_command", visitor.id) if visitor&.tel_user.nil?
58
61
  bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:invalid_command])
59
62
  evnt&.respond error_messages[:invalid_command]
60
63
  end
61
64
 
62
65
  def user_not_found_error_message
63
- register_visitor_action("error: user not found", visitor.id)
64
66
  bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:user_not_found]) if bot.api.present?
65
67
  evnt&.respond error_messages[:user_not_found] if evnt.present?
66
68
  end
67
69
 
68
70
  def user_created_message
69
- register_visitor_action("user_created", visitor.id)
70
71
  bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:user_created]) if bot.api.present?
71
72
  evnt&.respond success_messages[:user_created] if evnt.present?
72
73
  end
73
74
 
74
75
  def user_creation_error_message
75
- register_visitor_action("error: user creation", visitor.id)
76
76
  bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:user_creation]) if bot.api.present?
77
77
  evnt&.respond error_messages[:user_creation] if evnt.present?
78
78
  end
79
79
 
80
80
  def chat_created_message(chat)
81
- register_user_action("chat_created", user.id) if user
82
81
  user&.update(current_chat_id: chat.id)
83
- bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:chat_created]) if bot.api.present?
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?
84
97
  end
85
98
 
86
99
  def not_logged_in_message
87
- register_visitor_action("error: not logged in", visitor.id) if visitor_user?
88
100
  bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:user_not_logged_in])
89
101
  evnt&.respond(error_messages[:user_not_logged_in])
90
102
  end
91
103
 
92
104
  def wrong_password_error_message
93
- register_visitor_action("error: wrong password", visitor.id) if visitor_user?
94
105
  bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:password])
95
106
  evnt&.respond(error_messages[:password])
96
107
  end
97
108
 
98
109
  def chat_not_found_message
99
- register_visitor_action("error: visitors cant select chat", visitor.id) if visitor_user?
100
- register_user_action("error: chat not found", user.id) if user
101
110
  bot.api&.send_message(chat_id: msg.chat.id, text: error_messages[:chat_not_found])
102
111
  evnt&.respond(error_messages[:chat_not_found])
103
112
  end
104
113
 
105
114
  def no_chat_selected_message
106
- register_visitor_action("error: visitors cant select chat", visitor.id) if visitor_user?
107
- register_user_action("error: no chat selected", user.id) if user
108
115
  bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:no_chat_selected])
109
116
  evnt&.respond(error_messages[:no_chat_selected])
110
117
  end
111
118
 
112
119
  def no_messages_founded_message
113
- register_visitor_action("error: visitors cant view the chat history", visitor.id) if visitor_user?
114
- register_user_action("error: no messages founded", user.id) if user
115
120
  bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:no_messages_founded])
116
121
  evnt&.respond(error_messages[:no_messages_founded])
117
122
  end
118
123
 
119
124
  def chat_creation_failed_message
120
- register_visitor_action("error: chat creation failed", visitor.id) if visitor_user?
121
- register_user_action("error: chat creation failed", user.id) if user
122
125
  bot&.api&.send_message(chat_id: msg.chat.id, text: error_messages[:chat_creation_failed])
123
126
  evnt&.respond(error_messages[:chat_creation_failed])
124
127
  end
125
128
 
126
129
  def user_logged_in_message
127
- register_visitor_action("user_logged_in", visitor.id) if visitor
128
130
  bot.api&.send_message(chat_id: msg.chat.id, text: success_messages[:user_logged_in])
129
131
  evnt&.respond(success_messages[:user_logged_in])
130
132
  end
@@ -15,33 +15,6 @@ module ChatgptAssistant
15
15
  user ? chat_if_exists : not_logged_in_message
16
16
  end
17
17
 
18
- def telegram_audio_info
19
- bot.api.get_file(file_id: telegram_audio.file_id)
20
- end
21
-
22
- def telegram_audio_url
23
- "https://api.telegram.org/file/bot#{telegram_token}/#{telegram_audio_info["result"]["file_path"]}"
24
- end
25
-
26
- def telegram_audio
27
- msg.audio || msg.voice
28
- end
29
-
30
- def telegram_process_ai_voice(user_audio)
31
- ai_text = chatter.chat(user_audio["text"], user.current_chat_id)
32
- ai_voice = synthesis.synthesize_text(ai_text)
33
- {
34
- voice: ai_voice,
35
- text: ai_text
36
- }
37
- end
38
-
39
- def telegram_send_voice_message(voice: nil, text: nil)
40
- messages = parse_message text, 4096
41
- bot.api.send_voice(chat_id: msg.chat.id, voice: Faraday::UploadIO.new(voice, "audio/mp3"))
42
- messages.each { |message| send_message message, msg.chat.id }
43
- end
44
-
45
18
  def telegram_user_create(visitor_id, email, password)
46
19
  visitor = Visitor.find_by(id: visitor_id)
47
20
  return false unless visitor
@@ -62,7 +35,22 @@ module ChatgptAssistant
62
35
  def telegram_create_chat
63
36
  text = msg.text
64
37
  title = text.split("/").last
65
- chat = Chat.new(user_id: user.id, status: 0, title: title)
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
+
66
54
  chat.save ? chat_created_message(chat) : chat_creation_failed_message
67
55
  end
68
56
 
@@ -0,0 +1,33 @@
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