Created
September 29, 2019 16:31
-
-
Save giuliohome/97a0cb1cbf58c46def26d619d49a8a10 to your computer and use it in GitHub Desktop.
My Open Twitter Search integrated also with Slack channel, posting as bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.new_year2018; | |
import android.os.AsyncTask; | |
import java.io.IOException; | |
import java.io.UnsupportedEncodingException; | |
import java.util.ArrayList; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.client.ClientProtocolException; | |
import org.apache.http.client.entity.UrlEncodedFormEntity; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.impl.client.BasicResponseHandler; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.message.BasicNameValuePair; | |
import org.apache.http.protocol.HTTP; | |
public class PostSlack_BackGround extends AsyncTask<String, Void, String> { | |
@Override | |
protected String doInBackground(String... params) { | |
HttpPost post = new HttpPost(params[0]); | |
UrlEncodedFormEntity form; | |
if (params.length == 5) | |
{ | |
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); | |
nameValuePairs.add(new BasicNameValuePair("token", params[1] )); | |
nameValuePairs.add(new BasicNameValuePair("channel", params[2] )); | |
nameValuePairs.add(new BasicNameValuePair("text", params[3] )); | |
nameValuePairs.add(new BasicNameValuePair("blocks", params[4] )); | |
try { | |
form = new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8); | |
//form.setContentEncoding(HTTP.UTF_8); | |
post.setEntity(form); | |
} catch (UnsupportedEncodingException e) { | |
// TODO Auto-generated catch block | |
return "{\"error\":\""+e.getMessage()+"\"}"; | |
} | |
} | |
DefaultHttpClient client = new DefaultHttpClient(); | |
String response=""; | |
try { | |
response = client.execute(post, new BasicResponseHandler()); | |
} catch (ClientProtocolException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
return "{\"error\":\""+e.getMessage()+"\"}"; | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
return "{\"error\":\""+e.getMessage()+"\"}"; | |
} | |
return response; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
showToast("Saving tweets to Slack .Net Programming!"); | |
String slackTocken = getString(R.string.slackToken); | |
String slackPostMessage = "https://slack.com/api/chat.postMessage"; | |
String slackChannel = "twitter2slack"; | |
String slackTitle = textbox.getText().toString(); | |
String slackBlocks = "["; | |
for(final Tweet item : TweetSearchActivity.mytweets ){ | |
boolean foundImage = false; | |
String firstImage = ""; | |
slackBlocks += " { "; | |
slackBlocks += "\"type\": \"section\",\n" + | |
" \"text\": {\n" + | |
" \"type\": \"mrkdwn\",\n" + | |
" \"text\": \""; | |
slackBlocks += item.username+" @ "+ item.date.replace("+0000", "") + "\n"; | |
slackBlocks += item.message.replace("\"","'"); | |
for (int index =0; index < item.urls.size(); index++){ | |
String url = item.urls.get(index); | |
if (url.startsWith("media_url")) { | |
url = url.replace("media_url:", ""); | |
foundImage = true; | |
firstImage = url; | |
} | |
slackBlocks += "\n" + url.replace("\"","'"); | |
} | |
slackBlocks += "\"\n" + | |
" }"; | |
if (foundImage) { | |
slackBlocks += ",\n" + | |
" \"accessory\": {\n" + | |
" \"type\": \"image\",\n" + | |
" \"image_url\": \""; | |
slackBlocks += firstImage; | |
slackBlocks += "\",\n" + | |
" \"alt_text\": \"tweet image\"\n" + | |
" }"; | |
} | |
slackBlocks += " } ,"; | |
} | |
if (slackBlocks.endsWith(",")) { | |
slackBlocks = slackBlocks.substring(0, slackBlocks.length() - 1); | |
} | |
slackBlocks += "]"; | |
PostSlack_BackGround mySlackTask = new PostSlack_BackGround(); | |
String[] params = new String[5]; | |
params[0] = slackPostMessage; | |
params[1] = slackTocken; | |
params[2] = slackChannel; | |
params[3] = slackTitle; | |
params[4] = slackBlocks; | |
String response; | |
mySlackTask.execute(params); | |
try { | |
response = mySlackTask.get(); | |
showToast(response); | |
} catch (InterruptedException e1) { | |
// TODO Auto-generated catch block | |
showToast(e1.getMessage()); | |
return ; | |
} catch (ExecutionException e1) { | |
// TODO Auto-generated catch block | |
showToast(e1.getMessage()); | |
return ; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment