收到的 IM-MSG 信号消息

发布于 2024-10-09 08:38:03 字数 1177 浏览 3 评论 0原文

我想制作一个程序,在不同的协议上登录几个ID,接收消息并对不同的消息(命令)给出答案。

例如:

我:
机器人:我是一个由 libpurple 驱动的机器人。

代码如下所示:

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);
}

对我来说,这段代码看起来还不错,但无法按预期工作。机器人收到的任何消息,答案将始终是未知命令。。我不明白为什么这

message == "who"

不是真的,即使

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

打印出类似的内容:
example_id_345:谁

您知道为什么会发生这种情况吗?我做错了什么?

谢谢你,很抱歉我的英语不好。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吖咩 2024-10-16 08:38:03

您需要使用 strcmp 函数:

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

== 检查指针是否拥有相同的地址,这不是您想要的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文