Created
July 21, 2015 12:42
-
-
Save masahitojp/162db4b83a940fd64e53 to your computer and use it in GitHub Desktop.
Slack-XMPP bot by Java
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
apply plugin: 'java' | |
sourceCompatibility = 1.8 | |
targetCompatibility = 1.8 | |
def defaultEncoding = 'UTF-8' | |
tasks.withType(AbstractCompile)*.options*.encoding = defaultEncoding | |
tasks.withType(GroovyCompile)*.groovyOptions*.encoding = defaultEncoding | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
compile "org.igniterealtime.smack:smack-java7:4.1.3" | |
compile "org.igniterealtime.smack:smack-tcp:4.1.3" | |
compile "org.igniterealtime.smack:smack-im:4.1.3" | |
compile "org.igniterealtime.smack:smack-extensions:4.1.3" | |
} |
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
package com.github.masahitojp; | |
import org.jivesoftware.smack.*; | |
import org.jivesoftware.smack.tcp.XMPPTCPConnection; | |
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration; | |
import org.jivesoftware.smackx.muc.MultiUserChat; | |
import org.jivesoftware.smackx.muc.MultiUserChatManager; | |
import java.io.IOException; | |
public class SlackBot { | |
// TODO 設定から読み込む | |
static String SLACK_HOST_NAME = "SLACK_HOST_NAME"; | |
static String SLACK_HOST_USER = "SLACK_HOST_USER"; | |
static String SLACK_HOST_PASSWORD = "SLACK_HOST_PASSWORD"; | |
static String SLACK_ROOM_NAME = "general"; | |
static public void main(String[] Args) { | |
final XMPPTCPConnectionConfiguration connConfig = XMPPTCPConnectionConfiguration | |
.builder() | |
.setServiceName(SLACK_HOST_NAME) | |
.setCompressionEnabled(false) | |
.build(); | |
final AbstractXMPPConnection connection = new XMPPTCPConnection(connConfig); | |
try { | |
connection.connect(); | |
connection.login(SLACK_HOST_USER, SLACK_HOST_PASSWORD); | |
final MultiUserChatManager mucm = MultiUserChatManager.getInstanceFor(connection); | |
final MultiUserChat muc = mucm.getMultiUserChat(SLACK_ROOM_NAME + "@conference." + SLACK_HOST_NAME); | |
muc.addMessageListener(message -> { | |
System.out.println("Received message: " + message); | |
final String body = message.getBody(); | |
if (body != null) { | |
try { | |
if (message.getBody().equals("ping")) { | |
muc.sendMessage("pong"); | |
} | |
} catch (SmackException.NotConnectedException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
}); | |
muc.join(SLACK_HOST_USER, SLACK_HOST_PASSWORD); | |
muc.sendMessage("connected at " + System.currentTimeMillis()); | |
while(true) { | |
Thread.sleep(1000); | |
} | |
} catch (XMPPException | SmackException | IOException | InterruptedException e) { | |
e.printStackTrace(); | |
} finally { | |
connection.disconnect(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment