2 years ago
So in my local I have .env and the code reads it perfectly. On railway, neither the project scoped variables or the global shared variables can be readed in the following code: const axios = require("axios");
const { logger } = require("../utils/logger");
const { maskToken } = require("../utils/tokenUtils");
const WHATSAPP_API_VERSION = "v17.0";
const WHATSAPP_API_URL = https://graph.facebook.com/${WHATSAPP_API_VERSION};
// Verificar variables de entorno al inicio
logger.info("Checking WhatsApp environment variables...");
const requiredEnvVars = {
WHATSAPP_ACCESS_TOKEN: process.env.WHATSAPP_ACCESS_TOKEN,
WHATSAPP_PHONE_NUMBER_ID: process.env.WHATSAPP_PHONE_NUMBER_ID,
WHATSAPP_VERIFY_TOKEN: process.env.WHATSAPP_VERIFY_TOKEN,
};
// Verificar que todas las variables requeridas existan
const missingVars = Object.entries(requiredEnvVars)
// eslint-disable-next-line no-unused-vars
.filter(([_, value]) => !value)
.map(([key]) => key);
if (missingVars.length > 0) {
throw new Error(
Missing required environment variables: ${missingVars.join(", ")}
);
}
// Log seguro de las variables
logger.info(
"WhatsApp configuration:",
JSON.stringify(
{
ACCESS_TOKEN: maskToken(requiredEnvVars.WHATSAPP_ACCESS_TOKEN),
PHONE_NUMBER_ID: requiredEnvVars.WHATSAPP_PHONE_NUMBER_ID,
VERIFY_TOKEN: maskToken(requiredEnvVars.WHATSAPP_VERIFY_TOKEN),
},
null,
2
)
);
const sendText = async (to, text) => {
try {
const response = await axios.post(
${WHATSAPP_API_URL}/${requiredEnvVars.WHATSAPP_PHONE_NUMBER_ID}/messages,
{
messaging_product: "whatsapp",
recipient_type: "individual",
to: to,
type: "text",
text: { body: text },
},
{
headers: {
Authorization: Bearer ${requiredEnvVars.WHATSAPP_ACCESS_TOKEN},
"Content-Type": "application/json",
},
}
);
logger.info(`Message sent successfully to ${to}`);
return response.data;} catch (error) {
logger.error(Error sending WhatsApp message: ${error.message});
throw error;
}
};
const getMediaUrl = async (mediaId) => {
try {
const response = await axios.get(${WHATSAPP_API_URL}/${mediaId}, {
headers: {
Authorization: Bearer ${requiredEnvVars.WHATSAPP_ACCESS_TOKEN},
},
});
logger.info(`Media URL retrieved for ID: ${mediaId}`);
return response.data.url;} catch (error) {
logger.error(Error getting media URL: ${error.message});
throw error;
}
};
module.exports = {
sendText,
getMediaUrl,
};
ⓘ Deployment information is only viewable by project members and Railway employees.
1 Replies
2 years ago
Hello,
Few issues -
- You where starting your app during build, I removed the incorrect build command.
- All your variables where double quoted, I removed the double quotes.
- Your target port was set incorrectly, I set it to the correct port.
Looks to be online and ruining now!