telegram-support-bot 0.1.03 → 0.1.04

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: 8d61645170c11693b5022efec80d4a7e439ab8378c377ed010d121c0429518be
4
- data.tar.gz: 565dc25a57ed67dbd428c79b8e08bc7682a537d9ba467f918f646ff03db412a2
3
+ metadata.gz: 7136195dca0f9287a9534c5ef4004610d897a4b1033e5a45505826e472c927d8
4
+ data.tar.gz: 80556619e8db83f143d445df3ffb033d3f72838bb5bb69d4182c8379c441e522
5
5
  SHA512:
6
- metadata.gz: d17862439a4fb473284529a3420e69e47ab114418b4b166bdb77bcef4dbc578e25b1fbe768f87c3e9c53be566145221322c04e4eee2b4c83d26d2fb5b458c8b3
7
- data.tar.gz: 296405e4807e520564eb679086343c8b1f45e95c1148911b37a9a16157e8a6333f71e949912ac1b4568e62d6fc2f4a302212fa0b4d9b9a1268e1d56e1fa52a69
6
+ metadata.gz: d40c42dcca1f66a0a3c9fb9d563371699969a26a58d6719b49097c53f645aec5af86c5d1794a04d8e69cb0f90bb4e46cfeab8b3f1e2d74137c4dd0701b7e2350
7
+ data.tar.gz: 2c5b4fd82fa459cb6f8aed6004af0f7f2fff1a7958b32e3f947b7be45a965b5d445a5a00b52c94c2a37887b91208fee213522c48146d690b590eb9bcf437de59
data/README.md CHANGED
@@ -1,60 +1,138 @@
1
1
  # TelegramSupportBot
2
2
 
3
3
  ## Introduction
4
- `TelegramSupportBot` is a Ruby gem designed to enhance customer support through Telegram, allowing for the management of queries directly from a designated chat while ensuring privacy and confidentiality.
4
+
5
+ `TelegramSupportBot` is a Ruby gem designed to enhance customer support through Telegram, allowing
6
+ for the management of queries directly from a designated chat while ensuring privacy and
7
+ confidentiality.
5
8
 
6
9
  ## Features
10
+
7
11
  - Forward messages between users and a support chat
8
12
  - Supports various message types (text, images, videos, documents, audio, stickers)
9
13
  - Auto-away messages for off-hours
10
14
  - Simple configuration and deployment
11
15
 
12
16
  ## Installation
17
+
13
18
  Add to your Gemfile:
19
+
14
20
  ```ruby
15
21
  gem 'telegram-support-bot'
16
22
  ```
17
23
 
18
24
  ## Usage
19
- ### Webhook Integration
20
25
 
21
- #### Creating and Configuring Your Bot
26
+ ### Creating and Configuring Your Bot
27
+
22
28
  1. **Create Your Bot** via BotFather on Telegram to get your bot token.
23
- 2. **Deploy Your Application** and set up a controller action for webhook callbacks, directing them to `TelegramSupportBot.process_update`.
29
+ 2. **Deploy Your Application** and set up a controller action for webhook callbacks, directing them
30
+ to `TelegramSupportBot.process_update`.
24
31
  3. **Set the Webhook URL** using the Telegram Bot API to your controller action.
25
32
 
26
- #### Setting Up Your Telegram Bot
27
- 1. **Add Your Bot to a Support Chat** and obtain the `support_chat_id` by sending the `/start` command to the bot inside the support chat.
28
- 2. **Configure Your Bot** in your Ruby application with the token and `support_chat_id`, and set a welcome message.
33
+ ### Setting Up Your Telegram Bot
34
+
35
+ 1. **Add Your Bot to a Support Chat** and obtain the `support_chat_id` by sending the `/start`
36
+ command to the bot inside the support chat.
37
+ 2. **Configure Your Bot** in your Ruby application with the token and `support_chat_id`, and set a
38
+ welcome message.
29
39
 
30
40
  ```ruby
31
41
  TelegramSupportBot.configure do |config|
32
- config.adapter = :telegram_bot
42
+ config.adapter = :telegram_bot
33
43
  config.adapter_options = { token: 'YOUR_TELEGRAM_BOT_TOKEN' }
34
44
  config.support_chat_id = 'YOUR_SUPPORT_CHAT_ID'
35
45
  config.welcome_message = 'Hi! How can we help you?'
36
46
  end
37
47
  ```
38
48
 
