Telegram and libpurple

SPOILERS: If you're using Telegram and a libpurple-telegram based client such as bitlbee or Pidgin, you may need to turn off the send-read-notifications setting in order to receive notifications in other clients.

This can be done in bitlbee with the following command:

account telegram off
account telegram set send-read-notifications false
account telegram on
    

I've recently started using Telegram messenger as part of an attempt to move the bulk of my infrastructure off Google. Overall it's been good, but I had a problem with it: I never received notifications on my Android client.

I also didn't receive notifications on my old Blackberry Passport but I put that down to the Passport emulating an old version of the Android environment which may have stopped Telegram from using “proper” notifications.

I ignored the problem for a while, since I had enough other sharp edges to file off my new phone. I managed to resolve a good number of them, but a few remained. For example, sometimes my phone would be on my desk, and the screen would turn on. No notifications, no apparent cause, it would just show me the lock screen until the screen timed out and turned off.

Scouring the fields of the internet for any information I could glean, I found a lot of advice that didn't help me:

  • Update Telegram
  • Check the notification settings
  • Disable battery optimisation

I also found a report of someone fixing their problem by signing out of the desktop client. And that got me thinking... I use IRC for a lot of my communication, or at least an IRC bridge called bitlbee. This lets me use all my different chat programs in a unified interface, from anywhere (since the client runs on my VPS), in plain-text mode.

When I first started using Telegram, I checked whether it was supported, set up my account in bitlbee, and forgot about it. I couldn't really tell you who I'm talking to over Telegram or Google Hangouts.

So I conducted an experiment: I tried signing out of Telegram on bitlbee. Immediately Android notifications started flooding in! I'd isolated the problem, but now I had to debug and fix it...

After signing into bitlbee Telegram again I noticed that whenever someone messaged me on it, my phone screen would light up. This was predictable and consistent. It seemed like my phone was receiving the messages, just not notifying me.

I'd love to say I had a clear process for further debugging this issue, but the truth is the answer just came to me, as these things often do while on a walk or in the shower.

Read receipts.

bitlbee was marking each message it received as “read”, when it received them, not when I did. So Android Telegram didn't bother to notify me.

I looked for the source of the libpurple-telegram library responsible for implementing Telegram support, and followed through the code to find where it was setting read receipts. My original plan was to find the implementation, and build my own version of the library with that part removed.

I found the relevant function in telegram-purple.c:

static void update_marked_read (struct tgl_state *TLS, int num, struct tgl_message *list[]) {
  if (! purple_account_get_bool (tls_get_pa (TLS), TGP_KEY_DISPLAY_READ_NOTIFICATIONS, FALSE)) {
    return;
  }
  int i;
  for (i = 0; i < num; i++) {
    if (list[i]) {
      // only display the read recipes for messages that we sent ourselves
      if (tgl_get_peer_id (list[i]->from_id) == tgl_get_peer_id (TLS->our_id)) {
        debug ("update_mark_read to=%d", tgl_get_peer_id (list[i]->to_id));
        tgp_msg_special_out (TLS , _("Message marked as read."), list[i]->to_id, PURPLE_MESSAGE_SYSTEM);
      }
    }
  }
}
  

It turns out someone had already created a toggle for this functionality.

... It was at this point that I started looking at the options exposed from within bitlbee. Bitlbee has a pretty good interactive help system, and in short order I found the option I was looking for: send-read-notifications

Disabling this option with

account telegram set send-read-notifications false
  
promptly rewarded me with notifications on my phone. (Well, relatively promptly. Once I asked my girlfriend to message me to test the notifications.)

Unfortunately, IRC doesn't support a way to tell the server you've seen a message, so now I get notifications for *every* Telegram message I haven't seen on my phone, even if I'm actively chatting with someone on my laptop.

That's the price of running all your desktop chat through an IRC bridge like it's the 90s. For me, it's worth it! :)

In next week's episode, we'll debug why it takes my phone seven wall-clock seconds to work out that I've plugged in headphones!

Addendum

After I started getting notifications again, I realised I had somehow made Telegram give me giant full-screen popups that blocked whatever app I was actively using.

Since this took a little while to find, for posterity here is the path to the setting: Settings->Notifications and Sounds->{Private Chats,Groups,Channels}->Popup Notifications. Make sure to tap the "Private Chats", etc, text not the toggle. The affordances here are a bit weird but it's both an on/off *and* a submenu. This is a good paradigm, but I believe some indicator of further options would help a lot, be that a "..." or a little right arrow.