-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResponseMessage.js
74 lines (60 loc) · 1.93 KB
/
ResponseMessage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const { FieldIsRequiredError } = require('./Error')
class ResponseMessage {
/**
* Response Message is a response template that will be rendered into a telegram message.
*
* Response Message constructor
* @param {string} type
* @param {object} options
*/
constructor(type, options){
ResponseMessage.validate(type, options)
this.type = type
this.owner = options.owner
this.destroy = options.destroy || false
this.keyboard = options.keyboard || null
this.parseMode = options.parse_mode || 'Markdown'
this.inlineKeyboard = options.inline_keyboard || null
if(type != "$delete" ) this.message = options.message
}
/**
* Return response message body.
*/
body(){
const options = {
parse_mode : this.parseMode,
reply_markup: {}
}
const base = {
owner: this.owner,
type : this.type,
destroy: this.destroy
}
if(this.type != "$delete") base['message'] = this.message
if(this.keyboard != null)
options.reply_markup['keyboard'] = this.keyboard
if(this.inlineKeyboard != null )
options.reply_markup['inline_keyboard'] = this.inlineKeyboard
return {
...base,
options
}
}
/**
* Validate parameters for Response Message
*
* @param {string} type
* @param {object} options
*/
static validate(type, options){
if(typeof type != 'string')
throw new TypeError("type must be a 'string'")
if(options.owner == undefined)
throw new FieldIsRequiredError("'owner' is required!")
if(type != "$delete" && options.message == undefined)
throw new FieldIsRequiredError("'Message' is required!")
}
}
module.exports = {
ResponseMessage
}