Module: ChatgptAssistant::Bots::Telegram::Events

Included in:
TelegramBot
Defined in:
lib/chatgpt_assistant/bots/telegram/events.rb

Instance Method Summary collapse

Instance Method Details

#audio_eventObject



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/chatgpt_assistant/bots/telegram/events.rb', line 98

def audio_event
  audio_allowed?
  user_audio = transcribe_file(telegram_audio_url)
  message = Message.new(content: user_audio[:text], chat_id: user.current_chat_id, role: "user")
  raise MessageNotCreatedError unless message.save

  ai_response = telegram_process_ai_voice(user_audio[:file])
  telegram_send_voice_message(voice: ai_response[:voice], text: ai_response[:text])
  delete_file ai_response[:voice]
rescue UserNotLoggedInError, NoChatSelectedError, MessageNotCreatedError, AccountNotVerifiedError => e
  send_message e.message, msg.chat.id
end

#confirm_account_eventObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chatgpt_assistant/bots/telegram/events.rb', line 36

def 
  raise UserNotLoggedInError if user.nil?

   = msg.text.split("/* ").last
  name, token = .split(":")
  send_message "#{name} - #{token}", msg.chat.id
  if user_confirmed?
    send_message success_messages[:account_confirmed], msg.chat.id
  else
    send_message error_messages[:account_not_confirmed], msg.chat.id
  end
rescue UserNotLoggedInError => e
  send_message e.message, msg.chat.id
end

#help_eventObject



11
12
13
# File 'lib/chatgpt_assistant/bots/telegram/events.rb', line 11

def help_event
  help_messages.each { |m| send_message m, msg.chat.id }
end

#hist_eventObject



15
16
17
18
19
20
21
22
23
# File 'lib/chatgpt_assistant/bots/telegram/events.rb', line 15

def hist_event
  hist_allowed?

  user.chat_history.each do |m|
    send_message m, msg.chat.id
  end
rescue NoChatSelectedError, UserNotLoggedInError, NoMessagesFoundedError, AccountNotVerifiedError => e
  send_message e.message, msg.chat.id
end

#list_eventObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/chatgpt_assistant/bots/telegram/events.rb', line 25

def list_event
  list_allowed?

  send_message common_messages[:chat_list], msg.chat.id
  chats_str = ""
  user.chats.each_with_index { |c, i| chats_str += "Chat #{i + 1} - #{c.title}\n" }
  send_message chats_str, msg.chat.id
rescue NoChatsFoundedError, UserNotLoggedInError, AccountNotVerifiedError => e
  send_message e.message, msg.chat.id
end

#login_eventObject



51
52
53
54
55
56
57
58
59
# File 'lib/chatgpt_assistant/bots/telegram/events.rb', line 51

def 
  raise UserLoggedInError if user

   = msg.text.split("/").last
  email, password = .split(":")
  authenticate_user(email, password)
rescue UserNotFoundError, WrongPasswordError, UserLoggedInError => e
  send_message e.message, msg.chat.id
end

#new_chat_eventObject



80
81
82
83
84
85
# File 'lib/chatgpt_assistant/bots/telegram/events.rb', line 80

def new_chat_event
  common_allowed?
  NewChatJob.perform_async(msg.text.split("/").last, user.id, msg.chat.id)
rescue UserNotLoggedInError, AccountNotVerifiedError => e
  send_message e.message, msg.chat.id
end

#register_eventObject



61
62
63
64
65
66
67
68
69
# File 'lib/chatgpt_assistant/bots/telegram/events.rb', line 61

def register_event
   = msg.text.split("/").last
  email, password = register_allowed?()
  name = msg.from.first_name || "Anonymous"

  RegisterJob.perform_async(email, password, name, visitor.telegram_id)
rescue NoRegisterInfoError, UserLoggedInError => e
  send_message e.message, msg.chat.id
end

#select_chat_eventObject



87
88
89
90
91
92
93
94
95
96
# File 'lib/chatgpt_assistant/bots/telegram/events.rb', line 87

def select_chat_event
  common_allowed?
  title = msg.text.split("/").last
  chat = user.chat_by_title(title)
  select_allowed?(chat)

  send_message success_messages[:chat_selected], msg.chat.id
rescue UserNotLoggedInError, ChatNotFoundError, AccountNotVerifiedError => e
  send_message e.message, msg.chat.id
end

#sign_out_eventObject



71
72
73
74
75
76
77
78
# File 'lib/chatgpt_assistant/bots/telegram/events.rb', line 71

def sign_out_event
  raise UserNotLoggedInError if user.nil?

  user.update(telegram_id: nil)
  send_message success_messages[:user_logged_out], msg.chat.id
rescue UserNotLoggedInError => e
  send_message e.message, msg.chat.id
end

#start_eventObject



7
8
9
# File 'lib/chatgpt_assistant/bots/telegram/events.rb', line 7

def start_event
  telegram_send_start_message
end

#telegram_chat_eventObject



111
112
113
114
115
116
# File 'lib/chatgpt_assistant/bots/telegram/events.rb', line 111

def telegram_chat_event
  common_allowed?
  chat_if_exists
rescue UserNotLoggedInError, AccountNotVerifiedError => e
  send_message e.message, msg.chat.id
end