Skip to content

Commit 51ffe81

Browse files
committed
create
0 parents  commit 51ffe81

File tree

6 files changed

+374
-0
lines changed

6 files changed

+374
-0
lines changed

.gitignore

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### macOS template
3+
# General
4+
.DS_Store
5+
.AppleDouble
6+
.LSOverride
7+
8+
# Icon must end with two \r
9+
Icon
10+
11+
# Thumbnails
12+
._*
13+
14+
# Files that might appear in the root of a volume
15+
.DocumentRevisions-V100
16+
.fseventsd
17+
.Spotlight-V100
18+
.TemporaryItems
19+
.Trashes
20+
.VolumeIcon.icns
21+
.com.apple.timemachine.donotpresent
22+
23+
# Directories potentially created on remote AFP share
24+
.AppleDB
25+
.AppleDesktop
26+
Network Trash Folder
27+
Temporary Items
28+
.apdisk
29+
30+
### SublimeText template
31+
# Cache files for Sublime Text
32+
*.tmlanguage.cache
33+
*.tmPreferences.cache
34+
*.stTheme.cache
35+
36+
# Workspace files are user-specific
37+
*.sublime-workspace
38+
39+
# Project files should be checked into the repository, unless a significant
40+
# proportion of contributors will probably not be using Sublime Text
41+
# *.sublime-project
42+
43+
# SFTP configuration file
44+
sftp-config.json
45+
46+
# Package control specific files
47+
Package Control.last-run
48+
Package Control.ca-list
49+
Package Control.ca-bundle
50+
Package Control.system-ca-bundle
51+
Package Control.cache/
52+
Package Control.ca-certs/
53+
Package Control.merged-ca-bundle
54+
Package Control.user-ca-bundle
55+
oscrypto-ca-bundle.crt
56+
bh_unicode_properties.cache
57+
58+
# Sublime-github package stores a github token in this file
59+
# https://packagecontrol.io/packages/sublime-github
60+
GitHub.sublime-settings
61+
62+
### Example user template template
63+
### Example user template
64+
65+
# IntelliJ project files
66+
.idea
67+
*.iml
68+
out
69+
gen
70+
### JetBrains template
71+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
72+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
73+
74+
# User-specific stuff
75+
.idea/**/workspace.xml
76+
.idea/**/tasks.xml
77+
.idea/**/usage.statistics.xml
78+
.idea/**/dictionaries
79+
.idea/**/shelf
80+
81+
# Generated files
82+
.idea/**/contentModel.xml
83+
84+
# Sensitive or high-churn files
85+
.idea/**/dataSources/
86+
.idea/**/dataSources.ids
87+
.idea/**/dataSources.local.xml
88+
.idea/**/sqlDataSources.xml
89+
.idea/**/dynamic.xml
90+
.idea/**/uiDesigner.xml
91+
.idea/**/dbnavigator.xml
92+
93+
# Gradle
94+
.idea/**/gradle.xml
95+
.idea/**/libraries
96+
97+
# Gradle and Maven with auto-import
98+
# When using Gradle or Maven with auto-import, you should exclude module files,
99+
# since they will be recreated, and may cause churn. Uncomment if using
100+
# auto-import.
101+
# .idea/modules.xml
102+
# .idea/*.iml
103+
# .idea/modules
104+
# *.iml
105+
# *.ipr
106+
107+
# CMake
108+
cmake-build-*/
109+
110+
# Mongo Explorer plugin
111+
.idea/**/mongoSettings.xml
112+
113+
# File-based project format
114+
*.iws
115+
116+
# IntelliJ
117+
out/
118+
119+
# mpeltonen/sbt-idea plugin
120+
.idea_modules/
121+
122+
# JIRA plugin
123+
atlassian-ide-plugin.xml
124+
125+
# Cursive Clojure plugin
126+
.idea/replstate.xml
127+
128+
# Crashlytics plugin (for Android Studio and IntelliJ)
129+
com_crashlytics_export_strings.xml
130+
crashlytics.properties
131+
crashlytics-build.properties
132+
fabric.properties
133+
134+
# Editor-based Rest Client
135+
.idea/httpRequests
136+
137+
# Android studio 3.1+ serialized cache file
138+
.idea/caches/build_file_checksums.ser
139+
140+
### Composer template
141+
composer.phar
142+
/vendor/
143+
144+
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
145+
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
146+
# composer.lock
147+

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Ví dụ ứng dụng Telegram Bot trong triển khai monitor service

composer.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "tungvandev/scripts-telegram-bot-send-message",
3+
"type": "library",
4+
"description": "Script Telegram Bot Send Message",
5+
"keywords": [
6+
"telegram",
7+
"bot",
8+
"send message",
9+
"example"
10+
],
11+
"homepage": "https://github.com/tungvandev/scripts-telegram-bot-send-message",
12+
"license": "GPL-3.0",
13+
"authors": [
14+
{
15+
"name": "Nguyen An Hung",
16+
"email": "dev@nguyenanhung.com",
17+
"homepage": "https://nguyenanhung.com",
18+
"role": "Developer"
19+
}
20+
],
21+
"require": {
22+
"ext-curl": "*",
23+
"ext-json": "*",
24+
"ext-openssl": "*",
25+
"monolog/monolog": "^1.24",
26+
"php-curl-class/php-curl-class": "^8.6"
27+
},
28+
"require-dev": {
29+
"kint-php/kint": "^3.0"
30+
},
31+
"autoload": {
32+
"psr-4": {
33+
"tungvandev\\Example\\TelegramBOT\\": "src/"
34+
}
35+
}
36+
}

