Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Twitter - Post a Tweet #2174

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
252 changes: 252 additions & 0 deletions Twitter/Twitter_Post_a_Tweet.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "ab14b138-ea10-478e-8ffe-3d618978090c",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"<img width=\"10%\" alt=\"Naas\" src=\"https://landen.imgix.net/jtci2pxwjczr/assets/5ice39g4.png?w=160\"/>"
]
},
{
"cell_type": "markdown",
"id": "d0ced7ba-7b13-4959-a0d7-d432862bcf6e",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"# Twitter - Post a Tweet"
]
},
{
"cell_type": "markdown",
"id": "34f51201-0554-4c1f-80c0-7acb1cfc94e5",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Tags:** #twitter #tweet #post #api #developer #reference"
]
},
{
"cell_type": "markdown",
"id": "59081851-5541-413b-8604-9c2a916a0da0",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel)"
]
},
{
"cell_type": "markdown",
"id": "3fd38e55-7424-440a-bfcc-66fba72fd285",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Last update:** 2023-08-24 (Created: 2023-08-24)"
]
},
{
"cell_type": "markdown",
"id": "aa91289e-3e18-4d94-9dc7-8930c9975ff9",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Description:** This notebook creates a Tweet on behalf of an authenticated user. It is usefull for organizations to post content on Twitter from a notebook template."
]
},
{
"cell_type": "markdown",
"id": "3fbe5434-edd2-40ec-a0c0-91252a66310e",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**References:**\n- [Twitter API Reference - Post Tweets](https://developer.twitter.com/en/docs/twitter-api/tweets/manage-tweets/api-reference/post-tweets)\n- [Twitter API Reference - Authentication](https://developer.twitter.com/en/docs/basics/authentication/overview)"
]
},
{
"cell_type": "markdown",
"id": "3575660d-496a-48b6-94e0-5b2081e79093",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Input"
]
},
{
"cell_type": "markdown",
"id": "1afe080e-0227-40d9-b78e-675f9d9e8737",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Import libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6f399f29-0305-4735-878d-5e56b7de7a4b",
"metadata": {
"papermill": {},
"tags": []
},
"source": "import requests\nimport json",
"outputs": []
},
{
"cell_type": "markdown",
"id": "97dc8ce7-a254-4c1b-9066-357fc7524bac",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Setup variables\n- **consumer_key**: API key of the Twitter app\n- **consumer_secret**: API secret key of the Twitter app\n- **access_token**: Access token of the Twitter app\n- **access_token_secret**: Access token secret of the Twitter app\n- **tweet**: Text of the tweet to post"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6c76cfa7-4935-4acd-8061-5451268114af",
"metadata": {
"papermill": {},
"tags": []
},
"source": "consumer_key = \"<your_consumer_key>\"\nconsumer_secret = \"<your_consumer_secret>\"\naccess_token = \"<your_access_token>\"\naccess_token_secret = \"<your_access_token_secret>\"\ntweet = \"This is a tweet from a notebook template!\"",
"outputs": []
},
{
"cell_type": "markdown",
"id": "74cfcd68-91e2-4db1-b07d-5074e6d631dc",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Model"
]
},
{
"cell_type": "markdown",
"id": "164fc5e8-1a62-49f3-a5dd-78db3813cd38",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Post tweet"
]
},
{
"cell_type": "markdown",
"id": "152dd9b5-9c08-4da0-90d2-c881025b29a1",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"This function posts a tweet on behalf of an authenticated user."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d8b9adaa-3051-4d57-8d9a-097fcd6cafb4",
"metadata": {
"papermill": {},
"tags": []
},
"source": "def post_tweet(consumer_key, consumer_secret, access_token, access_token_secret, tweet):\n # Set up OAuth\n auth = requests.auth.OAuth1(\n consumer_key, consumer_secret, access_token, access_token_secret\n )\n\n # Post tweet\n response = requests.post(\n \"https://api.twitter.com/1.1/statuses/update.json\",\n auth=auth,\n data={\"status\": tweet},\n )\n\n # Return response\n return response",
"outputs": []
},
{
"cell_type": "markdown",
"id": "eb37c4b1-d4e4-4d69-9a3c-83169a2f527f",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Output"
]
},
{
"cell_type": "markdown",
"id": "53d225ce-d709-4cee-a821-5b1224fd29a3",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Display result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9dae5e8c-845a-4fa0-88c6-9080dc28e41d",
"metadata": {
"papermill": {},
"tags": []
},
"source": "response = post_tweet(\n consumer_key, consumer_secret, access_token, access_token_secret, tweet\n)\nprint(response.status_code)\nprint(response.text)",
"outputs": []
},
{
"cell_type": "markdown",
"id": "d7eb8bf8-2137-45a0-824c-a29eccf21862",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
" "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}