39
- 3. **Interact with Users**: Messages to your bot will be forwarded to the support chat, and replies in the chat will be sent back to the users.
49
+ 3. **Interact with Users**: Messages to your bot will be forwarded to the support chat, and replies
50
+ in the chat will be sent back to the users.
51
+
52
+ ## Adapters
53
+
54
+ `TelegramSupportBot` supports integration through adapters. Currently, `telegram-bot`
55
+ and `telegram-bot-ruby` are supported.
56
+
57
+ Configuration is pretty much the same for both gems:
58
+
59
+ ```ruby
60
+ TelegramSupportBot.configure do |config|
61
+ config.adapter = :telegram_bot
62
+ config.adapter_options = { token: 'YOUR_TELEGRAM_BOT_TOKEN' }
63
+ end
64
+ ```
65
+
66
+ ```ruby
67
+ TelegramSupportBot.configure do |config|
68
+ config.adapter = :telegram_bot_ruby
69
+ config.adapter_options = { token: 'YOUR_TELEGRAM_BOT_TOKEN' }
70
+ end
71
+ ```
72
+
73
+ ## Examples
40
74
 
41
- ### Adapters
42
- `TelegramSupportBot` supports integration through adapters. Currently, `telegram-bot` is supported with plans to add `telegram-bot-ruby`.
75
+ **Basically, just make sure you call `TelegramSupportBot.process_update` somewhere in you workflow
76
+ cycle and pass it a parsed json update received from Telegram servers.**
77
+
78
+ ### Using `telegram-bot` Gem with a Webhook Controller
79
+
80
+ If you're using the `telegram-bot` gem, set up a Rails controller to handle incoming webhook
81
+ requests. Here's an example of how you might implement such a controller:
82
+
83
+ ```ruby
84
+
85
+ class TelegramWebhooksController < ApplicationController
86
+
87
+ def webhook
88
+ update = JSON.parse(request.body.read)
89
+ TelegramSupportBot.process_update(update)
90
+ head :ok
91
+ end
92
+ end
93
+ ```
94
+
95
+ Make sure to configure your routes to direct webhook callbacks to this controller action.
96
+
97
+ ### Using `telegram-bot-ruby` Gem with bot.listen
98
+
99
+ For those utilizing telegram-bot-ruby, you can set up a simple listener loop to process incoming
100
+ messages. This approach is more suited for polling rather than webhooks:
101
+ require 'telegram/bot'
102
+
103
+ ```ruby
104
+ token = 'YOUR_TELEGRAM_BOT_TOKEN'
105
+
106
+ Telegram::Bot::Client.run(token) do |bot|
107
+ bot.listen do |message|
108
+ TelegramSupportBot.process_update(update.to_h)
109
+ end
110
+ end
111
+ ```
112
+
113
+ ## Custom Adapter Implementation
114
+
115
+ Implement custom adapters by inheriting from `TelegramSupportBot::Adapter::Base` and defining
116
+ message sending and forwarding methods.
43
117
 
44
- #### Custom Adapter Implementation
45
- Implement custom adapters by inheriting from `TelegramSupportBot::Adapter::Base` and defining message sending and forwarding methods.
46
118
 
47
119
  ## Development
120
+
48
121
  - Run `bin/setup` to install dependencies.
49
122
  - Use `rake spec` for tests and `bin/console` for an interactive prompt.
50
123
  - To install locally, use `bundle exec rake install`.
51
124
  - For releases, update `version.rb`, and run `bundle exec rake release`.
52
125
 
53
126
  ## Contributing
54
- Contributions are welcome via GitHub, adhering to the [code of conduct](https://github.com/austerlitz/telegram_support_bot/blob/main/CODE_OF_CONDUCT.md).
127
+
128
+ Contributions are welcome via GitHub, adhering to
129
+ the [code of conduct](https://github.com/austerlitz/telegram_support_bot/blob/main/CODE_OF_CONDUCT.md).
55
130
 
56
131
  ## License
132
+
57
133
  Available under the [MIT License](https://opensource.org/licenses/MIT).
58
134
 
59
135
  ## Code of Conduct
60
- Follow the project's [code of conduct](https://github.com/austerlitz/telegram_support_bot/blob/main/CODE_OF_CONDUCT.md).
136
+
137
+ Follow the
138
+ project's [code of conduct](https://github.com/austerlitz/telegram_support_bot/blob/main/CODE_OF_CONDUCT.md).
@@ -3,6 +3,7 @@
3
3
  module TelegramSupportBot
4
4
  module Adapters
5
5
  class Base
6
+
6
7
  attr_reader :bot
7
8
 
8
9
  def initialize(**options)
@@ -19,7 +20,15 @@ module TelegramSupportBot
19
20
  end
20
21
 
21
22
  def send_media(chat_id:, type:, media:, **options)
22
- # send photos, videos, documents
23
+ method = "send_#{type}"
24
+ args = { chat_id: chat_id, **options }
25
+ args[type] = media
26
+
27
+ if respond_to?(method, true)
28
+ send(method, **args)
29
+ else
30
+ raise ArgumentError, "Unsupported media type: #{type}"
31
+ end
23
32
  end
24
33
 
25
34
  def forward_message(from_chat_id:, chat_id:, message_id:)
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'telegram/bot'
3
4
  module TelegramSupportBot
4
5
  module Adapters
5
6
  class TelegramBot < Base
@@ -13,6 +14,8 @@ module TelegramSupportBot
13
14
  @bot.send_message(chat_id: chat_id, text: text, **options)
14
15
  end
15
16
 
17
+ alias_method :send_text, :send_message
18
+
16
19
  def send_photo(chat_id:, photo:, **options)
17
20
  @bot.send_photo(chat_id: chat_id, photo: photo, **options)
18
21
  end
@@ -29,6 +32,7 @@ module TelegramSupportBot
29
32
  def send_audio(chat_id:, audio:, **options)
30
33
  @bot.send_audio(chat_id: chat_id, audio: audio, **options)
31
34
  end
35
+
32
36
  # Handles sending audio messages
33
37
  def send_voice(chat_id:, voice:, **options)
34
38
  @bot.send_voice(chat_id: chat_id, voice: voice, **options)
@@ -39,30 +43,6 @@ module TelegramSupportBot
39
43
  @bot.send_sticker(chat_id: chat_id, sticker: sticker, **options)
40
44
  end
41
45
 
42
- # New generic send_media method
43
- def send_media(chat_id:, type:, media:, **options)
44
- case type.to_sym
45
- when :text
46
- send_message(chat_id: chat_id, text: media, **options)
47
- when :photo
48
- send_photo(chat_id: chat_id, photo: media, **options)
49
- when :video
50
- send_video(chat_id: chat_id, video: media, **options)
51
- when :video_note
52
- send_video_note(chat_id: chat_id, video_note: media, **options)
53
- when :document
54
- send_document(chat_id: chat_id, document: media, **options)
55
- when :audio
56
- send_audio(chat_id: chat_id, audio: media, **options)
57
- when :voice
58
- send_voice(chat_id: chat_id, voice: media, **options)
59
- when :sticker
60
- send_sticker(chat_id: chat_id, sticker: media, **options)
61
- else
62
- raise ArgumentError, "Unsupported media type: #{type}"
63
- end
64
- end
65
-
66
46
  def forward_message(from_chat_id:, chat_id:, message_id:)
67
47
  @bot.forward_message(
68
48
  chat_id: chat_id,
@@ -1,8 +1,48 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'telegram/bot'
3
4
  module TelegramSupportBot
4
5
  module Adapters
5
- class TelegramBotRuby
6
+ class TelegramBotRuby < Base
7
+
8
+ def initialize(**options)
9
+ super
10
+ @bot = Telegram::Bot::Client.new(@options[:token])
11
+ end
12
+
13
+ def send_message(chat_id:, text:, **options)
14
+ bot.api.send_message(chat_id: chat_id, text: text, **options)
15
+ end
16
+
17
+ alias_method :send_text, :send_message
18
+
19
+ def send_photo(chat_id:, photo:, **options)
20
+ bot.api.send_photo(chat_id: chat_id, photo: photo, **options)
21
+ end
22
+
23
+ def send_video(chat_id:, video:, **options)
24
+ bot.api.send_video(chat_id: chat_id, video: video, **options)
25
+ end
26
+
27
+ def send_document(chat_id:, document:, **options)
28
+ bot.api.send_document(chat_id: chat_id, document: document, **options)
29
+ end
30
+
31
+ def send_audio(chat_id:, audio:, **options)
32
+ bot.api.send_audio(chat_id: chat_id, audio: audio, **options)
33
+ end
34
+
35
+ def send_voice(chat_id:, voice:, **options)
36
+ bot.api.send_voice(chat_id: chat_id, voice: voice, **options)
37
+ end
38
+
39
+ def send_sticker(chat_id:, sticker:, **options)
40
+ bot.api.send_sticker(chat_id: chat_id, sticker: sticker, **options)
41
+ end
42
+
43
+ def forward_message(from_chat_id:, chat_id:, message_id:)
44
+ bot.api.forward_message(chat_id: chat_id, from_chat_id: from_chat_id, message_id: message_id)
45
+ end
6
46
  end
7
47
  end
8
48
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TelegramSupportBot
4
- VERSION = "0.1.03"
4
+ VERSION = "0.1.04"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telegram-support-bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.03
4
+ version: 0.1.04
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Buslaev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-25 00:00:00.000000000 Z
11
+ date: 2024-02-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |-
14
14
  The telegram_support_bot gem provides Rails applications with an