ssut/telegram-rb

Not working in Mac OS X

Closed this issue · 13 comments

I could not make it work on Mac OS X Yosemite :(
ruby 2.0.0p481
I see in the console:
[2015-08-10 03:48:57] INFO (Telegram::Client): Initialized
[2015-08-10 03:48:57] INFO (Telegram::Client): Trying to start telegram-cli and then connect
[2015-08-10 03:48:57] INFO (Telegram::Client): Successfully connected to the Telegram CLI
[2015-08-10 03:48:57] INFO (Telegram::Client): Start polling for events

I think the gem its not raising events or processing telegram.connect do block

Have you any idea about this?

Thank you very much for your time on this.

ssut commented

What telegram-rb did you use for? (gem or master branch)
I've made this work on my OS X Yosemite and it works properly on my machine.

Hi @ssut !
I use master branch, is this correct?
The code is this:

EM.run do
  telegram = Telegram::Client.new do |cfg|
    cfg.daemon = '/Users/jjmiranda/Documents/Proyectos/tg/bin/telegram-cli'
    cfg.key = '/Users/jjmiranda/Documents/Proyectos/tg/tg-server.pub'
  end
  puts 'Inicializando cliente...'
  telegram.connect do
    # This block will be executed when initialized.
    puts 'Dentro del connect...' ###Never Execute this!
    # See your telegram profile
    puts telegram.profile
    telegram.contacts.each do |contact|
      puts contact
    end
    telegram.chats.each do |chat|
      puts chat
    end
    # Event listeners
    # When you've received a message:
    telegram.on[Telegram::EventType::RECEIVE_MESSAGE] = Proc.new { |event|
      # `tgmessage` is TelegramMessage instance
      puts event.tgmessage
      puts 'Llega alguna vez aquiiiiiii??????????'
    }
    # When you've sent a message:
    telegram.on[Telegram::EventType::SEND_MESSAGE]= Proc.new { |event|
      puts event
    }
  end
end

Thanks in advance for your feedback on this.
Best regards!

ssut commented

Ye, the code you've written is correct. then, could you let me know what version of telegram-cli you were using?

Hi @ssut, I'm using:
Telegram-cli version 1.3.3, Copyright (C) 2013-2015 Vitaly Valtman.
eventmachine (1.0.8)
ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14]

Thanks in advance for your help!

ssut commented

Thanks for your information, I'll fix it asap if I can.

Thank you very much!
If you need more information please tell me!
Cheers,
JJMiranda - from Lima, Perú :)

Hi @ssut !
I think the problem is in the API.update! function, done variable never change to true

def update!(&cb)
      done = false
      EM.synchrony do
        multi = EM::Synchrony::Multi.new
        multi.add :profile, update_profile!
        multi.add :contacts, update_contacts!
        multi.add :chats, update_chats!
        multi.perform
        done = true   ###This never execute
      end

      check_done = Proc.new {
        if done   ### done never change to true
          @starts_at = Time.now
          cb.call unless cb.nil?
          logger.info("Successfully loaded all information")
        else
          EM.next_tick(&check_done)
        end
      }

      EM.add_timer(0, &check_done)
end

I hope this help to you!

ssut commented

Could you please check the update codes(methods start with "update_") where it's stopped the synchrony task? I think there is a problem with updating informations and some methods wouldn't work in its own way.
Thank you for your useful information and sorry for the late response on this.

The problem is in the update_chats method.

      done = false
      EM.synchrony do
        multi = EM::Synchrony::Multi.new
        multi.add :profile, update_profile!
        multi.add :contacts, update_contacts!
        multi.add :chats, update_chats!  #<-This is the problem :)
        puts 'Antes del multi.perform'
        res = multi.perform
        puts 'Despues del multi.perform'
        done = true
      end

Any advise?
Thank you very much!

Hi @ssut , i think found the cause of error.
If you don't have any chat['type'] == 'chat' in your telegram account your code never execute callback.trigger(:success).
This is my case, all my chats are only with users and update_chats in my case never execute callback.trigger(:success) located in collect_done :(.

def update_chats!
      assert!
      callback = Callback.new

      collected = 0
      collect_done = Proc.new do |id, data, count|
        collected += 1
        @chats << TelegramChat.new(self, data)
        callback.trigger(:success) if collected == count
      end
      collect = Proc.new do |id, count|
        @connection.communicate(['chat_info', "chat\##{id}"]) do |success, data|
          collect_done.call(id, data, count) if success
        end
      end

      @chats = []
      @connection.communicate('dialog_list') do |success, data|
        if success and data.class == Array
          chatsize = data.count { |chat| chat['type'] == 'chat' }
          data.each do |chat|
            if chat['type'] == 'chat'
              collect.call(chat['id'], chatsize)
            elsif chat['type'] == 'user'
              @chats << TelegramChat.new(self, chat)
            end
          end
        else
          raise "Couldn't fetch the dialog(chat) list."
        end
      end
      callback
    end

Any advice to solve this scenario and fix your great work :)?
Thanks!

ssut commented

Ahhh thanks, I'll fix it soon!

Perfect, thanks!
:)