I'm using the API for the first time. Can you help me figure out why it isn't working?

Apparently the requests are being made because I see on my account that usage is going up, but my bot doesn’t send me the chat completion.
I cannot ‘console.log’ because I’m hosting it on Firebase functions.
Here are my files:

openaicontroller.js :

const openai = require('../config/openaiConfig')

const generateMeta = async() => {
    const chatCompletion = await openai.chat.completions.create({
        model: "gpt-3.5-turbo",
        messages: [{"role": "user", "content": "Tell me something good!"}],
        max_tokens: 30,
      });
    
    return chatCompletion.choices[0].message;
}

module.exports = { generateMeta }

index.js :

// Required Libraries 
const { onRequest } = require("firebase-functions/v2/https");
const logger = require("firebase-functions/logger");
const qrcode = require('qrcode-terminal');
const { Client } = require('whatsapp-web.js');
const { generateMeta } = require('./controllers/openaicontroller');

// Use this to interact with wpp account
const client = new Client();

// Generates QR-Code for authentication
client.on('qr', qr => {
    qrcode.generate(qr, {small: true});
});

// Listens for messages
client.on('message_create', message => {
    contact_id = message.from;
    text_received = message.body.toLowerCase();
    response = generateMeta()
    
	if(text_received === 'start bot') {
        client.sendMessage(contact_id, "bot is working"); // This message works
	}

    if(text_received === 'open ai') {
        client.sendMessage(contact_id, response) // Nothing happens in this case
	}
});

client.initialize();

Forgive my opinionated response, but developing without any kind of logging support should be considered a deal breaker. Something you should quite literally refuse to try to do. Your top priority should be to get logging working somehow, and then we can probably help. Otherwise you could be shooting in the dark for months on every trivial thing.

2 Likes

That is very simple and good advice. I focused on trying to log. At first, I tried the Firebase logger library and it wasn’t working. Then I noticed that I could log things in deployment, but couldn’t do it by calling function after deployment.
So I logged my function in deployments until I found the problem (had to redeploy a few times). Thanks for the insight!

2 Likes