forked from nicholasfoden/rocketchat-cryptoconvert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CryptoVertConvertCommand.ts
115 lines (94 loc) · 3.1 KB
/
CryptoVertConvertCommand.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
import { IHttp, IModify, IPersistence, IRead, IHttpRequest, HttpStatusCode } from '@rocket.chat/apps-engine/definition/accessors';
import { ISlashCommand, SlashCommandContext } from '@rocket.chat/apps-engine/definition/slashcommands';
import { CryptoVertStrings } from "./CryptoVertStrings";
import { CryptocompareAPI } from "./CryptocompareAPI";
export class CryptoVertConvertCommand implements ISlashCommand {
public command = 'cryptoconvert';
public i18nParamsExample = "example_convert";
public i18nDescription = "example_convert_description";
public providesPreview = false;
private re: RegExp;
constructor(private readonly api: CryptocompareAPI, private home: string) {
this.re = new RegExp('to|in');
}
public async executor(
context: SlashCommandContext,
read: IRead,
modify: IModify,
http: IHttp,
persis: IPersistence
): Promise<void> {
let args = context.getArguments();
switch (args.length) {
case 2:
return await this.currencyConversionHandler(context, read, modify, http,
{
amount: args[0],
from: args[1].toUpperCase(),
to: this.home
});
case 3:
if (this.re.test(args[2])) {
return await this.invalidUsageHandler(context, modify);
}
else {
return await this.currencyConversionHandler(context, read, modify, http,
{
amount: args[0],
from: args[1].toUpperCase(),
to: args[2]
});
}
case 4:
return await this.currencyConversionHandler(context, read, modify, http,
{
amount: args[0],
from: args[1].toUpperCase(),
to: args[3].toUpperCase()
});
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, CryptoVertStrings.INVALID_COMMAND + CryptoVertStrings.CONVERT_USAGE)
}
private async currencyConversionHandler(
context: SlashCommandContext,
read: IRead,
modify: IModify,
http: IHttp,
data: { amount: string, from: string, to: string }
): Promise<void> {
//Get the price from API
let result = await this.api.getPrice(http, data.from, data.to);
if (result.Response == "Error") {
await this.sendNotifyMessage(context, modify, result.Message ? result.Message : CryptoVertStrings.FAILED_FETCH);
} else {
//convert the price to the amount
let conversion = parseInt(data.amount) * result[data.to];
let message = data.amount + " " + data.from + CryptoVertStrings.EQUAL + "\n" + conversion + " " + data.to;
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(CryptoVertStrings.USERNAME)
.setSender(context.getSender())
.setAvatarUrl(CryptoVertStrings.AVATAR_URL)
.setText(text)
.getMessage();
return await modify.getNotifier().notifyRoom(context.getRoom(), message);
}
}