Skip to content

Commit

Permalink
Merge pull request #29 from tanhongit/bot-tools
Browse files Browse the repository at this point in the history
Handle setting for the action of events
  • Loading branch information
tanhongit authored Aug 11, 2023
2 parents 8ed028b + d77dc75 commit 29ead91
Show file tree
Hide file tree
Showing 14 changed files with 304 additions and 130 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
- PHP ^8.0
- Composer
- Telegram Bot
- SSL Certificate

## Installation

Expand Down Expand Up @@ -70,12 +69,14 @@ APP_URL=https://123456789.ngrok.io

### Set the webhook

#### Set the webhook from the source code
We have two ways to set the webhook:

#### 1. Set the webhook from this project

After setting up your domain and SSL certificate, you need to set up the webhook for your bot. Go to:

```text
<APP_URL>/setWebhook.php
<APP_URL>/webhook/set.php
```

> **Note:** Replace `<APP_URL>` with your app URL in .env file.
Expand All @@ -86,7 +87,7 @@ If you see the following message, it means that the webhook has been sent succes
{"ok":true,"result":true,"description":"Webhook was set"}
```

#### Set the webhook manually
#### 2. Set the webhook manually

If you want to set the webhook manually, you can use the following URL:

Expand Down Expand Up @@ -140,4 +141,4 @@ Now you can send a message to your bot, and you will receive a notification.

Here is the first notification you will receive: ♻️ **Connection Successful**

> **You can add multiple webhooks to your repository.**
> **Note: You can add multiple webhooks to your repository. Please similarly set up the webhook for each repository.**
8 changes: 8 additions & 0 deletions resources/tools/custom_event_actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
/**
* @var string $event
*/

?>
Setting actions for the event: <b><?= $event ?></b>
Please select an action of this event to enable or disable notifications:
4 changes: 4 additions & 0 deletions resources/tools/custom_events.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Go to check the <a href="https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads">GitHub documentation</a> for more information about events.
---
<b>Click and configure child events if the option has theicon.</b>
And select an event to enable or disable notifications:
4 changes: 0 additions & 4 deletions resources/tools/event.php

This file was deleted.

2 changes: 1 addition & 1 deletion resources/tools/menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/id - To get your Chat ID.
/token - To get this bot token.
/usage - How to use me.
/settings - Settings github notify.
/settings - Settings GitHub notify.
/menu - To get this menu.

Select a button:
2 changes: 1 addition & 1 deletion resources/tools/settings.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<b>Settings for github notify.</b>
<b>Settings for GitHub notify.</b>
2 changes: 1 addition & 1 deletion resources/tools/start.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
Hey <b><?= $first_name ?></b>,

I can send you notifications from your GitHub Repository instantly to your Telegram.
Use /menu for more information about me.
Use /menu for more options.
33 changes: 32 additions & 1 deletion src/Models/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Event
{
public const EVENT_FILE = __DIR__ . '/../../storage/tg-event.json';

public const EVENT_PREFIX = Setting::SETTING_PREFIX . '.event.';
public const EVENT_PREFIX = Setting::SETTING_CUSTOM_EVENTS . '.evt.';

public array $eventConfig = [];

Expand Down Expand Up @@ -37,4 +37,35 @@ public function getEventConfig(): array
{
return $this->eventConfig;
}

/**
* Update event config by event and action
*
* @param string $event
* @param string|null $action
* @return void
*/
public function updateEvent(string $event, string|null $action): void
{
if (!empty($action)) {
$this->eventConfig[$event][$action] = !$this->eventConfig[$event][$action];
} else {
$this->eventConfig[$event] = !$this->eventConfig[$event];
}

$this->saveEventConfig();
}

/**
* Save event config
*
* @return void
*/
private function saveEventConfig(): void
{
if (file_exists(self::EVENT_FILE)) {
$json = json_encode($this->eventConfig, JSON_PRETTY_PRINT);
file_put_contents(self::EVENT_FILE, $json, LOCK_EX);
}
}
}
52 changes: 50 additions & 2 deletions src/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
class Setting
{
public const SETTING_FILE = __DIR__ . '/../../storage/tg-setting.json';
public const SETTING_PREFIX = 'setting.';
public const SETTING_PREFIX = 'stg.';

public const SETTING_IS_NOTIFIED = self::SETTING_PREFIX . 'is_notified';
public const SETTING_ALL_EVENTS_NOTIFY = self::SETTING_PREFIX . 'all_events_notify';
public const SETTING_CUSTOM_EVENTS = self::SETTING_PREFIX . 'custom_events';
public const SETTING_CUSTOM_EVENTS = self::SETTING_PREFIX . 'cus';
public const SETTING_BACK = self::SETTING_PREFIX . 'back.';

public array $settings = [];

Expand Down Expand Up @@ -64,4 +65,51 @@ public function isNotified(): bool

return false;
}

/**
* Update setting item value and save to file
*
* @param string $settingName
* @param $settingValue
* @return bool
*/
public function updateSettingItem(string $settingName, $settingValue = null): bool
{
$keys = explode('.', $settingName);
$lastKey = array_pop($keys);
$nestedSettings = &$this->settings;

foreach ($keys as $key) {
if (!isset($nestedSettings[$key]) || !is_array($nestedSettings[$key])) {
return false;
}
$nestedSettings = &$nestedSettings[$key];
}

if (isset($nestedSettings[$lastKey])) {
$nestedSettings[$lastKey] = $settingValue ?? !$nestedSettings[$lastKey];
if ($this->saveSettingsToFile()) {
return true;
}
}

return false;
}

/**
* Save settings to json file
*
* @return bool
*/
private function saveSettingsToFile(): bool
{
if (file_exists(self::SETTING_FILE)) {
$json = json_encode($this->settings, JSON_PRETTY_PRINT);
file_put_contents(self::SETTING_FILE, $json, LOCK_EX);

return true;
}

return false;
}
}
56 changes: 33 additions & 23 deletions src/Services/AppService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,16 @@ public function sendMessage(string $message = '', array $options = [], string $s
'parse_mode' => 'HTML'
);

try {
if ($sendType === 'Message') {
$content['text'] = $message;
} elseif ($sendType === 'Photo' && !empty($options)) {
$content['photo'] = $options['photo'];
$content['caption'] = $message;
}

if (!empty($options) && isset($options['reply_markup'])) {
$content['reply_markup'] = $this->telegram->buildInlineKeyBoard($options['reply_markup']);
}

$this->telegram->{'send' . $sendType}($content);
} catch (Exception $e) {
error_log($e->getMessage());
if ($sendType === 'Message') {
$content['text'] = $message;
} elseif ($sendType === 'Photo') {
$content['photo'] = $options['photo'] ?? null;
$content['caption'] = $message;
}

$content['reply_markup'] = $options['reply_markup'] ? $this->telegram->buildInlineKeyBoard($options['reply_markup']) : null;

$this->telegram->{'send' . $sendType}($content);
}

/**
Expand Down Expand Up @@ -81,10 +75,9 @@ public function answerCallbackQuery(string $text = null): void
public function editMessageText(?string $text = null, array $options = []): void
{
try {
$content = [
$content = array_merge([
'text' => $text ?? $this->Callback_Message_Text()
];
$content = array_merge($content, $this->setContentEditMessage($options));
], $this->setCallbackContentMessage($options));

$this->telegram->editMessageText($content);
} catch (Exception $e) {
Expand All @@ -102,7 +95,7 @@ public function editMessageText(?string $text = null, array $options = []): void
public function editMessageReplyMarkup(array $options = []): void
{
try {
$this->telegram->editMessageReplyMarkup($this->setContentEditMessage($options));
$this->telegram->editMessageReplyMarkup($this->setCallbackContentMessage($options));
} catch (Exception $e) {
error_log($e->getMessage());
}
Expand All @@ -119,10 +112,12 @@ public function Callback_Message_Text(): string
}

/**
* Create content for a callback message
*
* @param array $options
* @return array
*/
public function setContentEditMessage(array $options = []): array
public function setCallbackContentMessage(array $options = []): array
{
$content = array(
'chat_id' => $this->telegram->Callback_ChatID(),
Expand All @@ -131,10 +126,25 @@ public function setContentEditMessage(array $options = []): array
'parse_mode' => 'HTML',
);

if (!empty($options) && isset($options['reply_markup'])) {
$content['reply_markup'] = $this->telegram->buildInlineKeyBoard($options['reply_markup']);
}
$content['reply_markup'] = $options['reply_markup'] ? $this->telegram->buildInlineKeyBoard($options['reply_markup']) : null;

return $content;
}

/**
* Generate menu markup
*
* @return array[]
*/
public function menuMarkup(): array
{
return [
[
$this->telegram->buildInlineKeyBoardButton("📰 About", "", "about", ""),
$this->telegram->buildInlineKeyBoardButton("📞 Contact", config('author.contact'))
], [
$this->telegram->buildInlineKeyBoardButton("💠 Source Code", config('author.source_code'))
]
];
}
}
Loading

0 comments on commit 29ead91

Please sign in to comment.