Skip to content

Commit 7b6a912

Browse files
committed
Share chats config rework
Added a new way of configuring share chats. This new way adds support to threads in super groups. Code should be backwards compatible with the old config also, but haven't tested that yet. This is what the new config looks like: ``` "CHATS_SHARE": { "manual_share": { "all": [ {"id": -1001123321, "thread": 123, "title": "Chat title"}, {"id": -1001321123} ], "11": [ {"id": -1001567765, "thread": 321, "title": "Title of chat"} ] }, "after_attendance": [ {"id":-10019999} ], "webhook": { "all": [ {"id": -1001321123} ], "by_pokemon": [ {"pokemon_id":150, "chats":[{"id": -1001567765, "thread": 321}]} ], "geofences":{ "0": { // Geofence id "all": [ {"id": -1001567765} ], "1": [ // Raid level {"id": -1001567765} ] } } } }, ``` The chat object inside each section requires `id`. `thread` and `title` are optional. Title was added since Telegram API doesn't allow bots to get the thread title for existing threads.
1 parent 4a338d9 commit 7b6a912

32 files changed

+216
-128
lines changed

commands/delete.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
if($query->rowCount() == 0) {
3131
$msg = '<b>' . getTranslation('no_active_raids_found') . '</b>';
32-
send_message($update['message']['chat']['id'], $msg);
32+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg);
3333
exit;
3434
}
3535

@@ -66,4 +66,4 @@
6666
$callback_response = 'OK';
6767

6868
// Send message.
69-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);
69+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);

commands/events.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@
2525
$keys[][] = button(getTranslation('done'), ['exit', 'd' => '1']);
2626

2727
// Send message.
28-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);
28+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);

commands/exreport.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@
8585
$msg = '<b>EX Raid Report</b>';
8686

8787
// Send message.
88-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);
88+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);

commands/friendsearch.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323
}else {
2424
$msg = $searchterm.CR. getTranslation('trainer_not_found');
2525
}
26-
send_message($update['message']['chat']['id'], $msg, [], ['reply_markup' => ['selective' => true]]);
26+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, [], ['reply_markup' => ['selective' => true]]);

commands/get.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
// Any configs allowed?
4343
if(empty($allowed)) {
44-
send_message($update['message']['chat']['id'], getTranslation('not_supported'));
44+
send_message(create_chat_object([$update['message']['chat']['id']]), getTranslation('not_supported'));
4545
exit;
4646
}
4747
foreach($json as $cfg_name => $cfg_value) {
@@ -69,4 +69,4 @@
6969
$msg .= CR;
7070
}
7171
}
72-
send_message($update['message']['chat']['id'], $msg);
72+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg);

commands/gym.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
$msg.= $keys_and_gymarea['gymareaTitle'];
2121

2222
// Send message.
23-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true], 'disable_web_page_preview' => 'true']);
23+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true], 'disable_web_page_preview' => 'true']);

commands/gymname.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@
5050
}
5151

5252
// Send message.
53-
send_message($update['message']['chat']['id'], $msg, [], ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true], 'disable_web_page_preview' => 'true']);
53+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, [], ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true], 'disable_web_page_preview' => 'true']);

commands/help.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@
6767
}
6868

6969
// Send message.
70-
send_message($update['message']['from']['id'], $msg);
70+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg);

commands/history.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
$keys = $msg_keys[1];
2121
}
2222

23-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true], 'disable_web_page_preview' => 'true']);
23+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true], 'disable_web_page_preview' => 'true']);

commands/list.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
// Did we get any raids?
4646
if(count($raids) == 0) {
4747
$msg = '<b>' . getTranslation('no_active_raids_found') . '</b>';
48-
send_message($update['message']['chat']['id'], $msg, [], ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);
48+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, [], ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);
4949
exit();
5050
}
5151

@@ -98,4 +98,4 @@
9898
$msg .= '<b>' . getTranslation('select_gym_name') . '</b>' . CR;
9999

100100
// Send message.
101-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);
101+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);

commands/listall.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
$msg.= $keys_and_gymarea['gymareaTitle'];
2121

2222
// Send message.
23-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true], 'disable_web_page_preview' => 'true']);
23+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true], 'disable_web_page_preview' => 'true']);

commands/overview.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717
$msg = '<b>' . getTranslation('raids_share_overview') . ':</b>';
1818

1919
// Send message.
20-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);
20+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);

commands/pokedex.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@
5050
$keys[][] = button(getTranslation('abort'), 'exit');
5151

5252
// Send message.
53-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);
53+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);

commands/pokemon.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
if($query->rowCount() == 0) {
3131
$msg = '<b>' . getTranslation('no_active_raids_found') . '</b>';
32-
send_message($update['message']['chat']['id'], $msg);
32+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg);
3333
exit;
3434
}
3535

