open_ai_bot 0.3.6 → 0.3.7

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: 896b6e1a360e307f7fb4503460352c0ab88047f96250c0b43fb7ab14d9f536ec
4
- data.tar.gz: 123e1e25af6b26cda675eacde7f6fb442aefe49a726cd2a70a6d0e3aa95da0d1
3
+ metadata.gz: 0522c533c52a78ca5524ce185ccd858e3534369d854d3049d77c02bc8a7810cc
4
+ data.tar.gz: c2fd62a33367bba69fbf6b14b26c17521ffbbc4ce1d8ace5b6d96572c25da4f7
5
5
  SHA512:
6
- metadata.gz: 01b08599203bddae71d05930245411d19dc9b0d1cda04fcfda252ea6ed80d1866240c1b0c63608246d37dd153e65e5eb1762ce9f9798a8583286a650319ab23f
7
- data.tar.gz: b1eaf9099cf973ee5726e31ba2e58ae3f8e51f8d1386c38c7aa8dc92ee339213d1c764bd3549cc563ab1f3d70ec6cf5b3a45ed836327e56e8b89c21e0bd09bf4
6
+ metadata.gz: 436d2fe4a71f0b626cc147d4a868af91c46c7f7bca9c1a11440995e0924947032144f876926d2e143522b5cc06e8f8bb748d1ec6b8152b610f1dfb95cc23d17f
7
+ data.tar.gz: ff27d05381553aecf54eaddfd5b5bb4d1cbe7e3187f4eb4260a742efd7d08517c331f0fec2cb184077f1c11be6a332ac5fd4f053812e8bcc6535b3b509978ab0
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ConfigMenu
4
+ def self.included(base)
5
+ base.on_every_message :handle_config_query
6
+ base.on_command "/config" do
7
+ $original_config_message_id = @message.id
8
+ $config_message_id = reply("clicky buttons:", reply_markup: config_menu).dig("result", "message_id")
9
+ end
10
+ end
11
+
12
+ def model_selection_menu
13
+ options = []
14
+ OpenAI::Model::MODEL_INFO.each do |model, info|
15
+ options << [
16
+ Telegram::Bot::Types::InlineKeyboardButton.new(
17
+ text: "#{model} - #{sprintf('%.2f', info[:output_price])}$",
18
+ callback_data: "/set_model #{model}"
19
+ )
20
+ ]
21
+ end
22
+
23
+ options << [
24
+ Telegram::Bot::Types::InlineKeyboardButton.new(
25
+ text: "<< Go back",
26
+ callback_data: "/go_back"
27
+ )
28
+ ]
29
+
30
+ Telegram::Bot::Types::InlineKeyboardMarkup.new(inline_keyboard: options)
31
+ end
32
+
33
+ def config_menu
34
+ buttons = {
35
+ "Select model >>" => "/get_model_menu",
36
+ "Restart" => "/restart",
37
+ "Toggle price info" => "/toggle_price_info",
38
+ "Done" => "/config_done"
39
+ }
40
+
41
+ Telegram::Bot::Types::InlineKeyboardMarkup.new(
42
+ inline_keyboard: buttons.map { |k, v|
43
+ [Telegram::Bot::Types::InlineKeyboardButton.new(text: k, callback_data: v)]
44
+ }
45
+ )
46
+ end
47
+
48
+ def handle_config_query
49
+ return unless @update.is_a? Telegram::Bot::Types::CallbackQuery
50
+ return unless @user.username == config.owner_username
51
+
52
+ case @update.data
53
+ when "/get_model_menu"
54
+ @api.edit_message_text(chat_id: @chat.id, message_id: $config_message_id, reply_markup: model_selection_menu, text: "Select model")
55
+ when "/restart"
56
+ init_session
57
+ when "/toggle_price_info"
58
+ if config.respond_to?(:show_price_info)
59
+ config.show_price_info = !config.show_price_info
60
+ else
61
+ config.show_price_info = false
62
+ end
63
+ send_message("Now #{config.show_price_info ? "" : "NOT "}showing price info")
64
+ when "/go_back"
65
+ @api.edit_message_text(chat_id: @chat.id, message_id: $config_message_id, reply_markup: config_menu, text: "Config menu")
66
+ when "/config_done"
67
+ safe_delete_by_id($config_message_id, from_bot: true)
68
+ safe_delete_by_id($original_config_message_id)
69
+ else
70
+ handle_model_query
71
+ end
72
+ end
73
+
74
+ def handle_model_query
75
+ return unless @update.is_a? Telegram::Bot::Types::CallbackQuery
76
+ return unless @update.data.start_with? "/set_model "
77
+ return unless @user.username == config.owner_username
78
+
79
+ model = @update.data.delete_prefix("/set_model ").to_sym
80
+ return if OpenAI::Model::MODEL_INFO[model].nil?
81
+
82
+ text =
83
+ if current_thread.model.to_sym == model
84
+ "Already set to `#{model}`"
85
+ else
86
+ "Was `#{current_thread.model.to_s}`, now `#{model}`"
87
+ end
88
+
89
+ current_thread.model = OpenAI::Model.new(model)
90
+
91
+ send_message(text, parse_mode: "Markdown")
92
+ end
93
+ end
@@ -118,6 +118,7 @@ module OpenAI
118
118
  def send_request!
119
119
  send_chat_action(:typing)
120
120
 
121
+ # todo: rewrite in async
121
122
  response = open_ai.chat(
122
123
  parameters: {
123
124
  model: current_thread.model.to_s,
@@ -154,6 +155,7 @@ module OpenAI
154
155
 
155
156
  def send_chat_gpt_response(text, tokens_info)
156
157
  tokens_text = tokens_info[:info]
158
+ tokens_text = '' if config.show_price_info == false
157
159
 
158
160
  id = reply(text + tokens_text).dig("result", "message_id")
159
161
 
data/lib/open_ai_bot.rb CHANGED
@@ -12,14 +12,16 @@ require_relative "open_ai/image"
12
12
  require_relative "ext/blank"
13
13
  require_relative "ext/in"
14
14
 
15
+ require_relative "config_menu"
16
+
15
17
  class OpenAIBot < Rubydium::Bot
16
18
  include OpenAI::ChatGPT
17
19
  include OpenAI::Dalle
18
20
  include OpenAI::Utils
19
21
  include OpenAI::Whisper
22
+ include ConfigMenu
20
23
 
21
24
  on_every_message :handle_gpt_command
22
- on_every_message :handle_model_query
23
25
  on_every_message :transcribe
24
26
 
25
27
  on_command "/restart", :init_session, description: "Resets ChatGPT session"
@@ -37,37 +39,11 @@ class OpenAIBot < Rubydium::Bot
37
39
  safe_delete(@msg)
38
40
  end
39
41
 
40
- on_command "/model" do
41
- options = []
42
- OpenAI::Model::MODEL_INFO.each do |model, info|
43
- options << [
44
- Telegram::Bot::Types::InlineKeyboardButton.new(
45
- text: "#{model} - #{sprintf('%.2f', info[:output_price])}$",
46
- callback_data: "/set #{model}"
47
- )
48
- ]
49
- end
50
- markup = Telegram::Bot::Types::InlineKeyboardMarkup.new(inline_keyboard: options)
51
- reply("Select a model:", reply_markup: markup)
52
- end
53
-
54
- def handle_model_query
55
- return unless @update.is_a? Telegram::Bot::Types::CallbackQuery
56
- return unless @update.data.start_with? "/set "
57
- return unless @user.username == config.owner_username
58
-
59
- model = @update.data.delete_prefix("/set ").to_sym
60
- return if OpenAI::Model::MODEL_INFO[model].nil?
61
42
 
62
- text =
63
- if current_thread.model.to_sym == model
64
- "Already set to `#{model}`"
65
- else
66
- current_thread.model = OpenAI::Model.new(model)
67
- "Was `#{current_thread.model.to_s}`, now `#{model}`"
68
- end
69
-
70
- reply(text, parse_mode: "Markdown")
43
+ on_command "/dbg" do
44
+ must_be_owner do
45
+ binding.irb
46
+ end
71
47
  end
72
48
 
73
49
  def allowed_chat?
data/open_ai_bot.gemspec CHANGED
@@ -8,7 +8,7 @@ require_relative "lib/ext/in"
8
8
 
9
9
  Gem::Specification.new do |spec|
10
10
  spec.name = "open_ai_bot"
11
- spec.version = "0.3.6"
11
+ spec.version = "0.3.7"
12
12
  spec.authors = ["bulgakke"]
13
13
  spec.email = ["vvp835@yandex.ru"]
14
14
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_ai_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - bulgakke
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-06-16 00:00:00.000000000 Z
11
+ date: 2025-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: down
@@ -95,6 +95,7 @@ files:
95
95
  - README.md
96
96
  - config.example.yaml
97
97
  - lib/clean_bot.rb
98
+ - lib/config_menu.rb
98
99
  - lib/ext/blank.rb
99
100
  - lib/ext/in.rb
100
101
  - lib/my_custom_bot.rb