There are so many people who want to automatically post their tweets to their Instagram account. Well, it’s easy! But, what happens if you have location in your tweet?
In this post I’ll make it a bit more interesting and we will try to post your location as well to your Instagram account.
Create a Logic App that monitors a specific Twitter account for new tweets and automatically posts them to an Instagram account. Here’s how it could work:
- Use the Twitter Connector to monitor the specified Twitter account for new tweets.
- When a new tweet is detected, use the Instagram Connector to post the tweet’s text and image (if any) to the specified Instagram account.
- Use the Azure Cognitive Services Connector to analyze the tweet’s text for sentiment and if it’s positive, add some hashtags to the post on Instagram that are related to the tweet’s content.
- Use the Azure Function Connector to check if the tweet includes a location, if yes the function will use the Bing Maps Connector to fetch the location’s latitude and longitude and add it as location to the post on Instagram.
- Use the Azure Notification Connector to send a push notification to your mobile device whenever a new tweet is posted to Instagram.
This automation can be fun and useful for people who manage multiple social media accounts, as it eliminates the need to manually post the same content to multiple platforms. You can also customize the Logic App to suit your needs and preferences, like adding filters or different actions.
But, how do you implement step 4 in above guide? Here it is:
- Create a new Azure Function in your Azure subscription.
- In the function, create a new C# class and add the following code to it:
public static async Task<object> Run(HttpRequest req, ILogger log)
{
// Parse the JSON input from the Logic App
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
// Check if the tweet includes a location
if (data.tweet.place != null)
{
// Fetch the location's latitude and longitude using the Bing Maps API
var location = data.tweet.place.full_name;
var bingKey = "your_bing_maps_api_key";
var bingUrl = $"http://dev.virtualearth.net/REST/v1/Locations?q={location}&key={bingKey}";
var client = new HttpClient();
var bingResponse = await client.GetAsync(bingUrl);
var bingJson = await bingResponse.Content.ReadAsStringAsync();
dynamic bingData = JsonConvert.DeserializeObject(bingJson);
var latitude = bingData.resourceSets[0].resources[0].point.coordinates[0];
var longitude = bingData.resourceSets[0].resources[0].point.coordinates[1];
// Return the latitude and longitude as output
return new
{
latitude = latitude,
longitude = longitude
};
}
else
{
return null;
}
}
- In the function.json file, update the bindings to match the input and output of the function as follows:
{
"bindings": [
{
"authLevel": "function",
"name": "req",
"type": "httpTrigger",
"direction": "in",
"route": "location"
},
{
"name": "log",
"type": "log",
"direction": "in"
},
{
"name": "output",
"type": "http",
"direction": "out"
}
]
}
- In the Logic App, add a new “HTTP” action and set the URI to the URL of the Azure Function, along with the location route, and add the request body.
- Add a “Parse JSON” action to parse the response from the Azure Function, extracting the latitude and longitude values.
- Use the output of the function as input for the Instagram connector, add the location latitude and longitude to the post before it’s being posted.
By following these steps, you’ll be able to use Azure Function to check if the tweet includes location, if yes use the Bing Maps API to fetch the location’s latitude and longitude and add it as location to the post on Instagram.
It’s important to note that you need to have a valid Bing Maps API key in order for this to work, you can get one from the Bing Maps website.