Class: XNM::Telegram::Chat

Inherits:
Object
  • Object
show all
Defined in:
lib/xnm/telegram/Chat.rb

Direct Known Subclasses

User

Instance Attribute Summary collapse

Instance Method Summary collapse

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_nameObject (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_idObject (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_objObject (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_eventObject (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_idObject (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.

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
62
63
64
# File 'lib/xnm/telegram/Chat.rb', line 54

def on_command(command, **options, &block)
	raise ArgumentError, 'Block must be given!' unless block_given?

	options[:block] = block
	options[:command] = command

	out_evt = OnCommand.new(options);
	@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.

Raises:

  • (ArgumentError)

See Also:



42
43
44
45
46
47
48
49
# File 'lib/xnm/telegram/Chat.rb', line 42

def on_message(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 send_message(text, **options)
	@handler.send_message(self, text, **options);
end

#tg_mentionObject

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_iObject



78
79
80
# File 'lib/xnm/telegram/Chat.rb', line 78

def to_i
	@chat_id
end

#to_sObject



82
83
84
# File 'lib/xnm/telegram/Chat.rb', line 82

def to_s
	@casual_name
end