Class: ChatgptAssistant::NewChatService

Inherits:
Object
  • Object
show all
Defined in:
lib/chatgpt_assistant/bots/services/new_chat_service.rb

Overview

This class is responsible to background the new chat service

Instance Method Summary collapse

Constructor Details

#initialize(chat_title, user_id, chat_id, config) ⇒ NewChatService

Returns a new instance of NewChatService.



6
7
8
9
10
11
# File 'lib/chatgpt_assistant/bots/services/new_chat_service.rb', line 6

def initialize(chat_title, user_id, chat_id, config)
  @chat_title = chat_title
  @user_id = user_id
  @chat_id = chat_id
  @config = config
end

Instance Method Details

#actorObject



21
22
23
24
# File 'lib/chatgpt_assistant/bots/services/new_chat_service.rb', line 21

def actor
  @actor_name = actors[@actor_mode.to_i - 1] if @actor_mode
  @actor = AwesomeChatgptActors::Actor.new(role: @actor_name, language: @config.language) if @actor_name
end

#actorsObject



17
18
19
# File 'lib/chatgpt_assistant/bots/services/new_chat_service.rb', line 17

def actors
  @actors ||= AwesomeChatgptActors::CastControl.actors
end

#callObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/chatgpt_assistant/bots/services/new_chat_service.rb', line 38

def call
  raise NilError if @chat_title.nil?
  raise NilError if @user_id.nil?
  raise NilError if @chat_id.nil?

  parse_title

  raise InvalidModeError unless (@actor_mode.to_i >= 1 && @actor_mode.to_i <= actors.size + 1) || @actor_mode.nil?
  raise InvalidChatTitleError if @chat_title.nil? || @chat_title.empty?
  raise ChatAlreadyExistsError if Chat.find_by(title: @chat_title)

  if chat.save
    telegram_async.send_message("Chat created succesfully", @chat_id)
  else
    raise ChatAlreadyExistsError if Chat.find_by(title: @chat_title)
    raise NilError if chat.errors[:title].any?
  end
rescue ChatAlreadyExistsError, NilError => e
  telegram_async.send_message(e.message, @chat_id)
end

#chatObject



26
27
28
29
# File 'lib/chatgpt_assistant/bots/services/new_chat_service.rb', line 26

def chat
  @chat = Chat.new(title: @chat_title, user_id: @user_id, status: 0, actor: @actor_name, prompt: @actor.prompt) if !@actor.nil? && @actor_name
  @chat = Chat.new(title: @chat_title, user_id: @user_id, status: 0) unless @actor_name
end

#parse_titleObject



31
32
33
34
35
36
# File 'lib/chatgpt_assistant/bots/services/new_chat_service.rb', line 31

def parse_title
  return @chat_title.strip! unless @chat_title.include?("actor:")

  @actor_mode = @chat_title.split("actor:")[1].strip
  @chat_title = @chat_title.split("actor:")[0].strip
end

#telegram_asyncObject



13
14
15
# File 'lib/chatgpt_assistant/bots/services/new_chat_service.rb', line 13

def telegram_async
  TelegramBot.new(@config)
end