Skip to content

Commit dbaebd0

Browse files
committedDec 30, 2021
added examples & do cleaner
1 parent f0bc940 commit dbaebd0

13 files changed

+126
-50
lines changed
 

‎.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
config/env.php
1+
/config
22
/vendor

‎app/Controllers/CallbackQueryHandler.php

+11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,18 @@
55
class CallbackQueryHandler {
66

77
public function run(){
8+
89
//code
10+
11+
if(callback_query()->getData()=='sayHello'){
12+
13+
bot()->answerCallbackQuery(
14+
[
15+
'callback_query_id'=>callback_query()->getCallbackQueryId(),
16+
'text'=>'Hello ☺️'
17+
]
18+
);
19+
}
920
}
1021

1122
}

‎app/Controllers/MessageHandler.php

+44-5
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,54 @@
22

33
namespace App\Controllers;
44

5-
use App\Helpers\ThirdPartyClass;
6-
use App\Modules\ExampleModule;
5+
use Src\InlineKeyboardMarkup;
6+
use Src\ReplyKeyboardMarkup;
77

88
class MessageHandler{
9+
10+
//main method
911
public function run(){
12+
1013
//do somethings
11-
ExampleModule::run()->example();
14+
if(message()->getText()=='/start'){
15+
16+
//send simple message by options
17+
bot()->sendMessage(
18+
[
19+
'text'=>"welcome to TeleBot Hello World example 😎\nTeleBot is an open source framework for creating any telegram bots\n<a href='https://github.com/arashabedii/telebot'>see more</a>",
20+
'reply_to_message_id'=>message()->getMessageId(),
21+
'parse_mode'=>'html',
22+
'disable_web_page_preview'=>true,
23+
//KEYBOARDS PATERN LIKE: "$btn1--$btn2--$btn3//$btn4--$btn6//$btn7--$btn8::request_location"
24+
'reply_markup'=>ReplyKeyboardMarkup::createEasyier("Inline--Shere Contact::request_contact//share location::request_location"),
25+
26+
]
27+
);
28+
}elseif(message()->getText()=='Inline'){
29+
30+
//create inline keyboard rows structure
31+
$rows=[
32+
33+
//row 1
34+
[
35+
['text'=>'google','url'=>'https://google.com'],
36+
['text'=>'Say Hello','callback_data'=>'sayHello'],
37+
],
38+
39+
//row2
40+
[
41+
['text'=>'test inine query(need to turn on inline mode)','switch_inline_query'=>'telebot_tester'],
42+
],
43+
44+
];
1245

13-
//use Thired party class
14-
ThirdPartyClass::run()->sayHello();
46+
//send request to telegram
47+
bot()->sendMessage(
48+
[
49+
'text'=>'This is inline keyboard test',
50+
'reply_markup'=>InlineKeyboardMarkup::create($rows),
51+
]
52+
);
53+
}
1554
}
1655
}

‎bootstrap/bot.php

+33-13
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,73 @@
11
<?php
2+
23
//BOOTSTRAP
34
require_once "../vendor/autoload.php";
45

56
use Src\Bot;
67

8+
//bot starter
79
$bot=new Bot();
8-
//bot()->sendMessage(['text'=>json_encode(update()->getUpdate())]);
9-
//file_put_contents('debug.txt',json_encode(update()->getUpdate()));
10-
//die();
1110

1211

12+
//-------------------------------------------------------------------UPDATE HANDLERS----------------------------------------------------------------
13+
14+
//handling message update
1315
if(update()->getType()=='message'){
1416

1517
messageHandler()->run();
1618

17-
}elseif(update()->getType()=='callback_query'){
19+
}
20+
//handling callback_query update
21+
elseif(update()->getType()=='callback_query'){
1822

1923
callbackQueryHandler()->run();
2024

21-
}elseif(update()->getType()=='channel_post'){
25+
}
26+
//handling channel_post update
27+
elseif(update()->getType()=='channel_post'){
2228

2329
channelPostHandler()->run();
2430

25-
}elseif(update()->getType()=='edited_message'){
31+
}
32+
//edited_message update
33+
elseif(update()->getType()=='edited_message'){
2634

2735
editedMessageHandler()->run();
2836

29-
}elseif(update()->getType()=='edited_channel_post'){
37+
}
38+
//handling edited_channel_post update
39+
elseif(update()->getType()=='edited_channel_post'){
3040

3141
editedChannelPostHandler()->run();
3242

33-
}elseif(update()->getType()=='inline_query'){
43+
}
44+
//handling inline_query update
45+
elseif(update()->getType()=='inline_query'){
3446

3547
inlineQueryHandler()->run();
3648

37-
}elseif(update()->getType()=='poll'){
49+
}
50+
//handling poll update
51+
elseif(update()->getType()=='poll'){
3852

3953
pollHandler()->run();
4054

41-
}elseif(update()->getType()=='poll_answer'){
55+
}
56+
//handling poll_answer update
57+
elseif(update()->getType()=='poll_answer'){
4258

4359
pollAnswerHandler()->run();
4460

45-
}elseif(update()->getType()=='my_chat_member'){
61+
}
62+
//handling my_chat_member update
63+
elseif(update()->getType()=='my_chat_member'){
4664

4765
myChatMemberHandler()->run();
4866

49-
}elseif(update()->getType()=='chat_member'){
67+
}
68+
//handling chat_member update
69+
elseif(update()->getType()=='chat_member'){
5070

5171
chatMemberHandler()->run();
52-
72+
5373
}

