开发者 | Milmor |
---|---|
更新时间 | 2024年7月1日 22:10 |
捐献地址: | 去捐款 |
PHP版本: | 5.6 及以上 |
WordPress版本: | 6.5 |
版权: | GPLv2 or later |
版权网址: | 版权信息 |
telegram-bot
directory to the /wp-content/plugins/
directoryMore documentation is available on www.botpress.org/docs www.botpress.org/docs/telegram/how-to-create-a-bot
If you are a developer, or just want a more complete "Telegram > Log" enable WP_DEBUG mode. The plugin debug mode also allows to explore Telegram users and groups as standard posts. This let you to check custom fields for each users and modify them in real time. You'll notice a new column (= Telegram id for the user) in Subscribers and Groups page. We don't suggest to keep WP_DEBUG if not for testing purposes.
The best way to integrate PHP code is to build a custom integration plugin, but you can also add PHP to /$commands directly in your WordPress admin dashboard using the Insert Php plugin.
In case you want to scale and choose the first option, you can create a new file called telegram-bot-custom.php and upload it to wp-content/plugins.
The following example, once activated in the plugins list, will reply to /command
:
`<?php
/
Plugin Name: Telegram Bot & Channel (Custom)
Description: My Custom Telegram Plugin
Author: My name
Version: 1
/
add_action('telegram_parse','telegramcustom_parse', 10, 2);
function telegramcustom_parse( $telegram_user_id, $text ) {
$plugin_post_id = telegram_getid( $telegram_user_id );
if ( !$plugin_post_id ) {
return;
}
/
Here is the dynamic processing and how to reply.
You can:
- use if, switch and everything that works in php
- check if $text is made of multiple words (create an array from $text)
- customize and code other actions (ex. create WordPress post is $telegram_user_id is your id)
/
if ( $text == '/command') {
telegram_sendmessage( $telegram_user_id, 'Oh, yes you typed /command');
}
return;
}
?>`
You can send custom keyboards directly in php. Every keyboard can be set only when you send a message, and is kept in the client side until another keyboard is sent (in another message). You can also change this behaviour by setting the $one_time_keyboard true or false.
telegram_sendmessage( $telegram_user_id, 'Hello from the other side!'); //Message with no keyboard (or with default one if set in plugin options) telegram_sendmessage( $telegram_user_id, 'Hello from the other side!', telegram_build_reply_markup( '11,12,13;21,22', true )); //Message with custom keyboard
Here is the details of telegram_build_reply_markup (an array is returned):
telegram_build_reply_markup( '11,12,13;21,22', //The keyboard template (eg. 2 row, 3 columns for the first one and two columns for the second one true, // $one_time_keyboard (optional) (default false = kept until a new keyboard is sent) (true = kept until the user send something to the bot) true // $resize_keyboard (optional) (default true) );
You can also alter keyboards for commands defined in the admin area. Start from the previous custom plugin created, and add the following filter:
`add_filter( 'telegram_get_reply_markup_filter', 'telegram_get_reply_markup_filter_custom', 1 );
function telegram_get_reply_markup_filter_custom( $id ) {
if ( $id ) {
switch ( $id ) {
case 7: //Your command ID (found in the url while editing the command)
telegram_log('####', $id, 'custom keyboard'); //Useful for debug
return array(
'keyboard' => array(array('Top Left', 'Top Right'),array('Bottom')),
'resize_keyboard' => true, //true or false (suggested: true)
'one_time_keyboard' => true //true or false
);\
default:
return;
}
}
}`
It's easy, with harvesine algorithm (one-point radius) or standard geolocation (4-points). These snippets only cover the harvesine algorithm, that is simple and supported by the plugin. To use the standard 4-points geolocation it's enough to do some php-calc with basic if-then structures. You can start from the previous custom plugin created, and add the following action: `add_action('telegram_parse_location','telegramcustom_c_parse_location', 10, 3); function telegramcustom_c_parse_location ( $telegram_user_id, $lat, $long ) { if ( telegram_location_haversine_check ( 45.85, 9.70, $lat, $long, 20 ) ) { telegram_sendmessage( $telegram_user_id, 'Inside the radius'); } }` The examples sends a "Inside the radius" message when the user is inside the 20-meters radius centered in 45.85 Lat, 9.70 Long. You have two developer functions to use: `//Check if point is within a distance (max_distance required) $boolean = telegram_location_haversine_check ( $latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $max_distance, $min_distance = 0, $earthRadius = 6371000); //Calculate the distance $int = telegram_location_haversine_distance ( $latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000);` The first function returns a boolean (true/false) depending on given parameters. Please note that $min_distance and $earthRadius are optional. The second one returns a int (in meters) of the distance. $earthRadius optional. Both the functions calculates distances on meters. If you want another type of result, just change the $earthRadius.
We've written simple functions to let developers build everything. Photos are saved in /wp-content/uploads/telegram-bot/'.$plugin_post_id.'/'.$file_name where $plugin_post_id is the custom post type id associated with the Telegram subscription (ex. '24') and $file_name is time().$extension `<?php / Plugin Name: Telegram Bot & Channel (Custom) Description: My Custom Telegram Plugin Author: My name Version: 1 / add_action('telegram_parse_photo','telegramcustom_parse_photo', 10, 2); function telegramcustom_parse_photo ( $telegram_user_id, $photo ) { $plugin_post_id = telegram_getid( $telegram_user_id ); if ( !$plugin_post_id ) { return; } /* Here is the dynamic processing and how to reply. You can: