Coder Social home page Coder Social logo

javaflock's Introduction

javaflock

javaflock is a java client for FlockOS.

Installation

Maven

Add the JitPack repository to your build file

<repositories>
	<repository>
	    <id>jitpack.io</id>
	    <url>https://jitpack.io</url>
	</repository>
</repositories>

Add the dependency

<dependency>
    <groupId>com.flock</groupId>
    <artifactId>javaflock</artifactId>
    <version>v1.1</version>
</dependency>

Gradle

Add the JitPack repository to your root build.gradle

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

Add the dependency

dependencies {
	compile 'com.flock:javaflock:v1.1'
}

Usage

Index

Send a simple message

UidResponse messageId = Chat.sendMessage("token", "<to>", "<message>", null);

Sending message asynchronously

ListenableFuture<UidResponse> future = Chat.sendMessageAsync("token", "<to>", "<message>", null);

Send a message using a custom name and profile image

SendAs sendAs = new SendAs().name("HAL-9000").profileImage("https://pbs.twimg.com/profile_images/1788506913/HAL-MC2_400x400.png");
Chat.sendMessage("token", "<to>", "<message>", new SendMessageOptionalParams().sendAs(sendAs));

Send a widget view

WidgetView widget = new WidgetView().src("http://example.com").height(250);
Views views = new Views().widget(widget);
Attachment attachment = new Attachment().title("Test widget").description("<description>").views(views);
// NOTE: attachments is a list of attachments
Chat.sendMessage("<token>", "<to>", "<message>", new SendMessageOptionalParams().attachments(Collections.singletonList(attachment)));

Send a HTML view

HtmlView htmlView = new HtmlView().inline("A valid <b>html</b> here").height(50);
Views views = new Views().html(htmlView);
Attachment attachment = new Attachment().title("Test html").description("<description>").views(views);
Chat.sendMessage("<token>", "<to>", "<message>", new SendMessageOptionalParams().attachments(Collections.singletonList(attachment)));

Send a FlockML view

Views views = new Views().flockml("<flockml>FlockML is <b>AWESOME</b></flockml>");
Attachment attachment = new Attachment().title("Test flockml").description("<description>").views(views);
Chat.sendMessage("<token>", "<to>", "<message>", new SendMessageOptionalParams().attachments(Collections.singletonList(attachment)));

Send a Image view

Image image = new Image().src("http://library.acropolis.org/wp-content/uploads/2014/11/One_ring.png").width(400).height(400);
ImageView imageView = new ImageView().original(image).filename("onering.png");
Views views = new Views().image(imageView)
Attachment attachment = new Attachment().title("Test imageview").description("<description>").views(views);
Chat.sendMessage("<token>", "<to>", "<message>", new SendMessageOptionalParams().attachments(Collections.singletonList(attachment)));

Send download files

AttachmentDownload attachmentDownload = new AttachmentDownload().src("http://wallpapercave.com/wp/H630T6R.jpg");
Views views = new Views().flockml("<flockml>Download the <i>matrix</i></flockml>")
// NOTE: downloads is always a list
Attachment attachment = new Attachment().title("Test file").downloads(Collections.singletonList(attachmentDownload)).views(views);
Chat.sendMessage("<token>", "<to>", "<message>", new SendMessageOptionalParams().attachments(Collections.singletonList(attachment)));

Button with openwidget, open url & send to app service

AttachmentButton b1 = new AttachmentButton().name("Harry Potter").id("harry").action(new ActionConfig().type("openWidget").url("https://goo.gl/aygRGf").desktopType("sidebar"));
AttachmentButton b2 = new AttachmentButton().name("Ron Weasley").id("ron").action(new ActionConfig().type("openBrowser").url("https://goo.gl/gDpMVn").sendContext(true));
AttachmentButton b3 = new AttachmentButton().name("Hermione Granger").id("hermione").action(new ActionConfig().type("sendEvent"));
Attachment attachment = new Attachment().title("Test buttons").buttons(Arrays.asList(b1,b2,b3));
Chat.sendMessage("<token>", "<to>", "Who is your favourite Harry Potter character?", new SendMessageOptionalParams().attachments(Collections.singletonList(attachment)));

Now, just for fun, let us change colours

Attachment attachment = new Attachment().title("Test colour").color("#FF0000").description("It is red!");
Chat.sendMessage("<token>", "<to>", "<message>", new SendMessageOptionalParams().attachments(Collections.singletonList(attachment)));

Fetching messages

Message[] messages = Chat.fetchMessages("<token>", "<chat>", uids);

Channel APIs

Get channel info

Channel channel = Channels.getInfo("<token>", channelId);

Get channel members

PublicProfile[] members = Channels.getMembers("<token>", channelId);

Get channels list of which user is member of

Channel[] channelList = Channels.list("<token>");

User APIs

Get user info

User userInfo = Users.getInfo("<token>");

Get user's public profile

User userInfo = Users.getPublicProfile("<token>", userId);

Contact APIs

Get all contacts

PublicProfile[] contacts = Roster.listContacts("<token>");

Handling events

Create instance of EventHandlerClient

EventHandlerClient client = new EventHandlerClient("<appID>", "<appSecret>");

Attach eventListeners with the client (these will be fired when an event occurs)

client.setAppInstallListener(event -> {
	// handle app install event here
});
client.setClientSlashCommandListener(event -> {
    // handle slash command event here
    ...
    // return the response for the event
    return new ToastMessage().text("<toast message to be shown>");
});

Attach eventHandlerClient with your web framework by calling client.handleRequest when event request is received

HttpServletResponse response = client.handleRequest(request, response);

Verifying event token

TokenVerifier tokenVerifier = new TokenVerifier("<appId>", "<appSecret>");
boolean isTokenVerified = tokenVerifier.verifyToken("<eventToken>", "<userId>");

javaflock's People

Contributors

amnsinghl avatar dev8546 avatar prateeksultania avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.