Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 56 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,83 @@
# HW Week 3 - SLACK BOT CHALLENGE 🤖 !!!
<br>
A [Slack bot](https://api.slack.com/bot-users) is a non-human "user" that interacts with the Slack messaging app. Bots might post messages to users or to channels, send reminders, look up information in response to a question or perform a calculation. In this project, we'll work in teams to build bots that can preform simple interactions with the AccessCode3-3 Slack.
A [Slack bot](https://api.slack.com/bot-users) is a non-human "user" that interacts with the Slack messaging app.
Bots might post messages to users or to channels, send reminders, look up information in response to a question
or perform a calculation. In this project, we'll work in teams to build bots that can preform simple
interactions with the AccessCode3-3 Slack.

![slack bot example](https://api.slack.com/img/api/guide_bot_user.png)

Slack offers three different APIs that developers can use to interact with their service: the Web API, the Real Time Messaging API and the Events API . In this project, bots will only interact using the [Web API](https://api.slack.com/web). The Web API offers methods that can be used to list AccessCode3-3 channels, view message history on a given channel, and post and delete messages on the #bots channel.
Slack offers three different APIs that developers can use to interact with their service:
the Web API, the Real Time Messaging API and the Events API . In this project, bots will only interact using
the [Web API](https://api.slack.com/web). The Web API offers methods that can be used to list AccessCode3-3 channels,



view message history on a given channel, and post and delete messages on the #bots channel.



<br>
## Setup
The following setup steps only need to be completed **once per team**:

1. Fork and clone [this repo](https://github.com/ramonaharrison/SlackBot). Make sure both team members are added as contributors to the forked repo.
1. Fork and clone [this repo](https://github.com/ramonaharrison/SlackBot).
Make sure both team members are added as contributors to the forked repo.

2. Go to https://accesscode3-3.slack.com/apps/new/A0F7YS25R-bots. Make sure you are signed into the AccessCode3-3 Slack team. Choose a username for your bot and click the "Add bot integration" button.
2. Go to https://accesscode3-3.slack.com/apps/new/A0F7YS25R-bots.
Make sure you are signed into the AccessCode3-3 Slack team.
Choose a username for your bot and click the "Add bot integration" button.

3. Make a note of your **API token**. The token must always be kept in a **safe, secret** place. We will be storing our API keys in the `../SlackBot/api_token.txt` file, which has been added to our `.gitignore` file.
3. Make a note of your **API token**. The token must always be kept in a **safe, secret** place.
We will be storing our API keys in the `../SlackBot/api_token.txt` file,
which has been added to our `.gitignore` file.

> Always be careful when sharing API tokens! Be careful to never publish
>Always be careful when sharing API tokens! Be careful to never publish
> our bot user tokens in any public GitHub code repository.

4. Complete your bot's profile. Add an avatar image/emoji, a first and last name and a description of what your bot does. When you are finished, click the "Save Integration" button.
4. Complete your bot's profile. Add an avatar image/emoji, a first and last name and a description
of what your bot does. When you are finished, click the "Save Integration" button.

5. **Both team members:** join the #bots channel on Slack so you can see what your bot is up to.

5. **Both team members:** join the #bots channel on Slack so you can see what your bot is up to.

## Requirements
1. Form a clear picture of the project requirements. Spend some time reading through:
- This readme doc.
- The [Slack Web API docs](https://api.slack.com/web) and [Basic message formatting guidelines](https://api.slack.com/docs/message-formatting).
- The code that has been provided in the `SlackBot` project -- particulary the Slack.java class, which provides methods that your bot can use to interact with Slack's Web API.
- The [Slack Web API docs](https://api.slack.com/web) and
[Basic message formatting guidelines](https://api.slack.com/docs/message-formatting).
- The code that has been provided in the `SlackBot` project -- particulary the Slack.java class,
which provides methods that your bot can use to interact with Slack's Web API.
- Use the IntelliJ TODO panel (⌘6) to navigate to each of the project TODOs.

2. For both of the following Slack API JSON objects, write a Java class to parse and represent it as a Java object. Each team member should be responsible for parsing at least one of the classes:
2. For both of the following Slack API JSON objects, write a Java class to parse and represent it as a Java object.
Each team member should be responsible for parsing at least one of the classes:
- [user](https://api.slack.com/types/user) -> User.java
- [attachment](https://api.slack.com/docs/message-attachments) -> Attachment.java

Attachment.java and User.java files are already provided in the `model` subpackage -- just complete the TODOs as marked. See the Channel.java class for an example of how your completed classes should look.
Attachment.java and User.java files are already provided in the `model` subpackage -- just complete the TODOs
as marked. See the Channel.java class for an example of how your completed classes should look.

3. In Bot.java, design and implement your bot code! It's up to your team to decide what your bot does, but at a minimum, when your program runs it should post a message with some content to the #bots channel. See below for some ideas.
3. In Bot.java, design and implement your bot code! It's up to your team to decide what your bot does, but at
a minimum, when your program runs it should post a message with some content to the #bots channel. See below
for some ideas.

4. (Optional / Bonus!) In `Slack.java`, implement the `sendMessageWithAttachments(String messageText, List<Attachment> attachments)`, which should take in a `String messageText` and a `List<Attachment> attachments` to post to the #bots channel. It should return a `SendMessageResponse`. If your bot will send messages with attachments, you must implement this method. Read https://api.slack.com/docs/message-attachments to familiarize yourself with how attachment URL parameters should be formatted.
4. (Optional / Bonus!) In `Slack.java`, implement the `sendMessageWithAttachments(String messageText,
List<Attachment> attachments)`, which should take in a `String messageText` and a `List<Attachment>
attachments` to post to the #bots channel. It should return a `SendMessageResponse`. If your bot will
send messages with attachments, you must implement this method.
Read https://api.slack.com/docs/message-attachments to familiarize yourself with how attachment URL
parameters should be formatted.

5. Respect the **bot etiquette guidelines**:
- Bots may only post messages to the #bots channel! No direct messages or posting to pre-existing channels like #general or #random.
- Bots may only post messages to the #bots channel! No direct messages or posting to pre-existing channels
like #general or #random.
- No spamming or flooding the channel with messages: bots should post at most one or two messages at a time.
- Keep it classroom friendly :)

## Some bot ideas
- Pig Latin Bot: looks at the text of the last message that was posted to the channel and re-posts it in [pig latin](https://en.wikipedia.org/wiki/Pig_Latin) (or rot13!).
- Pig Latin Bot: looks at the text of the last message that was posted to the channel and re-posts it in [
pig latin](https://en.wikipedia.org/wiki/Pig_Latin) (or rot13!).
- Random Fact Bot: post a random fact or programming tip from a curated list.
- Inspirational Image Bot: post a random image link from a selected list of favorite image URLs.
- Mashup Bot: get and post interesting content from [another API](https://gist.github.com/afeld/4952991).
Expand All @@ -54,7 +86,10 @@ The following setup steps only need to be completed **once per team**:

## Take 15 minutes to discuss the following questions:
- What will your bot do? What messages will it post? Be specific!
- Draw out a rough blueprint for how you will achieve this programmatically -- it's ok if you don't know all the specifics yet, just sketch out some ideas for how your program might flow. Think about: If your bot is posting data (i.e. images, quotes, facts), where is the data coming from? How is your bot interacting with and responding to other messages / users on the channel? For example:
- Draw out a rough blueprint for how you will achieve this programmatically --
it's ok if you don't know all the specifics yet, just sketch out some ideas for how your program might flow.
Think about: If your bot is posting data (i.e. images, quotes, facts), where is the data coming from?
How is your bot interacting with and responding to other messages / users on the channel? For example:
>Cat Bot: The program will have an ArrayList of URLs of cat images that we find on the internet. When it starts, the program will check the last 100 messages on the #bots channel using the `listMessages()` method. If there is a message that contains the word "cat" in its text, the program with post a message with the text "CAT!!!" and a link to one of our cat image URLs.

- How will your team divide tasks?
Expand All @@ -67,6 +102,9 @@ The following setup steps only need to be completed **once per team**:

## Submission

1. For each feature that you work on, create a new branch in your forked SlackBot repo named `XXMMDD_FeatureName`, where `XXMMDD` are your initials and the month and day and `FeatureName` describes the feature you are working on. When you finish each feature, add, commit and push your changes. On GitHub, open a new Pull Request and assign it to your teammate to review and merge.
1. For each feature that you work on, create a new branch in your forked SlackBot repo named
`XXMMDD_FeatureName`, where `XXMMDD` are your initials and the month and day and `FeatureName`
describes the feature you are working on. When you finish each feature, add, commit and push your changes.
On GitHub, open a new Pull Request and assign it to your teammate to review and merge.

2. This assignment is due by ***9pm on Saturday 9/18***. All code should be committed and pushed by the deadline. Each team member should complete the Google Form (check Slack!) to submit.
1 change: 1 addition & 0 deletions src/nyc/c4q/ramonaharrison/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public void listMessages(String channelId) {
* @param text message text.
*/
public void sendMessageToBotsChannel(String text) {

SendMessageResponse sendMessageResponse = Slack.sendMessage(text);

if (sendMessageResponse.isOk()) {
Expand Down
16 changes: 7 additions & 9 deletions src/nyc/c4q/ramonaharrison/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ public static void main(String[] args) {

Bot myBot = new Bot();

myBot.testApi();
//myBot.testApi();
// myBot.listChannels();
// myBot.listMessages(Slack.BOTS_CHANNEL_ID);

myBot.listChannels();
// Post "Hello, world!" to the #bots channel
myBot.sendMessageToBotsChannel(" ");

myBot.listMessages(Slack.BOTS_CHANNEL_ID);

// Post "Hello, world!" to the #bots channel
//myBot.sendMessage("Hello, world!");

// Post a pineapple photo to the #bots channel
//myBot.sendMessage("http://weknowyourdreams.com/images/pineapple/pineapple-07.jpg");
// Post a pineapple photo to the #bots channel
//myBot.sendMessage("http://weknowyourdreams.com/images/pineapple/pineapple-07.jpg");

}
}
153 changes: 135 additions & 18 deletions src/nyc/c4q/ramonaharrison/model/Attachment.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package nyc.c4q.ramonaharrison.model;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
* Created by Ramona Harrison
* on 8/26/16
Expand All @@ -13,27 +18,139 @@

public class Attachment {

// TODO: implement private fields for each of the following attachment JSON keys:
// "fallback"
// "color"
// "pretext"
// "author_name"
// "author_link"
// "author_icon"
// "title"
// "title_link"
// "text"
// "fields"
// "image_url"
// "thumb_url"
// "footer"
// "footer_icon"
// "ts"

private String fallback;
private String color;
private String pretext;
private String authorName;
private String authorLink;
private String authorIcon;
private String title;
private String titleLink ;
private String text;
private List<Fields>fields;
private String imageUrl;
private String thumbUrl;
private String footer;
private String footerIcon;
private long ts;

public Attachment(JSONObject json) {
// TODO: parse an attachment from the incoming json

if (json.containsKey("fallback")){
this.fallback = (String)json.get("fallback");
}

if (json.containsKey("color")){
this.color = (String)json.get("color");
}
if (json.containsKey("pretext")){
this.pretext = (String)json.get("pretext");
}
if (json.containsKey("author_name")){
this.authorName = (String)json.get("author_name");
}
if (json.containsKey("author_link")){
this.authorLink = (String)json.get("author_link");
}
if (json.containsKey("author_icon")){
this.authorIcon = (String)json.get("author_icon");
}
if (json.containsKey("title")){
this.title = (String)json.get("title");
}
if (json.containsKey("title_link")){
this.titleLink = (String)json.get("title_link");
}
if (json.containsKey("text")){
this.text = (String)json.get("text");
}
if (json.containsKey("image_url")){
this.imageUrl = (String)json.get("image_url");
}
if (json.containsKey("thumb_url")){
this.thumbUrl = (String)json.get("thumb_url");
}
if (json.containsKey("footer")){
this.footer = (String)json.get("footer");
}
if (json.containsKey("footer_icon")){
this.footerIcon = (String)json.get("footer_icon");
}

if (json.containsKey("fields")){
JSONArray jsonFields = (JSONArray) json.get("fields");
this.fields = new ArrayList<Fields>();

for (int i = 0; i<jsonFields.size(); i++){
Fields fields = new Fields((JSONObject)jsonFields.get(i));
this.fields.add(fields);
}

}
if (json.containsKey(("ts"))){
this.ts = (Long) json.get("ts");
}
}


public String getFallback() {
return fallback;
}

public String getColor() {
return color;
}

public String getPretext() {
return pretext;
}

public String getAuthorName() {
return authorName;
}

public String getAuthorLink() {
return authorLink;
}

// TODO add getters to access private fields
public String getAuthorIcon() {
return authorIcon;
}

public String getTitle() {
return title;
}

public String getTitleLink() {
return titleLink;
}

public String getText() {
return text;
}

public List<Fields> getFields() {
return fields;
}

public String getImageUrl() {
return imageUrl;
}

public String getThumbUrl() {
return thumbUrl;
}

public String getFooter() {
return footer;
}

public String getFooterIcon() {
return footerIcon;
}

public long getTs() {
return ts;
}
}
38 changes: 38 additions & 0 deletions src/nyc/c4q/ramonaharrison/model/Fields.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package nyc.c4q.ramonaharrison.model;

import org.json.simple.JSONObject;

/**
* Created by meltemyildirim on 9/11/16.
*/
public class Fields {
private String title;
private String value;
private boolean isShort;

public Fields(JSONObject json){
if (json.containsKey("title")){
this.title = (String) json.get("title");
}

if (json.containsKey("value")) {
this.value = (String) json.get("value");
}

if (json.containsKey("short")){
this.isShort =(boolean) json.get("short");
}
}

public String getTitle() {
return title;
}

public String getValue() {
return value;
}

public boolean isShort() {
return isShort;
}
}
2 changes: 1 addition & 1 deletion src/nyc/c4q/ramonaharrison/model/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

public class Message {

private String text;
private String text ;
private String ts;
private String user;
List<Attachment> attachments;
Expand Down
Loading