티스토리 뷰

개발/java

텔레그램 봇(bot) 메시지 보내기 샘플 소스

개발자와코더사이가 PM일까? 2021. 3. 9. 09:48
반응형
package zitsm.framework.notice;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import zitsm.framework.exception.ZException;

public class ZTelegram {
	
	public ZTelegram() throws ZException {
		String Token = "tokenID";
		String chat_id = "chart_id";
		String text = "메시지 전송 테스트" ;
		
		BufferedReader in = null;
		
		 try {
			 URL obj = new URL("https://api.telegram.org/bot" + Token + "/sendmessage?chat_id=" + chat_id + "&text=" + text); // 호출할 url
			 
			 HttpURLConnection con = (HttpURLConnection)obj.openConnection();
			 con.setRequestMethod("GET");
			 in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
			 String line;
			 
			 while((line = in.readLine()) != null) { // response를 차례대로 출력
				 System.out.println(line);
			 }		 
			 
		 } catch(Exception e) {
			 e.printStackTrace();
		 } finally {
			 if(in != null) try { in.close(); } catch(Exception e) { e.printStackTrace(); }
		 }
    	
    }
}

텔레그램에 bot를 이용해서 메시지 보내는 java 샘플 소스이다.

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함