The Twitter block allows you to access Twitter's REST API through PubNub. You can Tweet, delete Tweets, and more from an authorized Twitter account by publishing a PubNub message. This can power apps like Twitter bots or a Delight Engine connection for a PubNub Open Growth instance.
Create a Twitter application on https://apps.twitter.com/, grant it access to a Twitter account, and give it Read and Write permission. Next, go to the Twitter application's Keys and Access Tokens page to retrieve the access token, key, and secrets. Enter them into a PubNub Twitter block event handler; they are declared near the top.
//Enter your Twitter app's keys and tokens here. Don't share them with anyone! const consumerKey = ""; const consumerSecret = ""; const accessToken = ""; const oauthTokenSecret = "";
To post a Tweet, publish a PubNub message to the block's channel like this:
{ "tweet": "My tweet via PubNub BLOCKS. #PubNub" }
To delete a Tweet, publish a message like this:
{ "delete": true, "id": "ID of Tweet to delete" }
A Tweet's specific ID number is exposed in the response from Twitter's REST API when you post or delete a Tweet. The ID is stored in the response body's id_str
property.
Publish message containing the Tweet body to input channel. Input Channel: twitter-input
{ "tweet": "My tweet via PubNub BLOCKS. #PubNub" }
Need help? Contact PubNub Support.
Copyright (c) 2018 PubNub
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.