Remove Twitter support in bots.

This commit is contained in:
Feufochmar 2022-12-26 14:10:22 +01:00
parent 40119ac6af
commit 6a0621468b
4 changed files with 10 additions and 182 deletions

View File

@ -2,7 +2,6 @@
(require net/url
"mastodon.rkt"
"twitter.rkt"
"configuration.rkt")
; Do an API call to the website
@ -19,15 +18,3 @@
quote
#:visibility "unlisted"))
(newline)
; Publication on Twitter
(display
(send
(new twitter-client%
[consumer-key arnytron:twitter-consumer-key]
[consumer-secret arnytron:twitter-consumer-secret]
[access-key arnytron:twitter-access-token]
[access-secret arnytron:twitter-access-token-secret])
status-update
quote))
(newline)

View File

@ -6,17 +6,9 @@
;
arnytron:mastodon-instance
arnytron:mastodon-authorization-bearer
arnytron:twitter-consumer-key
arnytron:twitter-consumer-secret
arnytron:twitter-access-token
arnytron:twitter-access-token-secret
;
floraverse:mastodon-instance
floraverse:mastodon-authorization-bearer
floraverse:twitter-consumer-key
floraverse:twitter-consumer-secret
floraverse:twitter-access-token
floraverse:twitter-access-token-secret
;
daily-island:mastodon-instance
daily-island:mastodon-authorization-bearer
@ -30,22 +22,12 @@
(define arnytron:mastodon-instance "...")
; OAuth2 authorization bearer
(define arnytron:mastodon-authorization-bearer "...")
; Twitter keys
(define arnytron:twitter-consumer-key "...")
(define arnytron:twitter-consumer-secret "...")
(define arnytron:twitter-access-token "...")
(define arnytron:twitter-access-token-secret "...")
; Floraverse postbot
; Publication on Mastodon
(define floraverse:mastodon-instance "...")
; OAuth2 authorization bearer
(define floraverse:mastodon-authorization-bearer "...")
; Twitter keys
(define floraverse:twitter-consumer-key "...")
(define floraverse:twitter-consumer-secret "...")
(define floraverse:twitter-access-token "...")
(define floraverse:twitter-access-token-secret "...")
; Daily Island postbot
; Publication on Mastodon

View File

@ -3,7 +3,6 @@
(require net/url
json
"mastodon.rkt"
"twitter.rkt"
"configuration.rkt"
srfi/8)
@ -14,7 +13,6 @@
(string->url character-generator))))
; Function to generate a character
; Each string defining the character should be less than 140 characters (twitter limit)
; The concatenation of strings should be less than 500 characters (mastodon limit)
; The function generates characters as long as the limits are not respected
; The function returns two values: a list of strings for twitter and a single string for mastodon
@ -26,36 +24,16 @@
(hash-ref chr 'motto)
(hash-ref chr 'traits)))
(define toot (string-join tweets "\n"))
(if (or (>= (string-length toot) 500)
(member #t (map (lambda (x) (>= (string-length x) 140)) tweets)))
(if (>= (string-length toot) 500)
(generate-messages)
(values tweets toot)))
toot))
; Post a character
(receive
(tweets toot) (generate-messages)
; Send to mastodon
(send
(new mastodon-client%
[instance floraverse:mastodon-instance]
[authorization-bearer floraverse:mastodon-authorization-bearer])
new-status
toot
#:visibility "public")
; Send to twitter
(define client (new twitter-client%
[consumer-key floraverse:twitter-consumer-key]
[consumer-secret floraverse:twitter-consumer-secret]
[access-key floraverse:twitter-access-token]
[access-secret floraverse:twitter-access-token-secret]))
(foldl
(lambda (message id-previous)
(hash-ref
(send
client
status-update
message
#:in-reply-to-id id-previous)
'id_str))
#f
tweets))
; Send to mastodon
(send
(new mastodon-client%
[instance floraverse:mastodon-instance]
[authorization-bearer floraverse:mastodon-authorization-bearer])
new-status
(generate-messages)
#:visibility "public")

View File

@ -1,119 +0,0 @@
#lang racket/base
(require racket/class
racket/string
net/url
net/uri-codec
json
racket/date
web-server/stuffers/hmac-sha1
net/base64)
(provide twitter-client%)
(define twitter-client%
(class object%
; Needs the various OAuth keys and secrets
(init consumer-key consumer-secret access-key access-secret)
(super-new)
;
(define twitter-consumer-key consumer-key)
(define twitter-consumer-secret consumer-secret)
(define twitter-access-token access-key)
(define twitter-access-token-secret access-secret)
; Twitter root
(define twitter-root "https://api.twitter.com/1.1")
;
; Private methods for OAuth 1.0
; To generate timestamp & nonce
(define (get-timestamp) (number->string (current-seconds)))
; Compute the parameter string
; The oauth-headers and parameters are lists of (key . value)
(define (compute-signature-parameter-string oauth-headers parameters)
(string-join
(sort
(map
(lambda (x)
(string-append (uri-unreserved-encode (car x)) "=" (uri-unreserved-encode (cdr x))))
(append oauth-headers parameters))
string<?)
"&"))
; compute the signature base string
(define (compute-signature-base-string request-type url parameter-string)
(string-append
(string-upcase request-type)
"&"
(uri-unreserved-encode url)
"&"
(uri-unreserved-encode parameter-string)))
; compute the signature key
(define (compute-signature-key)
(string-append
(uri-unreserved-encode twitter-consumer-secret)
"&"
(uri-unreserved-encode twitter-access-token-secret)))
; compute the signature
(define (compute-signature signature-key signature-base-string)
(bytes->string/utf-8
(base64-encode
(HMAC-SHA1
(string->bytes/utf-8 signature-key)
(string->bytes/utf-8 signature-base-string))
#"")))
; compute the signature from the request
(define (compute-signature-from-request request-type url oauth-headers parameters)
(compute-signature
(compute-signature-key)
(compute-signature-base-string
request-type
url
(compute-signature-parameter-string
oauth-headers
parameters))))
;
; Post a new status
; Return a dictionnary corresponding to the json structure returned by the server
(define/public (status-update status
#:in-reply-to-id [in-reply-to-id #f])
(let*
((url (string-append twitter-root "/statuses/update.json"))
(parameters
(append
(list (cons "status" status))
(if in-reply-to-id
(list
(cons "in_reply_to_status_id" in-reply-to-id)
(cons "auto_populate_reply_metadata" "true"))
(list))
))
(timestamp (get-timestamp))
(oauth-headers
(list
(cons "oauth_consumer_key" twitter-consumer-key)
(cons "oauth_token" twitter-access-token)
(cons "oauth_version" "1.0")
(cons "oauth_timestamp" timestamp)
(cons "oauth_nonce" timestamp)
(cons "oauth_signature_method" "HMAC-SHA1"))))
(read-json
(post-pure-port
(string->url url)
(string->bytes/utf-8
(string-join
(map
(lambda (x) (string-append (uri-unreserved-encode (car x)) "=" (uri-unreserved-encode (cdr x))))
parameters)
"&"))
(list
"Content-Type: application/x-www-form-urlencoded"
(string-append
"Authorization: OAuth "
"realm=\"\","
(string-join
(map
(lambda (x) (string-append (uri-unreserved-encode (car x)) "=\"" (uri-unreserved-encode (cdr x)) "\""))
oauth-headers)
",")
",oauth_signature=\"" (uri-unreserved-encode (compute-signature-from-request "POST" url oauth-headers parameters)) "\"")
)))))
))