Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending direct messages from twitter API

Tags:

php

twitter

I'm trying to send internal server notices via direct message to myself and another staff member through the API. I've gone through the API setup on dev.twitter and I've downloaded abraham's twitteroauth library.

Where do I need to be looking to send a message direct from the server (Through my account) without having to log in everytime with a browser? Last time I used the twitter API was before oauth, so quite some time has passed.

tl;dr Need to send direct messages through twitter without seeing this Twitter button

like image 516
Jamie Taylor Avatar asked Dec 20 '12 14:12

Jamie Taylor


People also ask

Can Twitter send Direct Messages?

Use Direct Messages to have non-public conversations on Twitter with anyone who follows you. You can check in with someone, start new conversations or move conversations off a timeline, or share Tweets or media with an individual or group. Your browser does not support the <code>video</code> element.

How do you send an automatic Direct Message on Twitter?

Use the navigation menu to go to Social Accounts > Edit Welcome DM and select your Twitter account. Here, you will find the form to enter your automated direct message and a checkbox to automatically send a welcome message.

Can Twitter no longer send Direct Messages?

Why am I having trouble sending Direct Messages? There is an account limit of 1,000 Direct Messages sent per day. Once you reach this limit, you can't send any more Direct Messages for the day. If you are sending Direct Messages to accounts that do not follow you, you may need to verify your phone number.


1 Answers

You can get your personal tokens from you app page on dev.twitter.com and use it without having to Sign in with Twitter.

On your app page under the section OAuth settings get:

  • the consumer key
  • the consumer secret

And under Your access token get:

  • the access token
  • the access token secret

Check that the access level is Read, write, and direct messages. Otherwise change it in the Settings tab and recreate your access tokens (there is a button on the bottom of the Details tab).

Then in php

require('Abrahams library');

// Get everything you need from the dev.twitter.com/apps page
$consumer_key = 'APP KEY';
$consumer_secret = 'APP SECRET';
$oauth_token = 'YOUR TOKEN';
$oauth_token_secret = 'YOUR TOKEN SECRET';

// Initialize the connection
$connection = new TwitterOAuth($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);

// Send a direct message
$options = array("screen_name" => "theGuyIWantToDM", "text" => "Hey that's my message");
$connection->post('direct_messages/new', $options);
like image 103
maxdec Avatar answered Oct 27 '22 15:10

maxdec