Jan 25, 2021 | twitter, zapier

How to post to Twitter from your Laravel app using Zapier

!
Warning: This post is over a year old. I don't always update old posts with new information, so some of this information may be out of date.

Recently Twitter started promoting their v2 API, which includes requiring all developers to re-apply to get developer credentials. Long story short, my application was rejected, so I'm left trying to build personal apps that tweet without API access. I'm pretty sure their goal here is to get each individual project to apply for its own sake, so I'm sure this won't impact my client work, but it's an annoyance for me.

I asked around for ideas, and Eric Barnes mentioned that he uses an RSS feed to post to various social networks using Zapier. Perfect!

Note: Zapier is required by Twitter to scrub your tweets of any @ mentions, so if your tweets require that, Zapier won't work for you.

The context

I'm building an app that I want to tweet when certain events happen. I was originally going to use the Twitter Laravel Notification Channel, but now my goal is to push a webhook up to Zapier and have Zapier post the tweet.

I'm working in a command that loops over all of the items, looks for those needing notification, and then pushes out the notification:

foreach ($this->itemsNeedingNotification() as $item) {
    // Build and send tweet for $item
}

The setup

Log into Zapier and you will find yourself on the dashboard. You can fill out the various sections to figure out exactly what you want to do:

Create workflow in Zapier

Or you can just click directly on this link: Create tweet in Twitter when catch hook in Webhooks by Zapier

Set up Zapier trigger

Now we've got a custom Webhook URL. Copy that URL and paste it into your Laravel .env file as ZAPIER_WEBHOOK_URL.

Now, Zapier wants us to test the webhook, so let's pause there and get to writing some code.

Zttp is dead. Long live Zttp!

Recent versions of Laravel have included an HTTP component, the direct successor of Adam Wathan's ZTTP client, that make it super simple to send HTTP requests. Let's build one in our command:

...
use Illuminate\Support\Facades\Http;
...

class TweetStuff
{
    public function handle()
    {
        Http::post(
            config('services.zapier.twitter_webhook_url'),
            [
                'text' => 'Testing tweet text'
            ]
        );
    }
}

Note: Later in this tutorial, you'll want to actually tweet this message out to make sure your integration works, so you may want to replace "Testing tweet text" with something you're actually interested in tweeting.

Now, of course, we need to make that config exist. Add ZAPIER_WEBHOOK_URL= to the bottom of the .env.example file, and then edit config/services.php and add the following to the bottom:

    'zapier' => [
        'twitter_webhook_url' => env('ZAPIER_WEBHOOK_URL'),
    ],

OK. Let's test that out. Run your command, and go back and check the Zapier page. Click "Test trigger" and you should see your tweet text right there in the app!

Zapier test trigger results

Since it worked, we can now hit Continue. Accept the default (app "Twitter", Action Event "Create Tweet") and hit Continue again. Now you'll be prompted whether you want to auto-follow Zapier, then taken to the Twitter OAuth screen.

Twitter OAuth screen

Now let's continue again!

Build the tweet

You can write text around the tweet, but for me, I know I wanted to just use the tweet text from my webhook. To add text from your webhook, click in the "Message" box and choose "1. Text: Testing tweet text" from the dropdown. I also choose to not have my links shortened by Zapier.

Here's what it looks like:

Screenshot of the "Set up action" form in Zapier

Let's hit continue, and now we're ready to actually test the Twitter connection! Hit either button—I hit "Test & Continue"—and you can now go over to your Twitter account and look for that tweet.

Test tweet

Look at that! Beautiful! If you're happy with how it turned out, you can click "Turn on Zap", and you're good to go! Happy tweeting!


Comments? I'm @stauffermatt on Twitter


Tags: twitter  •  zapier

Subscribe

For quick links to fresh content, and for more thoughts that don't make it to the blog.