southbridgeio / tdlib-ruby

Ruby bindings and client for TDLib
MIT License
94 stars 45 forks source link

issue with methods #29

Closed phykos closed 4 years ago

phykos commented 4 years ago

my code is:

loop {
  p(@me).inspect();
  TD::ClientMethods:send_message(chat_id => '@Rubydev',
                                input_message_content => 'test');
}

the given error is:

test.rb:62: syntax error, unexpected '(', expecting '}'
...TD::ClientMethods:send_message(chat_id => '@Rubydev',
...                              ^
test.rb:62: syntax error, unexpected ',', expecting end-of-input
...nd_message(chat_id =>  '@Rubydev',
...      

its like 30 minutes I am trying to understand why the code gives me this error.

vladislav-yashin commented 4 years ago
  1. There's no such syntax in Ruby: TD::ClientMethods:send_message. Class/module method calls can be performed with Class.method or Class::method, but not with single colon.
  2. You use client methods in a wrong way. You should create TD::Client instance and then call client methods through it (see example in Readme)
phykos commented 4 years ago
1. There's no such syntax in Ruby: `TD::ClientMethods:send_message`. Class/module method calls can be performed with `Class.method` or `Class::method`, but not with single colon.

2. You use client methods in a wrong way. You should create TD::Client instance and then call client methods through it (see  [example](https://github.com/southbridgeio/tdlib-ruby#basic-authentication-example) in Readme)

yea, Ik I also tried TD::ClientMethods.send_message but the error is the same, now I am going to fix it (or at least trying to)

edit

both TD::ClientMethods.send_message and TD::ClientMethods::send_message are "undefined method `send_message' for TD::ClientMethods:Module (NoMethodError)".

btw I didnt really understood what you mean with "You should create TD::Client instance and then call client methods through it "

vladislav-yashin commented 4 years ago

Please, read README and don't try to use library in uncodumented way.

both TD::ClientMethods.send_message and TD::ClientMethods::send_message are "undefined method `send_message' for TD::ClientMethods:Module (NoMethodError)"

Because they are not supposed to be used as static module functions. TD::ClientMethods is just a mixin, and its methods are available only for instances of TD::Client.

btw I didnt really understood what you mean with "You should create TD::Client instance and then call client methods through it "

It means that you should write something like this:

TD.configure do |config|
  config.lib_path = 'path_to_dir_containing_libtdjson'

  config.client.api_id = 'your_api_id'
  config.client.api_hash = 'your_api_hash'
end

client = TD::Client.new
client.connect

promise = client.get_me.then do |me|
  message_content = TD::Types::InputMessageContent::Text.new(text: TD::Types::FormattedText.new(text: 'hello', entities: []),
                                                             disable_web_page_preview: true,
                                                             clear_draft: false)

  client.send_message(me.id, message_content)
end

message = promise.flat.value!

Please, open another issue if you have any further questions.