通过(libpurple)消息协议发送和接收消息

4 投票
4 回答
3620 浏览
提问于 2025-04-15 15:21

我有个想法,想通过一些常见的聊天工具,比如MSN、ICQ、AIM、Skype等,来发送和接收消息。

我现在只会用PHP和Python,所以希望能找到一个可以在这两种语言中使用的库。我找到了一些,比如phurple(http://sourceforge.net/projects/phurple/)适用于PHP,还有python-purple(http://developer.pidgin.im/wiki/PythonHowTo),但感觉这些都不是很更新。你们有什么建议吗?我的目标是写一个像meebo.com那样的网页应用。

希望能有个教程或者示例代码,还有比较好的文档。pidgin.im上面没有什么实用的教程。

另外,你们也可以告诉我不同的实现方式,这样我可以根据现有的ICQ、AIM、MSN等实现来自己构建一个类。

如果能给我一个如何登录账户并发送一条消息的示例,那就太好了!

来吧,大家帮帮忙吧 :)

4 个回答

1

如果你解压从phurple下载的文件,你会看到一些这样的示例:

<?php
  if(!extension_loaded('phurple')) {
  dl('phurple.' . PHP_SHLIB_SUFFIX);
  }

  class CustomPhurpleClient extends PhurpleClient {
    private $someVar;
    protected function initInternal() {
        $this->someVar = "Hello World";
    }

    protected function writeIM($conversation, $buddy, $message, $flags, $time) {
        if(PhurpleClient::MESSAGE_RECV == $flags) {
            printf( "(%s) %s %s: %s\n",
                        $conversation->getName() ? $conversation->getName() : $buddy->getName(),
                        date("H:i:s", $time),
                        is_object($buddy) ? $buddy->getAlias() : $buddy,
                        $message
                );
        }
    }

    protected function onSignedOn($connection) {
        print $this->justForFun($this->someVar);
    }

    public function justForFun($param) {
        return "just for fun, the param is: $param";
    }
  } 
  // end Class CustomPhurpleClient

  // Example Code Below:
  try {
    $user_dir = "/tmp/phphurple-test";
    if(!file_exists($user_dir) || !is_dir($user_dir)) {
        mkdir($user_dir);
    }

    PhurpleClient::setUserDir($user_dir);
    PhurpleClient::setDebug(true);
    PhurpleClient::setUiId("TestUI");

    $client = CustomPhurpleClient::getInstance();
    $client->addAccount("msn://nick@hotmail.com:password@messenger.hotmail.com:1863");
    $client->connect();

    $client->runLoop();
  } catch (Exception $e) {
    echo "[Phurple]: " . $e->getMessage() . "\n";
    die();
  }
?>
2

一个不错的选择是使用DBus接口:Pidgin(也叫purple)完全支持这个接口,而且Python的DBus接口库也很稳定。

11

下面是如何连接到Pidgin的DBus服务器的方法。

#!/usr/bin/env python
import dbus

bus = dbus.SessionBus()

if "im.pidgin.purple.PurpleService" in bus.list_names():
    purple = bus.get_object("im.pidgin.purple.PurpleService",
            "/im/pidgin/purple/PurpleObject",
            "im.pidgin.purple.PurpleInterface")

    print "Connected to the pidgin DBus."
    for conv in purple.PurpleGetIms():
        purple.PurpleConvImSend(purple.PurpleConvIm(conv), "Ignore this message.")

else:
    print "Could not find piding DBus service, make sure Pidgin is running."

不知道你有没有看到过这个,但这是官方的Python DBus教程:链接

编辑:重新添加了Pidgin开发者维基的链接。里面教的内容和我上面说的一样,只需往下滚动页面就可以了。http://developer.pidgin.im/wiki/PythonHowTo

撰写回答