A screenshot showing a tweet with an image and highlighting the code with alternative text.

Twitter bots and accessibility

Twitter’s engineers are doing their best to make the site accessible, but there are few areas in which they need our help.

Image descriptions

When you upload an image to your tweet, you have an option to add a description that will be used by screen readers and thus making it possible for people who can’t see the image to get an idea what’s in it.

And when you make a Twitter bot that posts or generates images, you can use the Twitter API to do the same. Let me show you how.

The endpoint we’ll be using is media/metadata/create, check out the documentation for it here.

In short, after you upload an image, you can reference its ID and pass it to this endpoint together with an alt_text object that contains the description.

You can see the full code on GitHub, here’s what the relevant part looks like:

T.post( 'media/upload', { media_data: imageData }, function( err, data, response ){
    if ( err ){
        console.log( 'error:', err );
    }
    else{
        const image = data;

        /* Add image description. */

        T.post( 'media/metadata/create', {
            media_id: data.media_id_string,
            alt_text: {
                text: 'Describe the image'
            }            
        }, function( err, data, response ) {

            /* And finally, post a tweet with the image. */

            T.post( 'statuses/update', {
                // status: 'Optional tweet text.',
                media_ids: new Array( image.media_id_string )
            },
            function( err, data, response) {
                if (err){
                    console.log( 'error:', err );
                }
            );
        } );
    }
} );

It’s a pretty easy and straightforward way to make sure everyone gets to enjoy your bot.

For some pointers on how to write a good image description, you can read this article from Harvard University.

Hashtags

If your bot uses hashtags, another good practice is to capitalize the first letter of each word. So instead #generativeart you should use #GenerativeArt. This is useful for screen readers, but also helps folks with dyslexia or cognitive disabilities.

If you have any more tips, feel free to share them via email or Twitter DM!

More tutorials

Browse all