How to send an Instagram Direct message using the undocumented API or undocumented MQTT message protocol in Python?

Viewed 901

Disclaimer: I don't want to use Selenium or other automated browser

I am trying to send Instagram direct messages using the undocumented Instagram API just for fun.

I have ben trying to achieve this for the past week, however, I have only found that they are using a web socket for sending text messages, with some really weird json fields that I don't understand.

Also, I don't really know how I could emulate sending those messages using Python.

Here's an example of the web socket connection that I am talking (I will blur some fields in case they are sensitve)

2�/ig_send_message{
   "client_context":"censored",
   "device_id":"censored",
   "action":"send_item",
   "item_type":"text",
   "mutation_token":"censored",
   "text":"asdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdasdasasdas",
   "thread_id":"censored"
}

The request url is wss://edge-chat.instagram.com/chat

enter image description here

1 Answers

As I see you are using the web–api for reverse engineering.

Instagram is using mqtt for direct messaging and some other events. Since mqtt is not supported on browsers, they are using a websocket for workaround.

What you see on the top is the topic and after that the message. It makes no sense to view the data as json, just look at the raw data and try to decode it. I don't have much experience in the web api, but what i would try is to get content of the message via python and try to decode it. Sometimes its a protocol buffer, or they try to encode it using zstd. But try to using the raw data first.

Related