服务器每天都有这样那样的信息需要实时推送。最初一直是Mail,最容易实现,成本也低。后来有了短信接口,就有了相对实时但要花钱的方式推送。
知道GAE可以直接跟gtalk挂钩推送消息,可能一直挂Gtalk的人远没有挂QQ和MSN的多(密之声:Gtalk又没有星星和钻石,挂也没劲),但有了Android + 3G的实时在线,这种方式却可以直接替代SMS这类花钱的服务,何乐不为?
大致上完成了原型,制作了一个基于Python的命令行程序,不敢独享,分享给大家。希望大家能够一起加入这个项目完善它。
在此之前,请现将 server-say @@@ appspot.com (你懂的!)加为好友
#!/usr/bin/python
# -*- coding:utf-8 -*-
import sys
import re
import urllib
class ServerSaid:
ApiURL = 'http://server-say.appspot.com/api'
#
#请到http://server-said.appspot.com/注册您的Ip地址。没有注册的Ip每小时最多10条消息。
#
Account = ''
MessageBody = ''
DefaultEncode = 'utf-8'
def SendMessage(self):
cleanMessage = self.MessageBody.encode(self.DefaultEncode)
if (self.checkEmail(self.Account) and self.checkMessage(cleanMessage)):
cleanMessage = self.MessageBody.encode('utf-8')
url = self.ApiURL + '?account='+ urllib.quote(self.Account) +'&message=' + urllib.quote(cleanMessage)
# print url
query = urllib.urlopen(url)
if ( query.read() == '200' ):
print 'Message be sent!n'
else:
print self.Error(0)
else :
print self.Error(1)
def Error(self, code=0):
return 'Message can't be send! n'
def checkEmail(self, Email):
RegexString = r'^w+([-+.]w+)*@w+([-.]w+)*.w{2,5}$'
return re.match(RegexString, Email)
def checkMessage(self, Message):
RegexString = r'^.{1,140}$'
return re.match(RegexString, Message)
if __name__ == '__main__':
if (len(sys.argv)>1):
handler = ServerSaid()
handler.Account = sys.argv[1]
Message = sys.argv[2]
handler.MessageBody = Message.decode(handler.DefaultEncode)
handler.SendMessage()
else:
print 'serversaid.py YOUR_GTALK MESSAGE '
这里为了防止消息的滥发,使用前最好先到 http://server-say.appspot.com/ 注册一下。没有注册的IP每小时仅能发送10条消息,我想大部分情况下也够用了。注册了的用户可以无限制的发送消息了,除非我在GAE注册的那张信用卡里的钱花光——我会哭的。
啥也不说了,PHP接口也搞定了!
< ?php
/*
* Gtalk 通告机器人 Ver 0.1
*
* Litrin Jiang 2010/12/02
*
* --------------------------------
* 1. 2010/12: v0.1 原型实现
*
*/
Class Gtalk{
private $APIUrl = 'Http://server-say.appspot.com/api?';
//Api 的地址,不要修改
public $mothod = 'get';
//默认的提交方式,现阶段仅支持get
public $account = '';
//接收人的Gtalk账户
public $messageBody = '';
//消息主体,最多支持140个字符
public $Charset = 'UTF-8';
public function Send($account=null, $messageBody=null){
if (is_array($account)){
foreach ($account as $key) {
$this->Send($key);
}
}
if ($account === null){
$account = $this.account;
}
$this->CheckAccount($account);
if($messageBody === null){
$messageBody = $this->messageBody;
}
if ($this->Charset != 'UTF-8'){
$messageBody = iconv("UTF-8", $this->Charset.'//IGNORE', $messageBody);
}
$this->CheckMessage($messageBody);
$url = $this->APIUrl .'account=' . urlencode($account) . '&message=' . urlencode($messageBody);
$query = file($url);
/*
if($query == '200'){
return true;
}else{
return false;
}*/
}
private function CheckAccount($account){
$regexString = '^w+([-+.]w+)*@w+([-.]w+)*.w{2,5}$';
echo ereg($regexString, $account);
if (ereg($regexString, $account) != true){
#throw new Exception("Value not a Email account! ");
}
}
private function CheckMessage($messageBody){
$regexString = '^.{1,140}';
if (ereg($regexString, $messageBody) != true){
#throw new Exception("Message not a allowed format! ");
}
}
}
?>
转载请注明:爱开源 » Gtalk通知机器人脚本