Fill puppet metadata and report connectivity status

This commit is contained in:
Inex Code 2021-01-22 20:27:16 +00:00
parent ce4416c84a
commit ca0ea310a4
2 changed files with 62 additions and 37 deletions

View file

@ -103,7 +103,19 @@ async function run() {
puppet.setGetDescHook(async (puppetId: number, data: any): Promise<string> => { puppet.setGetDescHook(async (puppetId: number, data: any): Promise<string> => {
// here we receive the puppet ID and the data associated with that puppet // here we receive the puppet ID and the data associated with that puppet
// we are expected to return a displayable name for that particular puppet // we are expected to return a displayable name for that particular puppet
return `VK puppet ${data.name}`; let s = "VK";
if (data.isUserToken) {
s += " as user";
} else {
s += " as group";
}
if (data.username) {
s += ` ${data.username}`;
}
if (data.id) {
s += ` (${data.id})`;
}
return s;
}); });
// required: get data from string hook // required: get data from string hook
puppet.setGetDataFromStrHook(async (str: string): Promise<IRetData> => { puppet.setGetDataFromStrHook(async (str: string): Promise<IRetData> => {

View file

@ -135,6 +135,7 @@ export class VkPuppet {
} }
// usually we create a client class of some sorts to the remote protocol // usually we create a client class of some sorts to the remote protocol
// and listen to incoming messages from it // and listen to incoming messages from it
try {
const client = new VK({ token: data.token, apiLimit: 20 }); const client = new VK({ token: data.token, apiLimit: 20 });
log.info("Trying to init listener with", data.token); log.info("Trying to init listener with", data.token);
@ -164,22 +165,34 @@ export class VkPuppet {
} }
}); });
try { try {
await client.api.groups.getById({}); const linkedGroupInfo = await client.api.groups.getById({});
log.info("Got group token"); log.info("Got group token");
data.isUserToken = false; data.isUserToken = false;
data.username = linkedGroupInfo[0].name;
data.id = linkedGroupInfo[0].id;
} catch (err) { } catch (err) {
log.info("Got user token"); log.info("Got user token");
data.isUserToken = true; data.isUserToken = true;
const linkedUserInfo = await client.api.account.getProfileInfo({});
data.username = `${linkedUserInfo.first_name} ${linkedUserInfo.last_name}`;
data.id = linkedUserInfo.id;
} }
this.puppets[puppetId] = { this.puppets[puppetId] = {
client, client,
data, data,
}; };
await this.puppet.setUserId(puppetId, data.id);
await this.puppet.setPuppetData(puppetId, data);
try { try {
await client.updates.start(); await client.updates.start();
await this.puppet.sendStatusMessage(puppetId, "Connected!");
} catch (err) { } catch (err) {
await this.puppet.sendStatusMessage(puppetId, `Connection failed! ${err}`);
log.error("Failed to initialize update listener", err); log.error("Failed to initialize update listener", err);
} }
} catch (err) {
await this.puppet.sendStatusMessage(puppetId, `Connection failed! ${err}`);
}
} }
public async deletePuppet(puppetId: number) { public async deletePuppet(puppetId: number) {