Notifications
Clear all

Script [Closed] I want to post the reply content to Chatwork

4 Posts
2 Users
0 Reactions
614 Views
Posts: 6
Topic starter
(@gocchimama)
Active Member
Joined: 1 year ago

I want to post the reply content to Chatwork, but I can't get the new content with this code.

New topics can be sent to Chatwork.

I would like your advice.

function reply_message( $topic_or_post_id, $args ) {

    if(!get_option('post_cw_api_token') || !get_option('post_cw_roomid')) return;

    // Get post information from wpForo
    if(current_filter() === 'wpforo_after_add_topic'){
        $post = WPF()->topic->get_topic($topic_or_post_id);
    }else{
        $post = WPF()->post->get_post($topic_or_post_id);
    }

    $author_id = $post['userid']; // adjust this if 'userid' is not the correct key
    $author_name = get_the_author_meta('display_name', $author_id);

    $topic = wpforo_topic($post['topicid']);
    $topic_url = $topic['url'];

    $expert_num = get_option('post_send_cwr_expert');
    $send_title = esc_html( $post['title'] );

    $send_content = isset($post['topicid']) ? wp_strip_all_tags(wpforo_topic($post['topicid'], 'body')) : '';
    $send_content = mb_substr($send_content, 0, $expert_num);

    if (mb_strlen($send_content) >= $expert_num) {
        $send_content = mb_substr($send_content, 0, $expert_num - 3) . '...';
    }


    $body = '[info][title]' . get_option('post_send_cwr_messege') . '[/title]投稿者:' . $author_name . "\n[hr]\n"  . $send_title ."\n". $send_content . "\n" . $topic_url . '[/info]';

    $roomid = get_option('post_cw_roomid');
    $key = get_option('post_cw_api_token');
    $url = 'https://api.chatwork.com/v2/rooms/'.$roomid.'/messages';
    $data = array(
        'body' => $body
    );
    $headers = array(
        'Content-Type: application/x-www-form-urlencoded',
        'X-ChatWorkToken: '.$key
    );
    $options = array('http' => array(
        'method' => 'POST',
        'content' => http_build_query($data),
        'header' => implode("\r\n", $headers),
    ));
    $contents = file_get_contents($url, false, stream_context_create($options));
}
3 Replies
Robert
Posts: 10589
Admin
(@robert)
Support Team
Joined: 9 years ago

Hi @gocchimama,

I think you should connect your function to an action hook not to a filter hook. The best hook to intercept new topic and post data is the wpforo_after_add_post hook. It sends three arguments:

do_action( 'wpforo_after_add_post', $post, $topic, $forum );

So you can get the $post['body'] as the topic (first post) or reply content.

Posts: 6
Topic starter
(@gocchimama)
Active Member
Joined: 1 year ago

Thanks!

I use this hook,

add_action( 'wpforo_after_add_post', 'reply_message', 10, 2 );

But,

wpdb::prepare was called incorrectly.The query expected only one placeholder, but sent an array of multiple placeholders.

I get an error message.

1 Reply
Robert
Admin
(@robert)
Joined: 9 years ago

Support Team
Posts: 10589

@gocchimama,

The error comes from your code. In the wpForo function there is nothing after that hook, so there cannot be any error from wpForo side. Just check your code and correct it.