Class: XNM::Telegram::Chat
- Inherits:
-
Object
- Object
- XNM::Telegram::Chat
- Defined in:
- lib/xnm/telegram/Chat.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#casual_name ⇒ Object
readonly
Return a more human-friendly name for the chat.
-
#chat_id ⇒ Object
readonly
Returns the value of attribute chat_id.
-
#chat_obj ⇒ Object
readonly
Returns the value of attribute chat_obj.
-
#on_telegram_event ⇒ Object
readonly
Returns the value of attribute on_telegram_event.
-
#str_id ⇒ Object
readonly
Returns the value of attribute str_id.
Instance Method Summary collapse
-
#initialize(handler, chat_info) ⇒ Chat
constructor
Initialize a chat object.
-
#on_command(command, **options, &block) ⇒ Object
Add a command callback.
-
#on_message(regexp = nil, &block) ⇒ Object
Add a new message callback.
-
#send_message(text, **options) ⇒ Object
Send a message to this chat.
-
#tg_mention ⇒ Object
Return a Telegram mention link.
- #to_i ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(handler, chat_info) ⇒ Chat
Initialize a chat object. Call this to generate a new chat object. It will always have to be called with a Chat object, as returned by a message’s “chat” field or the getChat function
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/xnm/telegram/Chat.rb', line 18 def initialize(handler, chat_info) @handler = handler; @chat_id = chat_info[:id]; @str_id = chat_info[:username] || chat_info[:title] @casual_name = @str_id; @chat_obj = chat_info; @on_telegram_event = [] end |
Instance Attribute Details
#casual_name ⇒ Object (readonly)
Return a more human-friendly name for the chat.
74 75 76 |
# File 'lib/xnm/telegram/Chat.rb', line 74 def casual_name @casual_name end |
#chat_id ⇒ Object (readonly)
Returns the value of attribute chat_id.
5 6 7 |
# File 'lib/xnm/telegram/Chat.rb', line 5 def chat_id @chat_id end |
#chat_obj ⇒ Object (readonly)
Returns the value of attribute chat_obj.
10 11 12 |
# File 'lib/xnm/telegram/Chat.rb', line 10 def chat_obj @chat_obj end |
#on_telegram_event ⇒ Object (readonly)
Returns the value of attribute on_telegram_event.
12 13 14 |
# File 'lib/xnm/telegram/Chat.rb', line 12 def on_telegram_event @on_telegram_event end |
#str_id ⇒ Object (readonly)
Returns the value of attribute str_id.
6 7 8 |
# File 'lib/xnm/telegram/Chat.rb', line 6 def str_id @str_id end |
Instance Method Details
#on_command(command, **options, &block) ⇒ Object
Add a command callback. Equivalent to Handler#on_command, but will only be called on commands issued in this chat.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/xnm/telegram/Chat.rb', line 54 def on_command(command, **, &block) raise ArgumentError, 'Block must be given!' unless block_given? [:block] = block [:command] = command out_evt = OnCommand.new(); @on_telegram_event << out_evt out_evt end |
#on_message(regexp = nil, &block) ⇒ Object
Add a new message callback. Similar to the Handler’s function, but only applies to this chat’s messages. Especially nice for one-on-one bots.
42 43 44 45 46 47 48 49 |
# File 'lib/xnm/telegram/Chat.rb', line 42 def (regexp = nil, &block) raise ArgumentError, 'Block must be given!' unless block_given? out_evt = OnMessage.new({ block: block, regexp: regexp }); @on_telegram_event << out_evt out_evt end |
#send_message(text, **options) ⇒ Object
Send a message to this chat.
34 35 36 |
# File 'lib/xnm/telegram/Chat.rb', line 34 def (text, **) @handler.(self, text, **); end |
#tg_mention ⇒ Object
Return a Telegram mention link. Can be inserted into a Telegram HTML formatted message, and allows people to click on the name.
69 70 71 |
# File 'lib/xnm/telegram/Chat.rb', line 69 def tg_mention "<a href=\"tg://user?id=#{@chat_id}\">@#{@str_id}</a>" end |
#to_i ⇒ Object
78 79 80 |
# File 'lib/xnm/telegram/Chat.rb', line 78 def to_i @chat_id end |
#to_s ⇒ Object
82 83 84 |
# File 'lib/xnm/telegram/Chat.rb', line 82 def to_s @casual_name end |