Created
August 16, 2014 10:32
-
-
Save sifue/2846d02c0522102caa94 to your computer and use it in GitHub Desktop.
Slack API Client for Message Posting with API limit rating
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
import java.util.concurrent.Semaphore | |
import dispatch._ | |
import dispatch.Defaults._ | |
/** | |
* SlackのAPIへの操作を行うクライアント | |
* 制限に引っかかららないように1秒間に1回しか実行できない制御をかけてある | |
*/ | |
object SlackClient { | |
val conf = RankingIrcbot.getConf() | |
val token = conf.getProperty("slack.token", "") | |
val username = conf.getProperty("slack.username", "") | |
val iconUrl = conf.getProperty("slack.icon_url", "") | |
val available = new Semaphore(1, true) | |
val apiLimitWaitMilliSec = 1000L | |
def postMessage(message:String, channelName:String): Unit = { | |
try { | |
available.acquire(); | |
val request = url("https://slack.com/api/chat.postMessage") | |
val requestWithParameters = request | |
.POST | |
.addParameter("token", token) | |
.addParameter("channel", channelName) | |
.addParameter("text", message) | |
.addParameter("username", username) | |
.addParameter("icon_url", iconUrl) | |
.secure | |
val response = Http(requestWithParameters OK as.String).apply() | |
if(response.contains("\"ok\":false")) System.out.println(response) | |
Thread.sleep(apiLimitWaitMilliSec) | |
} catch { | |
case e: Throwable => e.printStackTrace() | |
} finally { | |
available.release() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment