Skip to content

Commit 16feb51

Browse files
committed
Initial commit
1 parent 73d12c2 commit 16feb51

File tree

8 files changed

+585
-24
lines changed

8 files changed

+585
-24
lines changed

README.md

Lines changed: 129 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,129 @@
1-
# Telegram Api
1+
# PHP Telegram Bot
2+
======================
3+
4+
A Telegram Bot based on the official [Telegram Bot API](https://core.telegram.org/bots/api)
5+
6+
7+
## introduction
8+
This is a pure php Telegram Bot, fully extensible via plugins. Telegram recently announced official support for a [Bot API](https://telegram.org/blog/bot-revolution) allowing integrators of all sorts to bring automated interactions to the mobile platform. This Bot aims to provide a platform where one could simply write a plugin and have interactions in a matter of minutes.
9+
10+
11+
Instructions
12+
============
13+
14+
1. Message @botfather https://telegram.me/botfather with the following text: `/newbot`
15+
If you don't know how to message by username, click the search field on your Telegram app and type `@botfather`, you should be able to initiate a conversation. Be careful not to send it to the wrong contact, because some users has similar usernames to `botfather`.
16+
17+
![botfather initial conversation](http://imgur.com/aI26ixR)
18+
19+
2. @botfather replies with `Alright, a new bot. How are we going to call it? Please choose a name for your bot.`
20+
21+
3. Type whatever name you want for your bot.
22+
23+
4. @botfather replies with `Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot.`
24+
25+
5. Type whatever username you want for your bot, minimum 5 characters, and must end with `bot`. For example: `telesample_bot`
26+
27+
6. @botfather replies with:
28+
29+
Done! Congratulations on your new bot. You will find it at telegram.me/telesample_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands.
30+
31+
Use this token to access the HTTP API:
32+
<b>123456789:AAG90e14-0f8-40183D-18491dDE</b>
33+
34+
For a description of the Bot API, see this page: https://core.telegram.org/bots/api
35+
36+
7. Note down the 'token' mentioned above.
37+
38+
8. Type `/setprivacy` to @botfather.
39+
40+
![botfather later conversation](http://imgur.com/tWDVvh4)
41+
42+
9. @botfather replies with `Choose a bot to change group messages settings.`
43+
44+
10. Type `@telesample_bot` (change to the username you set at step 5 above, but start it with `@`)
45+
46+
11. @botfather replies with
47+
48+
'Enable' - your bot will only receive messages that either start with the '/' symbol or mention the bot by username.
49+
'Disable' - your bot will receive all messages that people send to groups.
50+
Current status is: ENABLED
51+
52+
12. Type `Disable` to let your bot receive all messages sent to a group. This step is up to you actually.
53+
54+
13. @botfather replies with `Success! The new status is: DISABLED. /help`
55+
56+
57+
58+
59+
## Installation
60+
You need server with https and composer support.
61+
62+
Create composer.json filde:
63+
```js
64+
{
65+
"name": "yourproject/yourproject",
66+
"type": "project",
67+
"require": {
68+
"php": ">=5.3.0",
69+
"longman/telegram-bot": "*"
70+
}
71+
}
72+
```
73+
74+
And run composer update
75+
76+
77+
###### bot token
78+
You will notice that the Telegram Bot wants a value for `API_KEY`. This token may be obtained via a telegram client for your bot. See [this](https://core.telegram.org/bots#botfather) link if you are unsure of how to so this.
79+
80+
81+
## Usage
82+
You must set [WebHook](https://core.telegram.org/bots/api#setwebhook)
83+
84+
Create set.php and put:
85+
```php
86+
<?php
87+
88+
$loader = require __DIR__.'/vendor/autoload.php';
89+
90+
$API_KEY = 'your_bot_api_key';
91+
92+
// create Telegram API object
93+
$telegram = new Longman\TelegramBot\Telegram($API_KEY);
94+
95+
// set webhook
96+
$telegram->setWebHook('https://yourdomain/yourpath_to_hook.php');
97+
98+
```
99+
100+
After c reate hook.php and put:
101+
```php
102+
<?php
103+
104+
$loader = require __DIR__.'/vendor/autoload.php';
105+
106+
$API_KEY = 'your_bot_api_key';
107+
108+
// create Telegram API object
109+
$telegram = new Longman\TelegramBot\Telegram($API_KEY);
110+
111+
// handle telegram webhook request
112+
$telegram->handle();
113+
114+
```
115+
116+
117+
This code is available on [Github][0]. Pull requests are welcome.
118+
119+
Troubleshooting
120+
---------------
121+
If you like living on the edge, please report any bugs you find on the [PHP Telegram Bot issues](https://github.com/akalongman/php-telegram-bot/issues) page.
122+
123+
124+
## Credits
125+
126+
Created by [Avtandil Kikabidze][1].
127+
128+
[0]: https://github.com/akalongman/php-telegram-bot
129+
[1]: mailto:[email protected]

composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
2-
"name": "longman/telegram-api",
2+
"name": "longman/telegram-bot",
33
"type": "library",
4-
"description": "PHP library",
5-
"keywords": ["string", "utilities", "translit"],
4+
"description": "PHP telegram bot",
5+
"keywords": ["telegram", "bot", "api"],
66
"license": "MIT",
7-
"homepage": "https://github.com/akalongman/telegram-api",
7+
"homepage": "https://github.com/akalongman/php-telegram-bot",
88
"support": {
9-
"issues": "https://github.com/akalongman/telegram-api/issues",
10-
"source": "https://github.com/akalongman/telegram-api"
9+
"issues": "https://github.com/akalongman/php-telegram-bot/issues",
10+
"source": "https://github.com/akalongman/php-telegram-bot"
1111
},
1212
"authors": [
1313
{
@@ -22,7 +22,7 @@
2222
},
2323
"autoload": {
2424
"psr-4": {
25-
"Longman\\TelegramApi\\": "src/"
25+
"Longman\\TelegramBot\\": "src/"
2626
}
2727
},
2828
"autoload-dev": {

src/Commands/DateCommand.php

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
/*
3+
* This file is part of the TelegramApi package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace Longman\TelegramApi\Commands;
11+
12+
use Longman\TelegramApi\Request;
13+
use Longman\TelegramApi\Command;
14+
use Longman\TelegramApi\Entities\Update;
15+
16+
class DateCommand extends Command
17+
{
18+
private $google_api_key = '';
19+
private $base_url = 'https://maps.googleapis.com/maps/api';
20+
private $date_format = 'd-m-Y H:i:s';
21+
22+
23+
24+
25+
26+
27+
private function getCoordinates($location) {
28+
$url = $this->base_url.'/geocode/json?';
29+
$params = 'address='.urlencode($location);
30+
if (!empty($this->google_api_key)) {
31+
$params .= '&key='.$this->google_api_key;
32+
}
33+
34+
$data = $this->request($url.$params);
35+
if (empty($data)) {
36+
return false;
37+
}
38+
39+
$data = json_decode($data, true);
40+
if (empty($data)) {
41+
return false;
42+
}
43+
44+
if ($data['status'] !== 'OK') {
45+
return false;
46+
}
47+
48+
$lat = $data['results'][0]['geometry']['location']['lat'];
49+
$lng = $data['results'][0]['geometry']['location']['lng'];
50+
$acc = $data['results'][0]['geometry']['location_type'];
51+
$types = $data['results'][0]['types'];
52+
53+
return array($lat, $lng, $acc, $types);
54+
}
55+
56+
57+
58+
private function getDate($lat, $lng) {
59+
$url = $this->base_url.'/timezone/json?';
60+
61+
62+
$timestamp = time();
63+
64+
65+
$params = 'location='.urlencode($lat).','.urlencode($lng).'&timestamp='.urlencode($timestamp);
66+
if (!empty($this->google_api_key)) {
67+
$params .= '&key='.$this->google_api_key;
68+
}
69+
70+
$data = $this->request($url.$params);
71+
if (empty($data)) {
72+
return false;
73+
}
74+
75+
$data = json_decode($data, true);
76+
if (empty($data)) {
77+
return false;
78+
}
79+
80+
if ($data['status'] !== 'OK') {
81+
return false;
82+
}
83+
84+
85+
$local_time = $timestamp + $data['rawOffset'] + $data['dstOffset'];
86+
87+
return array($local_time, $data['timeZoneId']);
88+
}
89+
90+
private function getFormattedDate($location) {
91+
if (empty($location)) {
92+
return 'The time in nowhere is never';
93+
}
94+
list($lat, $lng, $acc, $types) = $this->getCoordinates($location);
95+
96+
if (empty($lat) || empty($lng)) {
97+
return 'It seems that in "'.$location.'" they do not have a concept of time.';
98+
}
99+
100+
list($local_time, $timezone_id) = $this->getDate($lat, $lng);
101+
102+
103+
104+
$date_utc = new \DateTime(date('Y-m-d H:i:s', $local_time), new \DateTimeZone($timezone_id));
105+
//$timestamp = $date_utc->format($this->date_format);
106+
107+
$return = 'The local time in '.$timezone_id.' is: '.date($this->date_format, $local_time).'';
108+
109+
return $return;
110+
}
111+
112+
113+
private function request($url) {
114+
$ch = curl_init();
115+
$curlConfig = array(
116+
CURLOPT_URL => $url,
117+
CURLOPT_RETURNTRANSFER => true,
118+
);
119+
120+
curl_setopt_array($ch, $curlConfig);
121+
$response = curl_exec($ch);
122+
123+
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
124+
if ($http_code !== 200) {
125+
throw new Exception('Error receiving data from url');
126+
}
127+
curl_close($ch);
128+
129+
return $response;
130+
}
131+
132+
133+
public function execute() {
134+
$update = $this->getUpdate();
135+
$message = $this->getMessage();
136+
137+
138+
139+
$chat_id = $message->getChat()->getId();
140+
$message_id = $message->getMessageId();
141+
$text = $message->getText(true);
142+
143+
144+
145+
if (empty($text)) {
146+
$text = 'You must specify location in format: /date <city>';
147+
} else {
148+
$date = $this->getformattedDate($text);
149+
if (empty($date)) {
150+
$text = 'Can not find date for location: '.$text;
151+
} else {
152+
$text = $date;
153+
}
154+
}
155+
156+
157+
$data = array();
158+
$data['chat_id'] = $chat_id;
159+
$data['reply_to_message_id'] = $message_id;
160+
$data['text'] = $text;
161+
162+
163+
$result = Request::sendMessage($data);
164+
}
165+
166+
167+
}

src/Commands/EchoCommand.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ public function execute() {
3333

3434
$result = Request::sendMessage($data);
3535

36-
37-
var_dump($result);
38-
die;
3936
}
4037

4138

src/Commands/HelpCommand.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/*
3+
* This file is part of the TelegramApi package.
4+
*
5+
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace Longman\TelegramApi\Commands;
11+
12+
use Longman\TelegramApi\Request;
13+
use Longman\TelegramApi\Command;
14+
use Longman\TelegramApi\Entities\Update;
15+
16+
class HelpCommand extends Command
17+
{
18+
19+
public function execute() {
20+
$update = $this->getUpdate();
21+
$message = $this->getMessage();
22+
23+
24+
25+
$chat_id = $message->getChat()->getId();
26+
$text = $message->getText(true);
27+
28+
29+
$data = array();
30+
$data['chat_id'] = $chat_id;
31+
$data['text'] = 'GeoBot v. '.VERSION;
32+
33+
34+
$result = Request::sendMessage($data);
35+
36+
}
37+
38+
39+
}
40+

0 commit comments

Comments
 (0)