iT邦幫忙

5

[筆記]使用java簡單製作line提示

code 2018-08-23 13:28:007406 瀏覽
  • 分享至 

  • xImage
  •  

這是我第一次在it邦發文章,主要是做一下筆記,順便和大家技術交流,請各位指教

首先是IFTTT,說明請參照IFTTT 發送 LINE 訊息通知

再來是java的類別部分

JMaker.java

package javaapplication4;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;

public class JMaker {

    private String eventName;
    private String key;

    public JMaker(String eventName, String key) {
        this.eventName = eventName;
        this.key = key;
    }

    public void trigger() throws IOException {
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost request = new HttpPost("https://maker.ifttt.com/trigger/" + eventName + "/with/key/" + key);
        httpClient.execute(request);
    }

    public void trigger(String values) throws IOException {
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost request = new HttpPost("https://maker.ifttt.com/trigger/" + eventName + "/with/key/" + key);
        StringEntity params = new StringEntity(buildJson(values), "UTF-8");
        request.addHeader("content-type", "application/json");
        request.setEntity(params);
        httpClient.execute(request);
    }

    //2018-07-25 確認是否發送
    public void trigger(String values, int state) throws IOException {
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost request = new HttpPost("https://maker.ifttt.com/trigger/" + eventName + "/with/key/" + key);
        if (state == 0) {
            StringEntity params = new StringEntity(buildJson(values), "UTF-8");
            request.addHeader("content-type", "application/json");
            request.setEntity(params);
            httpClient.execute(request);
        } else {
            httpClient.execute(request);
        }

    }

    private String buildJson(String values) throws UnsupportedEncodingException {
        String json = "{";

        json += "\"value1\":\"" + values + "\"";

        json += "}";

        System.out.println("傳送 json: " + json);
        return json;
    }

}

最後是使用的部分

main.java
JMaker maker = new JMaker(IFTTT的eventNam, IFTTT的key);
maker.trigger("test");

lib引用的部分
https://ithelp.ithome.com.tw/upload/images/20180823/20105113SRf77Yi6qL.jpg

完成的樣子
https://ithelp.ithome.com.tw/upload/images/20180823/20105113MGXB0L7jOr.jpg

最後再次讚嘆google大神的偉大,也感謝各位大大無私的分享,在此貢獻小弟一點經驗,回饋大家


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
神Q超人
iT邦研究生 5 級 ‧ 2018-08-23 18:51:31

我之前也有用python小玩一下LINE的機器人
不過後來就沒繼續碰了/images/emoticon/emoticon37.gif

我要留言

立即登入留言