|
1 | 1 | package demo.slack;
|
2 | 2 |
|
3 |
| - |
| 3 | +import demo.configuration.properties.SlackProperties; |
| 4 | +import java.util.regex.Matcher; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
| 6 | +import me.ramswaroop.jbot.core.common.Controller; |
| 7 | +import me.ramswaroop.jbot.core.common.EventType; |
| 8 | +import me.ramswaroop.jbot.core.common.JBot; |
4 | 9 | import me.ramswaroop.jbot.core.slack.Bot;
|
| 10 | +import me.ramswaroop.jbot.core.slack.models.Event; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | +import org.springframework.stereotype.Component; |
| 13 | +import org.springframework.web.socket.WebSocketSession; |
5 | 14 |
|
6 | 15 | /**
|
7 |
| - * @author zacconding |
8 |
| - * @Date 2019-01-16 |
9 |
| - * @GitHub : https://github.com/zacscoding |
| 16 | + * https://github.com/rampatra/jbot/blob/master/jbot-example/src/main/java/example/jbot/slack/SlackBot.java |
10 | 17 | */
|
11 |
| - |
| 18 | +@Slf4j |
| 19 | +@JBot |
| 20 | +@Component |
12 | 21 | public class SlackBot extends Bot {
|
13 | 22 |
|
| 23 | + /** |
| 24 | + * Slack token from application.properties file. You can get your slack token next <a |
| 25 | + * href="https://my.slack.com/services/new/bot">creating a new bot</a>. |
| 26 | + */ |
| 27 | + private SlackProperties slackProperties; |
| 28 | + |
| 29 | + @Autowired |
| 30 | + public SlackBot(SlackProperties slackProperties) { |
| 31 | + this.slackProperties = slackProperties; |
| 32 | + } |
| 33 | + |
14 | 34 | @Override
|
15 | 35 | public String getSlackToken() {
|
16 |
| - return null; |
| 36 | + return slackProperties.getSlackBotToken(); |
17 | 37 | }
|
18 | 38 |
|
19 | 39 | @Override
|
20 | 40 | public Bot getSlackBot() {
|
21 |
| - return null; |
| 41 | + return this; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Invoked when the bot receives a direct mention (@botname: message) or a direct message. NOTE: These two event |
| 46 | + * types are added by jbot to make your task easier, Slack doesn't have any direct way to determine these type of |
| 47 | + * events. |
| 48 | + */ |
| 49 | + @Controller(events = {EventType.DIRECT_MENTION, EventType.DIRECT_MESSAGE}) |
| 50 | + public void onReceiveDM(WebSocketSession session, Event event) { |
| 51 | + log.info("## receive DM. event :: {}", event.getText()); |
| 52 | + reply(session, event, "Hi, I am " + slackService.getCurrentUser().getName()); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Invoked when bot receives an event of type message with text satisfying the pattern {@code ([a-z ]{2})(\d+)([a-z |
| 57 | + * ]{2})}. For example, messages like "ab12xy" or "ab2bc" etc will invoke this method. |
| 58 | + */ |
| 59 | + @Controller(events = EventType.MESSAGE, pattern = "^([a-z ]{2})(\\d+)([a-z ]{2})$") |
| 60 | + public void onReceiveMessage(WebSocketSession session, Event event, Matcher matcher) { |
| 61 | + log.info("## Receive message : {}", event.getText()); |
| 62 | + reply(session, event, "First group: " + matcher.group(0) + "\n" + |
| 63 | + "Second group: " + matcher.group(1) + "\n" + |
| 64 | + "Third group: " + matcher.group(2) + "\n" + |
| 65 | + "Fourth group: " + matcher.group(3)); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Invoked when an item is pinned in the channel. |
| 70 | + */ |
| 71 | + @Controller(events = EventType.PIN_ADDED) |
| 72 | + public void onPinAdded(WebSocketSession session, Event event) { |
| 73 | + log.info("## pin added : {}", event.getText()); |
| 74 | + reply(session, event, "Thanks for the pin! You can find all pinned items under channel details."); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Invoked when bot receives an event of type file shared. NOTE: You can't reply to this event as slack doesn't send |
| 79 | + * a channel id for this event type. You can learn more about |
| 80 | + * <a href="https://api.slack.com/events/file_shared">file_shared</a> |
| 81 | + * event from Slack's Api documentation. |
| 82 | + */ |
| 83 | + @Controller(events = EventType.FILE_SHARED) |
| 84 | + public void onFileShared(WebSocketSession session, Event event) { |
| 85 | + log.info("File shared: {}", event); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + /** |
| 90 | + * Conversation feature of JBot. This method is the starting point of the conversation (as it calls {@link |
| 91 | + * Bot#startConversation(Event, String)} within it. You can chain methods which will be invoked one after the other |
| 92 | + * leading to a conversation. You can chain methods with {@link Controller#next()} by specifying the method name to |
| 93 | + * chain with. |
| 94 | + */ |
| 95 | + @Controller(pattern = "(setup meeting)", next = "confirmTiming") |
| 96 | + public void setupMeeting(WebSocketSession session, Event event) { |
| 97 | + startConversation(event, "confirmTiming"); // start conversation |
| 98 | + reply(session, event, "Cool! At what time (ex. 15:30) do you want me to set up the meeting?"); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * This method will be invoked after {@link SlackBot#setupMeeting(WebSocketSession, Event)}. |
| 103 | + */ |
| 104 | + @Controller(next = "askTimeForMeeting") |
| 105 | + public void confirmTiming(WebSocketSession session, Event event) { |
| 106 | + reply(session, event, "Your meeting is set at " + event.getText() + |
| 107 | + ". Would you like to repeat it tomorrow?"); |
| 108 | + nextConversation(event); // jump to next question in conversation |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * This method will be invoked after {@link SlackBot#confirmTiming(WebSocketSession, Event)}. |
| 113 | + */ |
| 114 | + @Controller(next = "askWhetherToRepeat") |
| 115 | + public void askTimeForMeeting(WebSocketSession session, Event event) { |
| 116 | + if (event.getText().contains("yes")) { |
| 117 | + reply(session, event, "Okay. Would you like me to set a reminder for you?"); |
| 118 | + nextConversation(event); // jump to next question in conversation |
| 119 | + } else { |
| 120 | + reply(session, event, "No problem. You can always schedule one with 'setup meeting' command."); |
| 121 | + stopConversation(event); // stop conversation only if user says no |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * This method will be invoked after {@link SlackBot#askTimeForMeeting(WebSocketSession, Event)}. |
| 127 | + */ |
| 128 | + @Controller |
| 129 | + public void askWhetherToRepeat(WebSocketSession session, Event event) { |
| 130 | + if (event.getText().contains("yes")) { |
| 131 | + reply(session, event, "Great! I will remind you tomorrow before the meeting."); |
| 132 | + } else { |
| 133 | + reply(session, event, "Okay, don't forget to attend the meeting tomorrow :)"); |
| 134 | + } |
| 135 | + stopConversation(event); // stop conversation |
22 | 136 | }
|
23 | 137 | }
|
0 commit comments