Skip to content

08. Direct Messaging

David Brouwer edited this page Nov 13, 2021 · 5 revisions

ToDo: still need to write this page

Tldr; You can message through the insta.Inbox struct. Everytime you run the program you need to fetch the messages. If you call Login() for the first time, this will happen automatically. Otherwise, you can do this by either calling OpenApp(), however this will also make a bunch of other calls such as fetch your home & explore page. You might not always want this. Therefore you can also call insta.Inbox.Sync(), this will fetch your inbox for you. To get your pending messages you need to call insta.Inbox.SyncPending()

Interacting with DMs should be pretty intuitive, but for more details please look at the code. If you need more help please push me to write this page.

Inbox:

type Inbox struct {
        Conversations []*Conversation `json:"threads"`
        Pending       []*Conversation `json:"pending"`

        HasNewer            bool   `json:"has_newer"` // TODO
        HasOlder            bool   `json:"has_older"`
        Cursor              string `json:"oldest_cursor"`
        UnseenCount         int    `json:"unseen_count"`
        UnseenCountTs       int64  `json:"unseen_count_ts"`
        MostRecentInviter   User   `json:"most_recent_inviter"`
        BlendedInboxEnabled bool   `json:"blended_inbox_enabled"`
        NextCursor          struct {
                CursorV2ID         float64 `json:"cursor_thread_v2_id"`
                CursorTimestampSec float64 `json:"cursor_timestamp_seconds"`
        } `json:"next_cursor"`
        PrevCursor struct {
                CursorV2ID         float64 `json:"cursor_thread_v2_id"`
                CursorTimestampSec float64 `json:"cursor_timestamp_seconds"`
        } `json:"prev_cursor"`
        // this fields are copied from response
        SeqID                 int64 `json:"seq_id"`
        PendingRequestsTotal  int   `json:"pending_requests_total"`
        HasPendingTopRequests bool  `json:"has_pending_top_requests"`
        SnapshotAtMs          int64 `json:"snapshot_at_ms"`
        // Has unexported fields.
}
    Inbox is the direct message inbox.

    Inbox contains Conversations. Each conversation has InboxItems. InboxItems
    are the message of the chat.

func (inbox *Inbox) Error() error
    Error will return Inbox.err

func (inbox *Inbox) InitialSnapshot() bool
    InitialSnapshot fetches the initial messages on app open, and is called

        from Instagram.OpenApp() automatically.

func (inbox *Inbox) New(user *User, text string) (*Conversation, error)
    New will send a message to a user in an existring message thread if it
    exists,

        if not, it will create a new one. It will return the Conversation object,
        for further messages you can call Conversation.Send()

func (inbox *Inbox) Next() bool
    Next allows pagination over message threads.

func (inbox *Inbox) NextPending() bool
    NextPending allows pagination over pending messages.

func (inbox *Inbox) Reset()
    Reset sets inbox cursor at the beginning.

func (inbox *Inbox) Sync() error
    Sync updates inbox messages.

func (inbox *Inbox) SyncPending() error
    SyncPending updates inbox pending messages.
type Conversation struct {
        ID   string `json:"thread_id"`
        V2ID string `json:"thread_v2_id"`
        // Items can be of many types.
        Items                      []*InboxItem          `json:"items"`
        Title                      string                `json:"thread_title"`
        Users                      []*User               `json:"users"`
        LeftUsers                  []*User               `json:"left_users"`
        AdminUserIDs               []int64               `json:"admin_user_ids"`
        ApprovalRequiredNewMembers bool                  `json:"approval_required_for_new_members"`
        Pending                    bool                  `json:"pending"`
        PendingScore               int64                 `json:"pending_score"`
        ReshareReceiveCount        int                   `json:"reshare_receive_count"`
        ReshareSendCount           int                   `json:"reshare_send_count"`
        ViewerID                   int64                 `json:"viewer_id"`
        ValuedRequest              bool                  `json:"valued_request"`
        LastActivityAt             int64                 `json:"last_activity_at"`
        Named                      bool                  `json:"named"`
        Muted                      bool                  `json:"muted"`
        Spam                       bool                  `json:"spam"`
        ShhModeEnabled             bool                  `json:"shh_mode_enabled"`
        ShhReplayEnabled           bool                  `json:"shh_replay_enabled"`
        IsPin                      bool                  `json:"is_pin"`
        IsGroup                    bool                  `json:"is_group"`
        IsVerifiedThread           bool                  `json:"is_verified_thread"`
        IsCloseFriendThread        bool                  `json:"is_close_friend_thread"`
        ThreadType                 string                `json:"thread_type"`
        ExpiringMediaSendCount     int                   `json:"expiring_media_send_count"`
        ExpiringMediaReceiveCount  int                   `json:"expiring_media_receive_count"`
        Inviter                    *User                 `json:"inviter"`
        HasOlder                   bool                  `json:"has_older"`
        HasNewer                   bool                  `json:"has_newer"`
        HasRestrictedUser          bool                  `json:"has_restricted_user"`
        Archived                   bool                  `json:"archived"`
        LastSeenAt                 map[string]lastSeenAt `json:"last_seen_at"`
        NewestCursor               string                `json:"newest_cursor"`
        OldestCursor               string                `json:"oldest_cursor"`

        LastPermanentItem InboxItem `json:"last_permanent_item"`
        // Has unexported fields.
}
    Conversation is the representation of an instagram already established
    conversation through direct messages.

func (c *Conversation) Approve() error
    Approve a pending direct message.

func (c *Conversation) Error() error
    Error will return Conversation.err

func (c *Conversation) GetItems() error
    GetItems is an alternative way to get conversation messages, e.g. refresh.
    The app calls this when approving a DM request, for example.

func (conv *Conversation) Hide() error

func (c *Conversation) MarkAsSeen(msg InboxItem) error
    MarkAsSeen will marks a message as seen.

func (c *Conversation) Next() bool
    Next loads older messages if available. If not it will call Refresh().

func (c *Conversation) Refresh() error
    Refresh will fetch a conversation's unseen messages.

func (c *Conversation) Send(text string) error
    Send sends message in conversation

func (c *Conversation) Write(b []byte) (int, error)
    Write is like Send but being compatible with io.Writer.

Clone this wiki locally