‎config/env.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
$domain='https://191e-51-161-66-74.ngrok.io';
2+
$domain='https://806b-5-39-2-198.ngrok.io';
33

44

55

‎config/env_example.php

-15
This file was deleted.

‎requestsHandler.php

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
use Src\Bot;
66

7+
//handle requests
78
Bot::redirectRequests();

‎src/Bot.php

+14-9
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Src;
44

5-
class Bot {
5+
//CORE BOT CLASS
66

7-
//HELPERS
7+
class Bot {
88

9+
//chat_id generator
910
public function paramsGenerator($params){
11+
1012
if(empty($params['chat_id'])){
1113
$params['chat_id']=chat()->getChatId();
1214
}
@@ -77,7 +79,6 @@ public function pinChatMessage(array $params){
7779

7880

7981
public function unpinChatMessage(array $params){
80-
8182
return $this->sendRequest('unpinChatMessage',$this->paramsGenerator($params));
8283
}
8384

@@ -255,40 +256,44 @@ public function answerInlineQuery($inlineQueryId,array $results,array $options=[
255256

256257

257258

259+
//--------------------------------------------REQUESTERS-------------------------------------------------------
260+
261+
//redirect all input requests from telegram servers
258262
static function redirectRequests($url=""){
259-
$params=file_get_contents("php://input");
260263

264+
$params=file_get_contents("php://input");
261265
if(empty($url)){
262266
$url=config()['bot_main_path'];
263267
}
264-
265268
return Server::sendRequestWithoutResponse($url,$params);
266-
267269
}
268270

271+
//get telegram update types
269272
public function getUpdateType(){
270273
return update()->getType();
271274
}
272275

276+
//set webhook helper
273277
public static function setWebhook(){
274278
$token=config()['token'];
275279
$url=config()['request_handler_path'];
276280
$reqUrl="https://api.telegram.org/bot$token/setWebhook?url=$url";
277-
var_dump(Server::sendRequest($reqUrl));
281+
return Server::sendRequest($reqUrl);
278282
}
279283

284+
//delete webhook helper
280285
public static function deleteWebhook(){
281286
$token=config()['token'];
282287
$url=config()['request_handler_path'];
283288
$reqUrl="https://api.telegram.org/bot$token/deleteWebhook?url=$url";
284-
var_dump(Server::sendRequest($reqUrl));
289+
return Server::sendRequest($reqUrl);
285290
}
286291

292+
//create telegram request
287293
public static function sendRequest($method,array $params){
288294
$token=config()['token'];
289295
$reqUrl="https://api.telegram.org/bot$token/$method";
290296
return Server::sendRequest($reqUrl,$params,'post');
291297
}
292298

293-
294299
}

‎src/Helpers/functions.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,39 @@
2020
use Src\UpdateTypes\InlineQuery;
2121
use Src\UpdateTypes\Poll;
2222

23+
//return configure files
2324
function config($fileName="env"){
24-
$conf=include($_SERVER['DOCUMENT_ROOT']."/config/$fileName.php");
25+
if(!empty($_SERVER['DOCUMENT_ROOT'])){
26+
$conf=include($_SERVER['DOCUMENT_ROOT']."/config/$fileName.php");
27+
}else{
28+
$conf=include($_SERVER['PWD']."/config/$fileName.php");
29+
}
30+
2531
return $conf;
2632
}
2733

28-
29-
34+
//bot object
3035
function bot(){
3136
global $bot;
3237
return $bot;
3338
}
3439

40+
//messageHandler object
3541
function messageHandler(){
3642
return new MessageHandler();
3743
}
3844

45+
//channelPostHandler object
3946
function channelPostHandler(){
4047
return new ChannelPostHandler();
4148
}
4249

50+
//callbackQueryHandler object
4351
function callbackQueryHandler(){
4452
return new CallbackQueryHandler();
4553
}
4654

55+
//callbackQueryHandler object
4756
function inlineQueryHandler(){
4857
return new InlineQueryHandler();
4958
}

‎src/UpdateTypes/Chat.php

+3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
namespace Src\UpdateTypes;
33

44
class Chat extends Update {
5+
56
protected $chat_id;
67

78
public function __construct()
89
{
10+
parent::__construct();
11+
912
$this->setChatId();
1013
}
1114

‎src/UpdateTypes/InlineQuery.php

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Src\UpdateTypes;
44

55
class InlineQuery extends Update {
6+
67
public function getId(){
78
return $this->inline_query->id;
89
}

‎src/UpdateTypes/Update.php

-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ public function getType(){
116116
}
117117

118118
//GET UPDATE
119-
120119
public function getUpdate(){
121120
return $this->update;
122121
}

‎webhookHandler.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
<?php
2+
23
require_once './vendor/autoload.php';
34
use Src\Bot;
45

6+
//webhook manul setting
7+
58
if(!empty($_GET['set'])){
6-
Bot::setWebhook();
9+
echo Bot::setWebhook();
710
}elseif(!empty($_GET['delete'])){
8-
Bot::deleteWebhook();
11+
echo Bot::deleteWebhook();
912
}

0 commit comments

Comments
 (0)
Please sign in to comment.