Unverified Commit 6710560d authored by Bradley Hilton's avatar Bradley Hilton
Browse files

Change the guggy rocketlet to be more advanced

parent f13572f6
......@@ -13,9 +13,14 @@ export class ServerCommandBridge implements IRocketletCommandBridge {
}
public doesCommandExist(command: string, rocketletId: string): boolean {
console.log('Checking if the command exists:', command);
return this.commands.has(command);
}
public enableCommand(command: string, rocketletId: string): void {
console.log(`Enabling the command "${command}" per request of the rocketlet: ${rocketletId}`);
}
public disableCommand(command: string, rocketletId: string): void {
console.log(`Disabling the command "${command}" per request of the rocketlet: ${rocketletId}`);
}
......
This diff is collapsed.
import { IConfigurationExtend, ILogger } from 'temporary-rocketlets-ts-definition/accessors';
import {
IConfigurationExtend,
IConfigurationModify,
IEnvironmentRead,
IHttp,
ILogger,
IRead,
} from 'temporary-rocketlets-ts-definition/accessors';
import { IRocketletInfo } from 'temporary-rocketlets-ts-definition/metadata';
import { Rocketlet } from 'temporary-rocketlets-ts-definition/Rocketlet';
import { SettingType } from 'temporary-rocketlets-ts-definition/settings';
import { ISetting, SettingType } from 'temporary-rocketlets-ts-definition/settings';
import { GuggyCommand } from './commands/GuggyCommand';
import { GuggyGetter } from './getters/GuggyGetter';
import { SettingToHttpHeader } from './handlers/SettingToHttpHeader';
export class GuggyRocketlet extends Rocketlet {
private readonly apiKeySettingid: string;
private readonly guggyGetter: GuggyGetter;
constructor(info: IRocketletInfo, logger: ILogger) {
super(info, logger);
this.apiKeySettingid = 'api-key';
this.guggyGetter = new GuggyGetter();
}
protected extendConfiguration(configuration: IConfigurationExtend): void {
public onEnable(environmentRead: IEnvironmentRead, configModify: IConfigurationModify): boolean {
if (!environmentRead.getSettings().getValueById(this.apiKeySettingid)) {
configModify.slashCommands.disableSlashCommand('guggy');
}
return true;
}
// tslint:disable-next-line:max-line-length
public onSettingUpdated(setting: ISetting, configModify: IConfigurationModify, read: IRead, http: IHttp): void {
switch (setting.id) {
case this.apiKeySettingid:
this.handleApiKeySettingHandle(setting, configModify, http);
break;
}
}
protected extendConfiguration(configuration: IConfigurationExtend, environmentRead: IEnvironmentRead): void {
configuration.settings.provideSetting({
id: this.apiKeySettingid,
type: SettingType.STRING,
......@@ -28,6 +55,21 @@ export class GuggyRocketlet extends Rocketlet {
configuration.http.provideDefaultHeader('Content-Type', 'application/json');
configuration.http.providePreRequestHandler(new SettingToHttpHeader(this.apiKeySettingid));
configuration.slashCommands.provideSlashCommand(new GuggyCommand());
configuration.slashCommands.provideSlashCommand(new GuggyCommand(this.guggyGetter));
}
private handleApiKeySettingHandle(setting: ISetting, configModify: IConfigurationModify, http: IHttp): void {
if (setting.value) {
try {
this.guggyGetter.getTheGif(http, 'testing');
configModify.slashCommands.enableSlashCommand('guggy');
} catch (e) {
// Not valid api key
configModify.slashCommands.disableSlashCommand('guggy');
}
} else {
// There is no value, so remove the command
configModify.slashCommands.disableSlashCommand('guggy');
}
}
}
import { IHttp, IModify, IRead } from 'temporary-rocketlets-ts-definition/accessors';
import { ISlashCommand, SlashCommandContext } from 'temporary-rocketlets-ts-definition/slashcommands';
import { GuggyGetter } from '../getters/GuggyGetter';
export class GuggyCommand implements ISlashCommand {
public command = 'guggy';
public paramsExample = 'Guggy_Text_On_Image';
public i18nDescription = 'Guggy_Command_Description';
private readonly url: string = 'http://text2gif.guggy.com/guggify';
constructor(private readonly getter: GuggyGetter) { }
public executor(context: SlashCommandContext, read: IRead, modify: IModify, http: IHttp): void {
const result = http.post(this.url, {
data: {
format: 'gif',
sentence: context.getArguments().join(' '),
},
});
const builder = modify.getCreator().startMessage().setSender(context.getSender()).setRoom(context.getRoom());
if (result.data && result.data.gif) {
const gifUrl = result.data.gif as string;
try {
const gifUrl = this.getter.getTheGif(http, context.getArguments().join(' '));
builder.addAttachment({ imageUrl: gifUrl });
} else {
} catch (e) {
builder.setText('Sorry I don\'t have a photo for you :disappointed_relieved:');
}
......
import { HttpStatusCode, IHttp } from 'temporary-rocketlets-ts-definition/accessors';
export class GuggyGetter {
private readonly url: string = 'http://text2gif.guggy.com/guggify';
public getTheGif(http: IHttp, sentence: string): string {
const result = http.post(this.url, {
data: {
format: 'gif',
sentence,
},
});
if (result.statusCode === HttpStatusCode.OK && result.data && result.data.gif) {
return result.data.gif as string;
} else {
throw new Error('No valid link found.');
}
}
}
......@@ -14,13 +14,11 @@ export class SettingToHttpHeader implements IHttpPreRequestHandler {
// tslint:disable-next-line:max-line-length
public executePreHttpRequest(url: string, request: IHttpRequest, read: IRead, persistence: IPersistence): IHttpRequest {
// const apiKey = read.getEnvironmentReader().getSettings().getValueById(this.apiKeyId);
const apiKey = read.getEnvironmentReader().getSettings().getValueById(this.apiKeyId);
// if (typeof apiKey !== 'string' || apiKey.length === 0) {
// throw new Error('Invalid Guggy Api Key!');
// }
const apiKey = '8MPFcbGNTEFXanv';
if (typeof apiKey !== 'string' || apiKey.length === 0) {
throw new Error('Invalid Guggy Api Key!');
}
if (typeof request.headers === 'undefined') {
request.headers = { };
......
......@@ -3,8 +3,8 @@
"name": "Guggy Command",
"nameSlug": "guggy-command",
"description": "Provides a command for generating gifs based upon text provided.",
"version": "1.0.3",
"requiredApiVersion": ">=0.6.20",
"version": "1.0.6",
"requiredApiVersion": ">=0.6.29",
"author": {
"name": "Bradley Hilton",
"homepage": "https://github.com/graywolf336/temporary-rocketlets-dev-environment",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment