内容摘要 连接几乎所有的 PHP 应用程序到 IRC 服务器并使信息随处可用
获得天气状况
要获得天气状况,将使用 Weather Web 服务 PEAR 模块。清单 3 展示了该模块,我对该模块进行了修改以提供我所在地区的天气状况。
清单 3. 报告天气状况的 weather 聊天机器人(bot)
<?php
include_once('Net/SmartIRC.php');
include_once('Services/Weather.php');
$weather = new Services_Weather();
$wdc = $weather->service( "Weatherdotcom" );
class weatherbot
{
function weather(&$irc, &$data)
{
global $wdc;
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel,
'Yeah, weather');
$fc = $wdc->getForecast( 94587, 1 );
foreach( $fc['days'] as $day )
{
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel,
"Condition: ".$day['day'][ 'condition' ] );
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel,
"High: ".$day[ 'temperatureHigh' ] );
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel,
"Low: ".$day[ 'temperatureLow' ] );
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel,
"Wind: ".$day['day'][ 'wind' ] );
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel,
"Wind Degrees: ".$day['day'][ 'windDegrees' ] );
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel,
"Wind Direction: ".$day['day'][ 'windDirection' ] );
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel,
"Precipitation: ".$day['day'][ 'precipitation' ] );
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel,
"Humidity: ".$day['day'][ 'humidity' ] );
}
}
}
$host = "localhost";
$port = 6667;
$nick = "weather";
$chan = "#weather";
$bot = &new weatherbot( );
$irc = &new Net_SmartIRC( );
$irc->setUseSockets( TRUE );
$irc->registerActionhandler( SMARTIRC_TYPE_CHANNEL,
'^weather', $bot, 'weather' );
$irc->connect( $host, $port );
$irc->login( $nick, 'Weather bot', 0, $nick );
$irc->join( array( $chan ) );
$irc->listen( );
$irc->disconnect( );
?>
|
我所做的第一个更新是添加 include 指令以引用天气服务模块。然后创建了一个天气服务对象。在 weather 方法中,调用 getForecast 方法并请求给定邮政编码地区的天气预报 —— 在本例中,邮政编码是 94587,即 Union City, Calif 的邮政编码。
从那里,我得到第一天的天气信息,并通过 IRC 信道将天气结果以消息的形式发送出去。图 4 展示了产生的文本窗口。
图 4. 带有硬编码邮政编码的天气响应
但是我想您并不想要得到我 所在地区的天气信息。那么,如何得到您所在城市的天气信息呢?
得到您需要的天气信息
要得到与您相关的天气信息,需要解析请求的文本。这就意味着要对 weather 方法做一些小小的修改,如清单 4 所示。
清单 4. 新的 weather 方法
class weatherbot
{
function weather(&$irc, &$data)
{
global $wdc;
$zip = $data->messageex[1];
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel,
"Yeah, weather for $zip");
$fc = $wdc->getForecast( $zip, 1 );
foreach( $fc['days'] as $day )
{
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel,
"Condition: ".$day['day'][ 'condition' ] );
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel,
"High: ".$day[ 'temperatureHigh' ] );
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel,
"Low: ".$day[ 'temperatureLow' ] );
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel,
"Wind: ".$day['day'][ 'wind' ] );
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel,
"Wind Degrees: ".$day['day'][ 'windDegrees' ] );
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel,
"Wind Direction: ".$day['day'][ 'windDirection' ] );
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel,
"Precipitation: ".$day['day'][ 'precipitation' ] );
$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel,
"Humidity: ".$day['day'][ 'humidity' ] );
}
}
}
|
IRC 模块巧妙地将用户输入的消息断开为单词,可以在 messageex 数组中以数组的方式引用这些单词。在本例中,第一个条目是邮政编码。
图 5 展示了如何使用具有邮政编码的天气请求。正如所看到的,我输入 19081,这是 Swarthmore, Pa.,Swarthmore College 所在地的邮政编码。
图 5. Swarthmore, Pa. 的天气情况
当然,我应该进行更多的错误检查,例如确保邮政编码是有效的。但是我将这些留给您自己来完成。
结束语
本文涵盖了连接 PHP 应用程序到 IRC 的基本内容。您可以使用该应用程序作为起点,并且添加自己的 PHP 类到代码中。然后您就可以通过 IRC 利用浏览器使用您的 Web 应用程序了。
我是让用户使用他们喜欢的交流方式的坚定的拥护者 —— 发送电子邮件消息或者是通过即时消息客户机。本文所提供的仅仅是在 PHP 应用程序中放入另一种类型的接口的一个例子。我鼓励在带有 PHP 代码的标准的 Web 范围之外进行思考。
另外,我鼓励您将自己的模块提供给 PEAR。最好是提供干净压缩的代码,这将有益于大家。
参考资料
学习
- 您可以参阅本文在 developerWorks 全球站点上的 英文原文。
- 访问 IRC.org 站点,查找有关 IRC 聊天系统的更多信息。
- 还有 Bot-Depot,在这里您能够与其他编写 聊天机器人(bot)的程序员进行交谈。
- 访问 developerWorks 开放源码专区,获得丰富的 how-to 信息、工具和项目更新,以帮助您用开放源码技术进行开发,并与 IBM 产品结合使用。
获得产品和技术
讨论
关于作者
 |
|
|
 |
Jack D. Herrington 是一名高级软件工程师,具有 20 多年的从业经验。他是以下 3 本书的作者:Code Generation in Action、Podcasting Hacks 以及即将出版的 PHP Hacks。他还撰写了 30 余篇文章。 |