@@ -72,4 +72,4 @@
7272
$msg .= '<b>' . getTranslation('select_gym_name') . '</b>' . CR;
7373

7474
// Send message.
75-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);
75+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);

commands/raid_from_webhook.php

+67-27
Original file line numberDiff line numberDiff line change
@@ -313,51 +313,92 @@ function isPointInsidePolygon($point, $vertices) {
313313

314314
$chats_geofence = $chats_raidlevel = $webhook_chats = $chats_by_pokemon = [];
315315
if($send_updates == true) {
316+
// Update raid polls and send alerts of updates
316317
require_once(LOGIC_PATH .'/update_raid_poll.php');
317318
$tg_json = update_raid_poll($raid_id, $raid, false, $tg_json, true);
318319
if($wasBossUpdated) $tg_json = alarm($raid, false, 'new_boss', '', $tg_json);
319-
if(!empty($config->WEBHOOK_CHATS_BY_POKEMON[0]) && !$no_auto_posting) {
320-
foreach($config->WEBHOOK_CHATS_BY_POKEMON as $rule) {
321-
if(isset($rule['pokemon_id']) && $rule['pokemon_id'] == $pokemon && (!isset($rule['form_id']) or (isset($rule['form_id']) && $rule['form_id'] == $form))) {
322-
foreach($rule['chats'] as $rule_chat) {
323-
// If the raid isn't already posted to the chats specified in WEBHOOK_CHATS_BY_POKEMON, we add it to the array
324-
if(!isset($cleanup_data[$raid_id]) or !in_array($rule_chat, $cleanup_data[$raid_id])) {
325-
$chats_by_pokemon[] = $rule_chat;
320+
// Post hatched Pokemon to their respective chats if configured
321+
// Start share_chats backwards compatibility
322+
if(!$config->CHATS_SHARE) {
323+
if(!empty($config->WEBHOOK_CHATS_BY_POKEMON[0]) && !$no_auto_posting) {
324+
foreach($config->WEBHOOK_CHATS_BY_POKEMON as $rule) {
325+
if(isset($rule['pokemon_id']) && $rule['pokemon_id'] == $pokemon && (!isset($rule['form_id']) or (isset($rule['form_id']) && $rule['form_id'] == $form))) {
326+
foreach($rule['chats'] as $rule_chat) {
327+
// If the raid isn't already posted to the chats specified in WEBHOOK_CHATS_BY_POKEMON, we add it to the array
328+
if(!isset($cleanup_data[$raid_id]) or !in_array($rule_chat, $cleanup_data[$raid_id])) {
329+
$chats_by_pokemon[] = $rule_chat;
330+
}
331+
}
332+
}
333+
}
334+
}
335+
// End chats_share backwards compatibility
336+
}else {
337+
if(!empty($config->CHATS_SHARE['webhook']['by_pokemon']) && !$no_auto_posting) {
338+
foreach($config->CHATS_SHARE['webhook']['by_pokemon'] as $rule) {
339+
if(isset($rule['pokemon_id']) && $rule['pokemon_id'] == $pokemon && (!isset($rule['form_id']) or (isset($rule['form_id']) && $rule['FORM_ID'] == $form))) {
340+
foreach($rule['chats'] as $rule_chat) {
341+
if(!isset($cleanup_data[$raid_id]) or !in_array($rule_chat['id'], $cleanup_data[$raid_id])) {
342+
$chats_by_pokemon[] = $rule_chat;
343+
}
326344
}
327345
}
328346
}
329347
}
330348
}
349+
331350
if(empty($chats_by_pokemon)) continue;
332351
}else {
333-
// Get chats to share to by raid level and geofence id
334-
if($geofences != false) {
335-
foreach($inside_geofences as $geofence_id) {
336-
$const_geofence = 'WEBHOOK_CHATS_LEVEL_' . $level . '_' . $geofence_id;
337-
$const_geofence_chats = $config->{$const_geofence} ?? [];
338-
339-
if(!empty($const_geofence_chats)) {
340-
$chats_geofence = explode(',', $const_geofence_chats);
352+
// Start share_chats backwards compatibility
353+
if(!$config->CHATS_SHARE) {
354+
// Get chats to share to by raid level and geofence id
355+
if($geofences != false) {
356+
foreach($inside_geofences as $geofence_id) {
357+
$const_geofence = 'WEBHOOK_CHATS_LEVEL_' . $level . '_' . $geofence_id;
358+
$const_geofence_chats = $config->{$const_geofence} ?? [];
359+
360+
if(!empty($const_geofence_chats)) {
361+
$chats_geofence = explode(',', $const_geofence_chats);
362+
}
341363
}
342364
}
343-
}
344365

345-
// Get chats to share to by raid level
346-
$const = 'WEBHOOK_CHATS_LEVEL_' . $level;
347-
$const_chats = $config->{$const} ?? [];
366+
// Get chats to share to by raid level
367+
$const = 'WEBHOOK_CHATS_LEVEL_' . $level;
368+
$const_chats = $config->{$const} ?? [];
348369

349-
if(!empty($const_chats)) {
350-
$chats_raidlevel = explode(',', $const_chats);
351-
}
370+
if(!empty($const_chats)) {
371+
$chats_raidlevel = explode(',', $const_chats);
372+
}
352373

353-
// Get chats
354-
if(!empty($config->WEBHOOK_CHATS_ALL_LEVELS)) {
355-
$webhook_chats = explode(',', $config->WEBHOOK_CHATS_ALL_LEVELS);
374+
// Get chats
375+
if(!empty($config->WEBHOOK_CHATS_ALL_LEVELS)) {
376+
$webhook_chats = explode(',', $config->WEBHOOK_CHATS_ALL_LEVELS);
377+
}
378+
// End chats_share backwards compatibility
379+
}else {
380+
if($geofences != false) {
381+
foreach($inside_geofences as $geofence_id) {
382+
$geofence_chats_all = $config->CHATS_SHARE['webhook']['geofences'][$geofence_id]['all'] ?? [];
383+
$geofence_chats_by_level = $config->CHATS_SHARE['webhook']['geofences'][$geofence_id][$level] ?? [];
384+
385+
if(!empty($geofence_chats_all)) {
386+
$chats_geofence = array_merge($chats_geofence, $geofence_chats_all);
387+
}
388+
if(!empty($geofence_chats_by_level)) {
389+
$chats_geofence = array_merge($chats_geofence, $geofence_chats_by_level);
390+
}
391+
}
392+
}
393+
// Get chats to share to by raid level
394+
$chats_raidlevel = $config->CHATS_SHARE['webhook'][$level] ?? [];
395+
396+
// Get chats
397+
$webhook_chats = $config->CHATS_SHARE['webhook']['all'] ?? [];
356398
}
357399
}
358400

359401
$chats = array_merge($chats_geofence, $chats_raidlevel, $webhook_chats, $chats_by_pokemon);
360-
361402
require_once(LOGIC_PATH .'/send_raid_poll.php');
362403
if($metrics) {
363404
$webhook_raids_posted_total->inc();
@@ -366,6 +407,5 @@ function isPointInsidePolygon($point, $vertices) {
366407
$tg_json = send_raid_poll($raid_id, $chats, $raid, $tg_json);
367408
}
368409
}
369-
370410
// Telegram multicurl request.
371411
curl_json_multi_request($tg_json);

commands/set.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,4 @@
177177
}
178178

179179
// Send message.
180-
send_message($update['message']['chat']['id'], $msg);
180+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg);

commands/start.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
require('list.php');
1717
}else {
1818
$response_msg = '<b>' . getTranslation('bot_access_denied') . '</b>';
19-
send_message($update['message']['from']['id'], $response_msg);
19+
send_message(create_chat_object([$update['message']['chat']['id']]), $response_msg);
2020
}
2121
exit;
2222
}
@@ -60,4 +60,4 @@
6060
}
6161

6262
// Send message.
63-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);
63+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);

commands/trainer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@
6767
$keys[] = $nav_keys;
6868

6969
// Send message.
70-
send_message($update['message']['chat']['id'], $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);
70+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg, $keys, ['reply_markup' => ['selective' => true, 'one_time_keyboard' => true]]);

core/bot/commands.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
}
1414

1515
if(isset($update['message']['chat']['id']) && new_user($update['message']['chat']['id']) && $com != 'start' && $com != 'tutorial') {
16-
send_message($update['message']['chat']['id'], getTranslation("tutorial_command_failed"));
16+
send_message(create_chat_object([$update['message']['chat']['id']]), getTranslation("tutorial_command_failed"));
1717
exit();
1818
}
1919

@@ -33,6 +33,6 @@
3333
// Include start file and exit.
3434
include_once($startcommand);
3535
} else {
36-
send_message($update['message']['chat']['id'], '<b>' . getTranslation('not_supported') . '</b>');
36+
send_message(create_chat_object([$update['message']['chat']['id']]), '<b>' . getTranslation('not_supported') . '</b>');
3737
}
3838
}

core/bot/importal.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Invalid input or unknown bot - send message and end.
66
$msg = '<b>' . getTranslation('invalid_input') . '</b>';
77
$msg .= CR . CR . getTranslation('not_supported') . SP . getTranslation('or') . SP . getTranslation('internal_error');
8-
send_message($update['message']['from']['id'], $msg);
8+
send_message(create_chat_object([$update['message']['chat']['id']]), $msg);
99
exit();
1010
}
1111

0 commit comments

Comments
 (0)