config.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
return [
3+
'bot_name' => 'xxx',
4+
'bot_token' => 'xxx:xxx'
5+
];

src/Telegram.php

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
3+
namespace tungvandev\Example\TelegramBOT;
4+
5+
use Monolog\Logger;
6+
use Monolog\Handler\StreamHandler;
7+
use Curl\Curl;
8+
9+
/**
10+
* Class Telegram
11+
*
12+
* @package tungvandev\Example\TelegramBOT
13+
*/
14+
class Telegram
15+
{
16+
17+
private $request; // Alias cURL
18+
private $logger; // Logger
19+
private $config; // Mảng cấu hình thông tin BOT
20+
21+
private $chat_id; // ID chat room
22+
private $message; // Nội dung tin nhắn cần gửi đi
23+
24+
/**
25+
* Telegram constructor.
26+
*
27+
* @throws \ErrorException
28+
* @throws \Exception
29+
*/
30+
public function __construct()
31+
{
32+
$this->request = new Curl();
33+
// create a log channel
34+
$this->logger = new Logger('telegram');
35+
$this->logger->pushHandler(new StreamHandler(__DIR__ . '/../logs', Logger::INFO));
36+
}
37+
38+
/**
39+
* @param array $config
40+
*
41+
* @return $this
42+
*/
43+
public function setConfig($config = [])
44+
{
45+
$this->config = $config;
46+
47+
return $this;
48+
}
49+
50+
/**
51+
* @return mixed
52+
*/
53+
public function getConfig()
54+
{
55+
return $this->config;
56+
}
57+
58+
/**
59+
* Hàm gán chat_id cho cuộc trò chuyện
60+
*
61+
* @param string $chat_id
62+
*
63+
* @return $this
64+
*/
65+
public function setChatId($chat_id = '')
66+
{
67+
$this->chat_id = $chat_id;
68+
69+
return $this;
70+
}
71+
72+
/**
73+
* @return mixed
74+
*/
75+
public function getChatId()
76+
{
77+
return $this->chat_id;
78+
}
79+
80+
/**
81+
* Hàm gán nội dung cho cuộc trò chuyện
82+
*
83+
* @param string $message
84+
*
85+
* @return $this
86+
*/
87+
public function setMessage($message = '')
88+
{
89+
$this->message = $message;
90+
91+
return $this;
92+
}
93+
94+
/**
95+
* @return mixed
96+
*/
97+
public function getMessage()
98+
{
99+
return $this->message;
100+
}
101+
102+
/**
103+
* Hàm gửi Message qua Telegram
104+
*
105+
* @return bool
106+
*/
107+
public function sendMessage()
108+
{
109+
// Bắt lỗi
110+
// Ko tồn tại chat_id || message || không xác định được request
111+
if (empty($this->chat_id) || empty($this->message) || empty($this->config) || !isset($this->config['bot_token'])) {
112+
return FALSE;
113+
}
114+
// Xác đinh Endpoint gửi tin đi
115+
$endpoint = 'https://api.telegram.org/bot' . $this->config['bot_token'] . '/sendMessage';
116+
117+
// Xác định Request gửi tin đi
118+
$params = [
119+
'chat_id' => $this->chat_id,
120+
'text' => $this->message
121+
];
122+
123+
// Request tới Telegram
124+
$handle = $this->request->get($endpoint, $params);
125+
if ($handle->error) {
126+
return FALSE;
127+
}
128+
129+
// Nếu không có lỗi gì, request response sẽ trả về 1 object
130+
// Example: stdClass (2) (
131+
// public 'ok' -> boolean true
132+
// public 'result' -> stdClass (5) (
133+
// public 'message_id' -> integer 10
134+
// public 'from' -> stdClass (4) (
135+
// public 'id' -> integer 892069632
136+
// public 'is_bot' -> boolean true
137+
// public 'first_name' -> UTF-8 string (13) "Test Bot Chơi"
138+
// public 'username' -> string (14) "ong_a_test_bot"
139+
// )
140+
// public 'chat' -> stdClass (5) (
141+
// public 'id' -> integer 474860058
142+
// public 'first_name' -> string (4) "Hung"
143+
// public 'last_name' -> string (6) "Nguyen"
144+
// public 'username' -> string (12) "nguyenanhung"
145+
// public 'type' -> string (7) "private"
146+
// )
147+
// public 'date' -> integer 1566063717
148+
// public 'text' -> UTF-8 string (29) "Test con bot chơi cái nhờ :))"
149+
// )
150+
//)
151+
152+
if (empty($handle)) {
153+
// Không xác định được response, lỗi chứ còn gì nữa
154+
return FALSE;
155+
}
156+
if (isset($handle->ok) && $handle->ok == TRUE) {
157+
// Send Message thành công, body trường ok == true
158+
return TRUE;
159+
}
160+
161+
return FALSE;
162+
163+
164+
}
165+
}

test.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
require_once __DIR__ . '/vendor/autoload.php';
3+
4+
use tungvandev\Example\TelegramBOT\Telegram;
5+
6+
// Nạp config
7+
$config = require_once __DIR__ . '/config.php';
8+
9+
// Khởi tạo class telegram
10+
$telegram = new Telegram();
11+
12+
$telegram->setConfig($config)
13+
->setChatId(474860058)
14+
->setMessage('Test con bot chơi cái nhờ :))');
15+
16+
// Send Message
17+
18+
$result = $telegram->sendMessage();
19+
20+
d($result);

0 commit comments

Comments
 (0)