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.

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

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法