Skip to content

Commit

Permalink
Basic functions:
Browse files Browse the repository at this point in the history
 - server
 - parsing
 - json encoding
  • Loading branch information
xtrime-ru committed Jan 7, 2019
1 parent 72ab404 commit 7759217
Show file tree
Hide file tree
Showing 11 changed files with 877 additions and 6 deletions.
21 changes: 21 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# SWOOLE
SWOOLE_SERVER_ADDRESS=127.0.0.1
SWOOLE_SERVER_PORT=9503
SWOOLE_WORKER_NUM=1
SWOOLE_HTTP_COMPRESSION=1

# TELEGRAM CLIENT
TELEGRAM_API_ID=
TELEGRAM_API_HASH=

# TELEGRAM SOCKS5 PROXY (optional)
TELEGRAM_PROXY_ADDRESS=
TELEGRAM_PROXY_PORT=
TELEGRAM_PROXY_USERNAME=
TELEGRAM_PROXY_PASSWORD=

# CURL PROXY (optional)
# address of your socks5 proxy user:[email protected]:12000
CURL_PROXY=
# value of CURLPROXY_SOCKS5_HOSTNAME const
CURL_PROXY_TYPE=7
97 changes: 96 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,97 @@
# TelegramSwooleClient
Fast async php telegram client: MadelineProto + Swoole Server
Fast, simple, async php telegram client and parser: MadelineProto + Swoole Server

* Online server for tests (previous version, different request syntax): http://tg.i-c-a.su/?format=json&images=0&url=breakingmash
* My content aggregator: [https://i-c-a.su](https://i-c-a.su)

**Features**
* Fast async swoole server
* Use as micro-service
* Get any public telegram posts from groups as json

**TODO**
* RSS output
* flood protection (for use in public)
* logging

**Installation**

1. Get app_id and app_hash at [my.telegram.org](https://my.telegram.org/)
1. Swoole extension required: [Install swoole](https://github.com/swoole/swoole-src#%EF%B8%8F-installation)
1. Install this package:

a. Standalone:

1. download files from github and extract.
2. Run `composer install` inside unpacked directory

b. Existing project:

1. Add following into your project's composer.json
```
"repositories": [
{
"type": "git",
"url": "https://github.com/xtrime-ru/TelegramSwooleClient.git"
}
],
"require": {
"xtrime-ru/telegramswooleclient": "dev-master",
}
```
**Usage**
1. Install
1. Fill options in .env file (see .env.example)
1. Run server/parser
```
php server.php [--help] [-a|--address=127.0.0.1] [-p|--port=9503]
Options:
--help Show this message
-a --address Server ip (optional) (example: 127.0.0.1 or 0.0.0.0 to listen all hosts)
-p --port Server port (optional) (example: 9503)
Also all options can be set in .env file (see .env.example)
```
1. Get posts from any open channel
* Get 10 latests posts from any open channel via GET request:
`http://%address%:%port%/json/%channel%`
Example:
`http://127.0.0.1:9503/json/breakingmash`
* Get posts from multiple channels via POST:
Url: `http://127.0.0.1:9503/json/`
Headers: `Content-Type: application/json`
`form-data` and `x-www-form-urlencoded` should work,
but was not tested
Body:
```
{
"getHistory": [
{
"peer":"channel#1259060275"
},
{
"peer": "breakingmash",
"limit": 30,
"max_id": 200,
}
]
}
```
You can use any other options from https://docs.madelineproto.xyz/API_docs/methods/messages_getHistory.html
peer name can be provided in different formats: https://docs.madelineproto.xyz/API_docs/types/InputPeer.html
**Contacts**
* Telegram: [@xtrime](tg://resolve?domain=xtrime)
* Email: alexander(at)i-c-a.su
9 changes: 9 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
//Check if autoload has been already loaded (in case plugin installed in existing project)
if (!class_exists('TelegramSwooleClient')) {
require __DIR__ . '/vendor/autoload.php';
}
//Check if root env file hash been loaded (in case plugin installed in existing project)
if (!getenv('SWOOLE_SERVER_ADDRESS')){
(new Dotenv\Dotenv(__DIR__))->load();
}
22 changes: 17 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
{
"name": "xtrime-ru/telegramswooleclient",
"description": "Fast async php telegram client: MadelineProto + Swoole Server",
"description": "Fast, simple, async php telegram client and parser: MadelineProto + Swoole Server",
"type": "project",
"homepage": "http://tg.i-c-a.su/",
"license": "MIT",
"keywords": ["telegram", "mtproto", "protocol", "client", "PHP", "swoole", "async", "demon"],
"keywords": ["telegram", "mtproto", "protocol", "client", "PHP", "swoole", "async", "daemon", "coroutine", "parser", "micro-service"],
"require": {
"php": ">=7.2.0",
"danog/madelineproto": "dev-master",
"danog/madelineproto":"dev-master",
"amphp/dns": "dev-master#861cc857b1ba6e02e8a7439c30403682785fce96 as 0.9.9",
"amphp/file": "dev-master#5a69fca406ac5fd220de0aa68c887bc8046eb93c as 0.3.3",
"amphp/uri": "dev-master#f3195b163275383909ded7770a11d8eb865cbc86 as 0.1.3",
"vlucas/phpdotenv": "^2.4",
"jaeger/querylist": "dev-master",
"ext-json": "*",
"ext-swoole": ">=4.0"
},
"require-dev": {
"swoole/ide-helper": "dev-master"
},
"repositories": [
{
"type": "git",
"url": "https://github.com/danog/phpseclib"
},
{
"type": "git",
"url": "https://github.com/swoole/ide-helper.git"
}
],
"minimum-stability": "dev",
Expand All @@ -25,8 +37,8 @@
}
],
"autoload": {
"psr-0": {
"xtrime-ru\\TelegramSwooleClient\\": "src/"
"psr-4": {
"TelegramSwooleClient\\": "src/"
}
}
}
47 changes: 47 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

return [
'swoole' => [
'server' => [
'address' => (string) getenv('SWOOLE_SERVER_ADDRESS'),
'port' => (string) getenv('SWOOLE_SERVER_PORT'),
],
'options'=> [
'worker_num' => (int) getenv('SWOOLE_WORKER_NUM'),
'http_compression' => (bool) getenv('SWOOLE_HTTP_COMPRESSION'),
]
],
'telegram' => [
'app_info' => [ // obtained in https://my.telegram.org
'api_id' => getenv('TELEGRAM_API_ID'),
'api_hash' => getenv('TELEGRAM_API_HASH'),
],
'logger' => [ // Logger settings
'logger' => 3, // Logs disabled.
'logger_level' => 5, // Logging level, available logging levels are: ULTRA_VERBOSE - 0, VERBOSE - 1 , NOTICE - 2, WARNING - 3, ERROR - 4, FATAL_ERROR - 5.
],
'updates' => [
'handle_updates' => false, // Should I handle updates?
'handle_old_updates' => false, // Should I handle old updates on startup?
],
'connection_settings' => [
'all' => [
'proxy' => '\SocksProxy',
'proxy_extra' => [
'address' => getenv('TELEGRAM_PROXY_ADDRESS'),
'port' => getenv('TELEGRAM_PROXY_PORT'),
'username' => getenv('TELEGRAM_PROXY_USERNAME'),
'password' => getenv('TELEGRAM_PROXY_PASSWORD'),
]
]
]
],
'curl' => [
'proxy' => [
'address' => getenv('CURL_PROXY_ADDRESS'),
'port' => getenv('CURL_PROXY_PORT'),
'username' => getenv('CURL_PROXY_USERNAME'),
'password' => getenv('CURL_PROXY_PASSWORD'),
]
]
];
45 changes: 45 additions & 0 deletions server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

require_once __DIR__ . '/bootstrap.php';

if (PHP_SAPI !== 'cli') {
throw new \Exception('Start in CLI');
}

$shortopts = 'a::p::';
$longopts = [
'address::', // ip адресс сервера, необязательное значение
'port::', // порт сервера, необязательное значение
'help', //нужна ли справка?
];
$options = getopt($shortopts, $longopts);
$options = [
'address' => $options['address'] ?? $options['a'] ?? '',
'port' => $options['port'] ?? $options['p'] ?? '',
'id' => $options['id'] ?? $options['i'] ?? '',
'hash' => $options['hash'] ?? $options['h'] ?? '',
'help' => isset($options['help']),
];

if ($options['help']) {
$help = 'Fast, simple, async php telegram parser: MadelineProto + Swoole Server
usage: php server.php [--help] [-a|--address=127.0.0.1] [-p|--port=9503]
Options:
--help Show this message
-a --address Server ip (optional) (example: 127.0.0.1)
-p --port Server port (optional) (example: 9503)
Also all options can be set in .env file (see .env.example)
Example:
php server.php
';
echo $help;
exit;
}

$client = new \TelegramSwooleClient\Client();
new TelegramSwooleClient\Server($client, $options);
Loading

0 comments on commit 7759217

Please sign in to comment.