UNPKG

4.47 kBJavaScriptView Raw
1'use strict'
2
3const EventEmitter = require('events').EventEmitter
4
5class Adapter extends EventEmitter {
6 // An adapter is a specific interface to a chat source for robots.
7 //
8 // robot - A Robot instance.
9 constructor (robot) {
10 super()
11 this.robot = robot
12 }
13
14 // Public: Raw method for sending data back to the chat source. Extend this.
15 //
16 // envelope - A Object with message, room and user details.
17 // strings - One or more Strings for each message to send.
18 //
19 // Returns nothing.
20 send (envelope/* , ...strings */) {}
21
22 // Public: Raw method for sending emote data back to the chat source.
23 // Defaults as an alias for send
24 //
25 // envelope - A Object with message, room and user details.
26 // strings - One or more Strings for each message to send.
27 //
28 // Returns nothing.
29 emote (envelope/* , ...strings */) {
30 const strings = [].slice.call(arguments, 1)
31 return this.send.apply(this, [envelope].concat(strings))
32 }
33
34 // Public: Raw method for building a reply and sending it back to the chat
35 // source. Extend this.
36 //
37 // envelope - A Object with message, room and user details.
38 // strings - One or more Strings for each reply to send.
39 //
40 // Returns nothing.
41 reply (envelope/* , ...strings */) {}
42
43 // Public: Raw method for setting a topic on the chat source. Extend this.
44 //
45 // envelope - A Object with message, room and user details.
46 // strings - One more more Strings to set as the topic.
47 //
48 // Returns nothing.
49 topic (envelope/* , ...strings */) {}
50
51 // Public: Raw method for playing a sound in the chat source. Extend this.
52 //
53 // envelope - A Object with message, room and user details.
54 // strings - One or more strings for each play message to send.
55 //
56 // Returns nothing
57 play (envelope/* , ...strings */) {}
58
59 // Public: Raw method for invoking the bot to run. Extend this.
60 //
61 // Returns nothing.
62 run () {}
63
64 // Public: Raw method for shutting the bot down. Extend this.
65 //
66 // Returns nothing.
67 close () {}
68
69 // Public: Dispatch a received message to the robot.
70 //
71 // Returns nothing.
72 receive (message) {
73 this.robot.receive(message)
74 }
75
76 // Public: Get an Array of User objects stored in the brain.
77 //
78 // Returns an Array of User objects.
79 users () {
80 this.robot.logger.warning('@users() is going to be deprecated in 3.0.0 use @robot.brain.users()')
81 return this.robot.brain.users()
82 }
83
84 // Public: Get a User object given a unique identifier.
85 //
86 // Returns a User instance of the specified user.
87 userForId (id, options) {
88 this.robot.logger.warning('@userForId() is going to be deprecated in 3.0.0 use @robot.brain.userForId()')
89 return this.robot.brain.userForId(id, options)
90 }
91
92 // Public: Get a User object given a name.
93 //
94 // Returns a User instance for the user with the specified name.
95 userForName (name) {
96 this.robot.logger.warning('@userForName() is going to be deprecated in 3.0.0 use @robot.brain.userForName()')
97 return this.robot.brain.userForName(name)
98 }
99
100 // Public: Get all users whose names match fuzzyName. Currently, match
101 // means 'starts with', but this could be extended to match initials,
102 // nicknames, etc.
103 //
104 // Returns an Array of User instances matching the fuzzy name.
105 usersForRawFuzzyName (fuzzyName) {
106 this.robot.logger.warning('@userForRawFuzzyName() is going to be deprecated in 3.0.0 use @robot.brain.userForRawFuzzyName()')
107 return this.robot.brain.usersForRawFuzzyName(fuzzyName)
108 }
109
110 // Public: If fuzzyName is an exact match for a user, returns an array with
111 // just that user. Otherwise, returns an array of all users for which
112 // fuzzyName is a raw fuzzy match (see usersForRawFuzzyName).
113 //
114 // Returns an Array of User instances matching the fuzzy name.
115 usersForFuzzyName (fuzzyName) {
116 this.robot.logger.warning('@userForFuzzyName() is going to be deprecated in 3.0.0 use @robot.brain.userForFuzzyName()')
117 return this.robot.brain.usersForFuzzyName(fuzzyName)
118 }
119
120 // Public: Creates a scoped http client with chainable methods for
121 // modifying the request. This doesn't actually make a request though.
122 // Once your request is assembled, you can call `get()`/`post()`/etc to
123 // send the request.
124 //
125 // Returns a ScopedClient instance.
126 http (url) {
127 this.robot.logger.warning('@http() is going to be deprecated in 3.0.0 use @robot.http()')
128 return this.robot.http(url)
129 }
130}
131
132module.exports = Adapter