开发者

received-im-msg signal message

开发者 https://www.devze.com 2023-02-01 18:57 出处:网络
I want to make a program that logs in few IDs on different protocols, receives the messages and gives answers to different messages (commands).

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消