Skip to content

Instantly share code, notes, and snippets.

@pich4ya
Created October 17, 2017 05:40
Show Gist options
  • Save pich4ya/1fbec9cb826ee94c55c10317e8fdac7c to your computer and use it in GitHub Desktop.
Save pich4ya/1fbec9cb826ee94c55c10317e8fdac7c to your computer and use it in GitHub Desktop.
Line Notify - send message from line notify bot to user who generates the token / group
// https://notify-bot.line.me/en/ < login & gen token
// https://notify-bot.line.me/static/pdf/line-notify-api.pdf
// https://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientCustomContext.java
// http://www-us.apache.org/dist//httpcomponents/httpclient/binary/httpcomponents-client-4.5.3-bin.zip
// curl -X POST https://notify-api.line.me/api/notify -H "Authorization: Bearer xxx" -H 'Content-Type: application/x-www-form-urlencoded' -d 'message=test'
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.*;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class LINEBot1 {
public final static void main(String[] args) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();
// Create local HTTP context
HttpClientContext localContext = HttpClientContext.create();
// Bind custom cookie store to the local context
localContext.setCookieStore(cookieStore);
HttpPost httpget = new HttpPost("https://notify-api.line.me/api/notify");
httpget.addHeader("Authorization", "Bearer <GenAuthenCode>");
httpget.addHeader("Content-Type", "application/x-www-form-urlencoded");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("message", "test"));
httpget.setEntity(new UrlEncodedFormEntity(params));
System.out.println("Executing request " + httpget.getRequestLine());
// Pass local context as a parameter
CloseableHttpResponse response = httpclient.execute(httpget, localContext);
// try {
// System.out.println("----------------------------------------");
// System.out.println(response.getStatusLine());
// List<Cookie> cookies = cookieStore.getCookies();
// for (int i = 0; i < cookies.size(); i++) {
// System.out.println("Local cookie: " + cookies.get(i));
// }
// EntityUtils.consume(response.getEntity());
// } finally {
// response.close();
// }
} finally {
httpclient.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment