doudi8519 2017-06-22 12:43 采纳率: 100%
浏览 112
已采纳

用于定期发送消息的Telegram bot

I have a Telegram bot that will send new posts from certain pages on Twitter and Facebook.

I did it this way:

function checkTwitterPost($post, $author) {
    $tw_last_post = strtotime($post['created_at']);
    $twitter_last_update_path = './timestamps/'.$author.'_twitter_last_update.txt';
    if (file_exists($twitter_last_update_path)) {
        $tw_last_update = (int)file_get_contents($twitter_last_update_path);
    } else {
        $tw_last_update = 0;
    }
    file_put_contents($twitter_last_update_path, $tw_last_post);
    if ($tw_last_post > $tw_last_update) {
        unset($tw_last_update);
        return true;
    }
    unset($tw_last_update);
    return false;
}

function checkFacebookPost($post, $author) {
    $fb_last_post = strtotime($post->created_time);
    $facebook_last_update_path = './timestamps/'.$author.'_facebook_last_update.txt';
    if (file_exists($facebook_last_update_path)) {
        $fb_last_update = (int)file_get_contents($facebook_last_update_path);
    } else {
        $fb_last_update = 0;
    }
    file_put_contents($facebook_last_update_path, $fb_last_post);
    if ($fb_last_post > $fb_last_update) {
        unset($fb_last_update);
        return true;   
    }
    unset($fb_last_update);
    return false;
}



$bot->command('start', function ($message) use ($bot) {
    @unlink('./timestamps/user1_facebooke_last_update.txt');
    @unlink('./timestamps/user1_twitter_last_update.txt');
    @unlink('./timestamps/user2_facebooke_last_update.txt');
    @unlink('./timestamps/user2_twitter_last_update.txt');


    $users = array(
        'user1'     => ['facebook'=>'user1', 'twitter'=>'user1'],
        'user2'     => ['facebook'=>'user2', 'twitter'=>'user2'],
    );
    while (true) {

        $account = $users['user1']['facebook']; 
        $post = FacebookUtil::getLastPost($account);
        if (checkFacebookPost($post, $account)) {
            $bot->sendMessage($message->getChat()->getId(), $post->message);
        }
        unset($post);

        $account = $users[user1]['twitter'];
        $post = TwitterUtil::getLastPost($account);
        if (checkTwitterPost($post, $account)) {
            $bot->sendMessage($message->getChat()->getId(), $post->message);
        }
        unset($post);

        $account = $users['user2']['facebook'];
        $post = FacebookUtil::getLastPost($account);
        if (checkFacebookPost($post, $account)) {
            $bot->sendMessage($message->getChat()->getId(), $post->message);
        }
        unset($post);

        $account = $users['user2']['twitter'];
        $post = TwitterUtil::getLastPost($account);
        if (checkTwitterPost($post, $account)) {
            $bot->sendMessage($message->getChat()->getId(), $post['text']);
        }
        unset($post);

        sleep(1500);
    }
});

It's works, but causes memory problems. After some time, the server falls down.

Are there any more suitable solutions?

Or I can somehow optimize this approach?

  • 写回答

1条回答 默认 最新

  • dongqiuqiu4736 2017-06-27 14:43
    关注

    Problem solved with using Cron and storing of chats id in the file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化