收到的 IM-MSG 信号消息
我想制作一个程序,在不同的协议上登录几个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
strcmp
函数:==
检查指针是否拥有相同的地址,这不是您想要的。You need to use the
strcmp
function:==
checks that the pointers hold the same address, which is not what you want.