Skip to content
Newer
Older
100644 67 lines (56 sloc) 2.39 KB
ddcb061 @robertbasic a simple example of a chat bot
authored
1 <?php
2
3 $dbus = new Dbus(Dbus::BUS_SESSION);
4
5 $interface = "im.pidgin.purple.PurpleInterface";
6 $method = "ReceivedImMsg";
7 $dbus->addWatch($interface, $method);
8
9 $proxy = $dbus->createProxy("im.pidgin.purple.PurpleService",
10 "/im/pidgin/purple/PurpleObject",
11 "im.pidgin.purple.PurpleInterface");
12
13 $kill = false;
14 do {
15 $signal = $dbus->waitLoop(1000);
16
17 if ($signal instanceof DbusSignal) {
18 if ($signal->matches($interface, $method)) {
19 /**
20 * Contents of the recieved data, see:
21 * http://developer.pidgin.im/doxygen/dev/html/pages.html
22 * http://developer.pidgin.im/doxygen/dev/html/conversation-signals.html
23 * data:
24 * 0 => the reciever account
25 * 1 => the user sending the message
26 * 2 => the message
27 * 3 => the IM conversation
28 * 4 => flags
29 */
30 $data = $signal->getData()->getData();
31
ca18155 @robertbasic typo fix
authored
32 $receiver = $data[0];
ddcb061 @robertbasic a simple example of a chat bot
authored
33 $sender = $data[1];
34 $message = $data[2];
35 $conversation = $data[3];
36
37 $responseMessage = "I'm sorry Dave. I'm affraid I can't do that.";
38
39 // 2034 is *my* account's number, yours will be different
40 // 3681 is the account number from which *I* accept messages
ca18155 @robertbasic typo fix
authored
41 if ($receiver == 2034 && $proxy->PurpleAccountIsConnected($receiver)
42 && $proxy->PurpleFindBuddy($receiver, $sender) == 3681) {
ddcb061 @robertbasic a simple example of a chat bot
authored
43
44 switch ($message) {
45 case 'hi': {
46 $responseMessage = 'Hi Dave!';
47 } break;
48 case 'uptime': {
49 exec('uptime', $responseMessage);
50 $responseMessage = $responseMessage[0];
51 } break;
52 case 'current ip': {
53 $responseMessage = file_get_contents('http://ifconfig.me/ip');
54 } break;
55 case 'kill': {
56 $responseMessage = 'Bye Dave!';
57 $kill = true;
58 }
59 }
60
61 $im = $proxy->PurpleConvIm($conversation);
62 $proxy->PurpleConvImSend($im, $responseMessage);
63 }
64 }
65 }
66
67 } while (!$kill);
Something went wrong with that request. Please try again.