如何解决电报机器人中的重复链接问题?

时间:2019-09-30 03:25:41

标签: php cron telegram

我创建了一个电报机器人来为我的用户显示远程URL更新的链接,但是当远程URL更新时,该机器人向我的用户发送2-3次相同的URL。不知道我在哪里遇到问题,所以我在这里添加了curl代码,还有一件事我使用cron作业来运行bot。请问我的代码有问题吗?

class TGGroconBot {
    private $curl;
    private $tgUrl;
    private $db;
    private $chats = array();
    private $lastupdateid = 0;
    function __construct($token) {
        $this->curl = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, 3);
        curl_setopt($this->curl, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($this->curl, CURLOPT_VERBOSE, true);
        $this->tgUrl = 'https://api.telegram.org/bot'.urlencode($token).'/';
        $this->db = new PDO('sqlite:groconbot.sqlite');
        $this->db->exec("CREATE TABLE if not exists chats (id INTEGER PRIMARY KEY, username varchar(200))");
        if(!$resChats = $this->db->query('select * from chats')) {
            echo "error get chat list from db\n";exit;
        }
        foreach($resChats as $chat) {
            $this->chats[$chat['id']] = $chat['username'];
        }
        print_r($this->chats);
    }
    private function postData($url, $data, $contenttype) {
        curl_setopt($this->curl, CURLOPT_URL, $url);
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Content-type: '.$contenttype));
        return curl_exec($this->curl);
    }
    public function sendTextMessage($recipient, $text) {
        echo "\n======== sendTextMessage\n";
        $endpointUrl = $this->tgUrl.'sendMessage';
        $data = new stdClass;
        $data->chat_id = $recipient;
        $data->text = $text;
        $result = $this->postData($endpointUrl, json_encode($data), 'application/json');
        $result = json_decode($result);
        return $result;
    }
    private function getUpdates() {
        echo "\n======== getUpdates\n";
        $endpointUrl = $this->tgUrl.'getUpdates';
        $data = new stdClass;
        $data->offset = $this->lastupdateid + 1;
        $result = $this->postData($endpointUrl, json_encode($data), 'application/json');
        $result = json_decode($result);
        return $result;
    }
    public function update() {
        echo "\n======== update\n";
        $updates = $this->getUpdates();
        foreach($updates->result as $x) {
            $this->lastupdateid = $x->update_id;
            if($x->channel_post) $event = $x->channel_post;
            elseif($x->message) $event = $x->message;
            else continue;
            $this->chats[$event->chat->id] = $event->chat->first_name;
            $st = $this->db->prepare('insert into chats(id, username) values(?, ?)');
            $st->execute(array($event->chat->id, $event->chat->first_name));
            // /prout command
            if(strpos($event->text, '/prout') === 0) {
                $this->sendTextMessage($event->chat->id, 'prout...');
            }
        }
    }
    public function broadcast($text) {
        $this->update();
        foreach($this->chats as $chatid=>$user) {
            $this->sendTextMessage($chatid, $text);
            usleep(33000);
        }
    }
}

0 个答案:

没有答案