AI Search
Classic Search
 Search Phrase:
 Search Type:
Advanced search options
 Search in Forums:
 Search in date period:

 Sort Search Results by:

Filter by custom fields

Topic prefix

AI Assistant
Notifications
Clear all

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

4 Posts
2 Users
0 Reactions
2,024 Views
Posts: 6
Topic starter
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
(@gocchimama)
Active Member
Joined: 3 years ago
[#50036]

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));
}
wpForo Version
2.1.9
WordPress Version
6.2.2

3 Replies
Robert
Posts: 10716
Admin
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
(@robert)
Support Team
Joined: 2 months 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
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian
(@gocchimama)
Active Member
Joined: 3 years 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: 2 months ago

Support Team
Posts: 10716
Translate
English
Spanish
French
German
Italian
Portuguese
Russian
Chinese
Japanese
Korean
Arabic
Hindi
Dutch
Polish
Turkish
Vietnamese
Thai
Swedish
Danish
Finnish
Norwegian
Czech
Hungarian
Romanian
Greek
Hebrew
Indonesian
Malay
Ukrainian
Bulgarian
Croatian
Slovak
Slovenian
Serbian
Lithuanian
Latvian
Estonian

@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.


Share: