Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How Do I: Send this template #943

Closed
JaimeCasillasBluu opened this issue Jun 22, 2024 · 1 comment
Closed

How Do I: Send this template #943

JaimeCasillasBluu opened this issue Jun 22, 2024 · 1 comment
Assignees
Labels
question Question about how to use the SDK

Comments

@JaimeCasillasBluu
Copy link

JaimeCasillasBluu commented Jun 22, 2024

I have this code it works when i want to send a template from a whatsapp number and have an application created in the dashboard:
require('dotenv').config({ path: '/Users/jaime/Documents/Bluu/templateFoto/.env' });

const VONAGE_API_KEY = process.env.VONAGE_API_KEY;
const VONAGE_API_SECRET = process.env.VONAGE_API_SECRET;
const VONAGE_APPLICATION_ID = process.env.VONAGE_APPLICATION_ID;
const VONAGE_PRIVATE_KEY = process.env.VONAGE_PRIVATE_KEY;

const TO_NUMBER = process.env.TO_NUMBER;
const WHATSAPP_NUMBER = process.env.WHATSAPP_NUMBER;
const WHATSAPP_TEMPLATE_NAME = process.env.WHATSAPP_TEMPLATE_NAME;
const IMAGE_URL = process.env.IMAGE_URL;
const WHATSAPP_TEMPLATE_REPLACEMENT_TEXT = process.env.WHATSAPP_TEMPLATE_REPLACEMENT_TEXT;

const { Vonage } = require('@vonage/server-sdk');
const { WhatsAppCustom } = require('@vonage/messages');

const vonage = new Vonage({
apiKey: VONAGE_API_KEY,
apiSecret: VONAGE_API_SECRET,
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_PRIVATE_KEY,
});

vonage.messages.send(
new WhatsAppCustom({
custom: {
type: 'template',
template: {
name: ${WHATSAPP_TEMPLATE_NAME},
language: {
policy: 'deterministic',
code: 'es_MX'
},
components: [
{
type: 'header',
parameters: [
{
type: 'image',
image: {
link: ${IMAGE_URL}
}
}
]
},
{
type: 'body',
parameters: [
{
type: 'text',
text: ${WHATSAPP_TEMPLATE_REPLACEMENT_TEXT}
}
]
}
]
}
},
to: TO_NUMBER,
from: WHATSAPP_NUMBER,
}),
)
.then(resp => console.log(resp.message_uuid))
.catch(err => console.error(err));

The problem is, How do i send this code without using the private key? I want to sent a template from the same number a whatsapp bot agent is running, and getting a new private key is going to kill the bot.

Once the vonage support via email told me i can get a JWT each time i want to send a message and i have this function:
async function obtenerJWTToken() {
try {
const response = await axios.get('https://studio-api-eu.ai.vonage.com/agents/65f31dbc22c0c4204fc065f2', {
headers: {
'X-Vgai-Key': '//MYKEYhere'
}
});
JWT_TOKEN = response.data.token;
console.log("Token actualizado: ", JWT_TOKEN);
} catch (error) {
console.error("Error al obtener el JWT Token: ", error);
}
}

But the thing here is how do i send the first code without the private key?

@JaimeCasillasBluu JaimeCasillasBluu added the question Question about how to use the SDK label Jun 22, 2024
@manchuck
Copy link
Contributor

Duplicate of #942

@manchuck manchuck marked this as a duplicate of #942 Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question about how to use the SDK
Projects
None yet
Development

No branches or pull requests

4 participants