-
Notifications
You must be signed in to change notification settings - Fork 1
/
CryptoVertPriceCommand.ts
116 lines (91 loc) · 3.26 KB
/
CryptoVertPriceCommand.ts
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { IHttp, IModify, IPersistence, IRead, IEnvironmentRead, IHttpRequest, HttpStatusCode } from '@rocket.chat/apps-engine/definition/accessors';
import { ISlashCommand, SlashCommandContext } from '@rocket.chat/apps-engine/definition/slashcommands';
import { Messages } from "./CryptoVertStrings";
import { CryptocompareAPI } from "./CryptocompareAPI";
import { CryptoVertSettings } from "./CryptoVertSettings";
import { APIKEY } from "./devkey";
export class CryptoVertPriceCommand implements ISlashCommand {
public command = 'price';
public i18nParamsExample = Messages.PRICE_EXAMPLE;
public i18nDescription = Messages.PRICE_DESC;
public providesPreview = false;
constructor(private readonly api: CryptocompareAPI, public home: string) { }
public async executor(
context: SlashCommandContext,
read: IRead,
modify: IModify,
http: IHttp,
persis: IPersistence
): Promise<void> {
let args = context.getArguments();
switch (args.length) {
case 1:
return await this.priceHandler(context, read, modify, http, persis, args[0].toUpperCase());
case 3:
case 4:
case 5:
return await this.multiHandler(context, read, modify, http, persis, args[0], args.slice(2, args.length) );
default:
return await this.invalidUsageHandler(context, modify);
}
}
private async invalidUsageHandler(
context: SlashCommandContext,
modify: IModify
): Promise<void> {
//TODO use enum for std messages
await this.sendNotifyMessage(context, modify, Messages.INVALID_COMMAND + Messages.PRICE_USAGE)
}
private async priceHandler(
context: SlashCommandContext,
read: IRead,
modify: IModify,
http: IHttp,
persis: IPersistence,
from: string,
): Promise<void> {
//Get the price from API
let result = await this.api.getPrice(http, from, this.home);
//TODO enum standard messages
if (result.Response == "Error"){
await this.sendNotifyMessage(context, modify, result.Message ? result.Message : Messages.FAILED_FETCH);
} else {
//convert the price to the amount
let message = from + ": \n" + result[this.home] + " " + this.home;
await this.sendNotifyMessage(context, modify, message);
}
}
private async multiHandler(
context: SlashCommandContext,
read: IRead,
modify: IModify,
http: IHttp,
persis: IPersistence,
from: string,
args: Array<string>
): Promise<void> {
let result = await this.api.getPrices(http, from, args);
if (result.Response == "Error"){
await this.sendNotifyMessage(context, modify, result.Message ? result.Message : Messages.FAILED_FETCH);
} else {
let message = args.reduce((mess, arg) => mess.concat(" ", arg, ": ", result[arg], " "), from + ": \n ");
await this.sendNotifyMessage(context, modify, message);
}
}
private async sendNotifyMessage(
context: SlashCommandContext,
modify: IModify,
text: string
): Promise<void> {
const message = modify.getCreator()
.startMessage()
.setGroupable(false)
.setRoom(context.getRoom())
.setUsernameAlias(Messages.USERNAME)
.setSender(context.getSender())
.setAvatarUrl(Messages.AVATAR_URL)
.setText(text)
.getMessage();
return await modify.getNotifier().notifyRoom(context.getRoom(), message);
}
}