Question

I want to make a program that logs in few IDs on different protocols, receives the messages and gives answers to different messages (commands).

example:

me: who
bot: I'm a libpurple powered bot.

The code looks like this:

static void received_im_msg(PurpleAccount *account, char *sender, char *message,
                              PurpleConversation *conv, PurpleMessageFlags flags)
{
    if (conv==NULL) {
        conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, sender);
    }

    printf("%s: %s\n", sender, message);

    char *answer;

    if (message == "who") {
        answer="I'm a libpurple powered bot.";
    } else if (message=="hello") {
        answer="Hello, my firend!";
    } else {
        answer="Unknown command.";
    }
    //print the answer, so we can see it in terminal:
    printf("bot: %s\n",message);  

    //send the message:
    purple_conv_im_send(purple_conversation_get_im_data(conv),answer);
}

For me, this code looks just OK, but doesn't work as expected. Any message the bot receives, the answer will be always Unknown command.. I can't understand why the

message == "who"

is not true, even if

printf("%s: %s\n", sender, message);

prints something like:
example_id_345: who.

Do you have any idea of why this thing happens? What I did wrong?

Thank you and sorry for my bad english.

Was it helpful?

Solution

You need to use the strcmp function:

if (strcmp(message, "who") == 0) {
    answer="I'm a libpurple powered bot.";
} else if (strcmp(message, "hello") == 0) {

== checks that the pointers hold the same address, which is not what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top