1. 연구 모듈/텔레그램
텔레그램 봇으로 채널 포스팅 - 7. Clojure 사용 예제 (2)
사용자-1
2017. 9. 14. 06:00
위의 두 글을 참고하면 웹페이지의 특정 class에 해당하는 부분을 추출하여 텔레그램 채널로 보내는 것이 가능하다.
사전 준비사항
app template을 사용하여 프로젝트를 생성한다.
lein new app clj-test-02
project.clj 파일의 dependencies 부분에 아래와 같이 필요한 모듈을 정의한다.
(defproject clj-test-02 "0.1.0-SNAPSHOT" :description "FIXME: write description" :url "http://example.com/FIXME" :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} :dependencies [[org.clojure/clojure "1.8.0"][clj-http "2.2.0"] [enlive "1.1.6" :exclusions [org.clojure/clojure]]] :main ^:skip-aot clj-test-02.core :target-path "target/%s" :profiles {:uberjar {:aot :all}})
예제
(ns clj-test-02.core
(:gen-class)
(:require [net.cgrand.enlive-html :as html])
(:require [clj-http.client :as client]))
(use 'clojure.pprint)
(defn html-data []
;; clj-html 모듈을 사용하여 웹페이지의 내용을 가져온다.
(html/html-resource (java.io.StringReader.
(:body (client/get "http://httpbin.org"))))
)
(defn -main [& args]
(pprint (client/post "https://api.telegram.org//bot<텔레그램 봇 토큰>/sendMessage"
{:body (format "{\"chat_id\": \"@<채널 ID>\", \"text\": \"%s\"}"
(first (:content (first (html/select (html-data) [:.bash])))))
:headers {"Content-Type" "application/json"}}))
)
<테스트 환경> - OS : Windows 7 - Leiningen 버전 : 1.0.0