Fill puppet metadata and report connectivity status
This commit is contained in:
parent
ce4416c84a
commit
ca0ea310a4
14
src/index.ts
14
src/index.ts
|
@ -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> => {
|
||||||
|
|
85
src/vk.ts
85
src/vk.ts
|
@ -135,50 +135,63 @@ 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
|
||||||
const client = new VK({ token: data.token, apiLimit: 20 });
|
try {
|
||||||
log.info("Trying to init listener with", data.token);
|
const client = new VK({ token: data.token, apiLimit: 20 });
|
||||||
|
log.info("Trying to init listener with", data.token);
|
||||||
|
|
||||||
client.updates.on("message_new", async (context) => {
|
client.updates.on("message_new", async (context) => {
|
||||||
|
try {
|
||||||
|
log.info("Recieved something!");
|
||||||
|
await this.handleVkMessage(puppetId, context);
|
||||||
|
} catch (err) {
|
||||||
|
log.error("Error handling vk message event", err.error || err.body || err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
client.updates.on("message_edit", async (context) => {
|
||||||
|
try {
|
||||||
|
log.info("Edit recieved!");
|
||||||
|
await this.handleVkEdit(puppetId, context);
|
||||||
|
} catch (err) {
|
||||||
|
log.error("Error handling vk message event", err.error || err.body || err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
client.updates.on("message_typing_state", async (context) => {
|
||||||
|
if (context.isUser) {
|
||||||
|
const params = await this.getSendParams(puppetId, context.fromId, context.fromId);
|
||||||
|
await this.puppet.setUserTyping(params, context.isTyping);
|
||||||
|
} else {
|
||||||
|
const params = await this.getSendParams(puppetId, 2000000000 + (context?.chatId ?? 0), context.fromId);
|
||||||
|
await this.puppet.setUserTyping(params, context.isTyping);
|
||||||
|
}
|
||||||
|
});
|
||||||
try {
|
try {
|
||||||
log.info("Recieved something!");
|
const linkedGroupInfo = await client.api.groups.getById({});
|
||||||
await this.handleVkMessage(puppetId, context);
|
log.info("Got group token");
|
||||||
|
data.isUserToken = false;
|
||||||
|
data.username = linkedGroupInfo[0].name;
|
||||||
|
data.id = linkedGroupInfo[0].id;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log.error("Error handling vk message event", err.error || err.body || err);
|
log.info("Got user token");
|
||||||
|
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] = {
|
||||||
client.updates.on("message_edit", async (context) => {
|
client,
|
||||||
|
data,
|
||||||
|
};
|
||||||
|
await this.puppet.setUserId(puppetId, data.id);
|
||||||
|
await this.puppet.setPuppetData(puppetId, data);
|
||||||
try {
|
try {
|
||||||
log.info("Edit recieved!");
|
await client.updates.start();
|
||||||
await this.handleVkEdit(puppetId, context);
|
await this.puppet.sendStatusMessage(puppetId, "Connected!");
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log.error("Error handling vk message event", err.error || err.body || err);
|
await this.puppet.sendStatusMessage(puppetId, `Connection failed! ${err}`);
|
||||||
|
log.error("Failed to initialize update listener", err);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
client.updates.on("message_typing_state", async (context) => {
|
|
||||||
if (context.isUser) {
|
|
||||||
const params = await this.getSendParams(puppetId, context.fromId, context.fromId);
|
|
||||||
await this.puppet.setUserTyping(params, context.isTyping);
|
|
||||||
} else {
|
|
||||||
const params = await this.getSendParams(puppetId, 2000000000 + (context?.chatId ?? 0), context.fromId);
|
|
||||||
await this.puppet.setUserTyping(params, context.isTyping);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
try {
|
|
||||||
await client.api.groups.getById({});
|
|
||||||
log.info("Got group token");
|
|
||||||
data.isUserToken = false;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log.info("Got user token");
|
await this.puppet.sendStatusMessage(puppetId, `Connection failed! ${err}`);
|
||||||
data.isUserToken = true;
|
|
||||||
}
|
|
||||||
this.puppets[puppetId] = {
|
|
||||||
client,
|
|
||||||
data,
|
|
||||||
};
|
|
||||||
try {
|
|
||||||
await client.updates.start();
|
|
||||||
} catch (err) {
|
|
||||||
log.error("Failed to initialize update listener", err);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue