hayamiz / twittering-mode

An Emacs major mode for Twitter
http://twmode.sourceforge.net/
545 stars 92 forks source link

getting the cited-id from a newly posted tweet? #147

Open jkitchin opened 6 years ago

jkitchin commented 6 years ago

If I use code like this:

(twittering-call-api 'update-status '((status . "you got it 5")))

I can post tweets. I wonder how I can get the cited-id for that tweet though, so I can reply to it in a thread. Any hints?

ketan0 commented 3 years ago

For anyone who's interested, this is possible! just needs a little bit of a workaround, because while twittering-call-api allows for 'sentinel' functions (i.e. callbacks called upon completion of the request) passed through the args-alist parameter, curiously for many of the API endpoints, update-status included, the sentinel is ignored. 🤔

Using update-status as an example, you can tweak twittering-mode.el as follows to extract and pass on the sentinel (line 6805):

Before:

 ((eq command 'update-status)
    ;; Post a tweet.
    (let* ((status (cdr (assq 'status args-alist)))
       (id (cdr (assq 'in-reply-to-status-id args-alist)))
       (host twittering-api-host)
       (method "1.1/statuses/update")
       (http-parameters
        `(("status" . ,status)
          ,@(when id `(("in_reply_to_status_id" . ,id)))))
       (format-str "json"))
      (twittering-http-post account-info-alist host method http-parameters
                format-str additional-info)))

After:

 ((eq command 'update-status)
    ;; Post a tweet.
    (let* ((status (cdr (assq 'status args-alist)))
       (sentinel (cdr (assq 'sentinel args-alist))) ;; added this line
       (id (cdr (assq 'in-reply-to-status-id args-alist)))
       (host twittering-api-host)
       (method "1.1/statuses/update")
       (http-parameters
        `(("status" . ,status)
          ,@(when id `(("in_reply_to_status_id" . ,id)))))
       (format-str "json"))
      (twittering-http-post account-info-alist host method http-parameters
                format-str additional-info (or sentinel 'ignore)))) ;; changed this line

Then you can pass a sentinel to the request and extract the tweet id as follows:

(defun my-sentinel (proc status connection-info header-info)
  (message
   (let ((status-line (cdr (assq 'status-line header-info)))
         (status-code (cdr (assq 'status-code header-info))))
     (case-string
      status-code
      (("200")
       (let ((json-response (twittering-json-read)))
         "the tweet id is: %s" (alist-get 'id json-response)))
      (t
       (format "Response: %s"
               (twittering-get-error-message header-info connection-info)))))))

(twittering-call-api 'update-status '((status . "hello twitter")
                                      (sentinel . my-sentinel)))