Created
January 2, 2020 11:12
-
-
Save ybagheri/45e7bcf93906b85e38d0e91bf9c03023 to your computer and use it in GitHub Desktop.
Telegram api bot forward forwardMessage from a channel to another.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$token = YOUR_BOT_TOKEN; | |
//your bot must be admin of both channel. | |
$original_from_chat_id = ORIGINAL_CHAT_ID; // like this -1001122688109 | |
$target_chat_id = TARGET_CHAT_ID; // like this -1001131688104 | |
$forward_from_message_id_start = 2356; | |
$forward_from_message_id_finish = 2501; | |
$startTime = time(); | |
for ($x = $forward_from_message_id_start; $x <= $forward_from_message_id_finish; $x++) { | |
if(time() - $startTime > 3){ | |
$startTime=time(); | |
sleep(1); | |
} | |
$res = telegramHTTPRequest($token, 'forwardMessage', ['chat_id' => $target_chat_id ,'from_chat_id' => $original_from_chat_id ,'disable_notification' => true , 'message_id' => $x ]); | |
} | |
function allFileInDir($path) | |
{ | |
$files = new \RecursiveIteratorIterator( | |
new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS), | |
\RecursiveIteratorIterator::CHILD_FIRST | |
); | |
$arr = []; | |
foreach ($files as $fileinfo) { | |
if (!$fileinfo->isDir()) { | |
$arr[] = $fileinfo->getRealPath(); | |
} | |
} | |
asort($arr); | |
$array_with_new_keys = array_values($arr); | |
return $array_with_new_keys; | |
} | |
function makeHTTPRequest($url, $method, $datas = [], $certAddress = false) | |
{ | |
//$proxy_userpwd must be like this ===> "username:pass" | |
//'CURLOPT_CAINFO' => "/etc/certs/cabundle.pem" is CURLOPT_CAINFO - path to Certificate Authority (CA) bundle | |
// example : 'CURLOPT_CAINFO' => "/etc/certs/cabundle.pem" | |
$url = rtrim($url, '/'); | |
$url = $url . "/" . trim($method, '/'); | |
$ch = curl_init(); | |
if (isset($datas['proxy_url']) && (isset($datas['proxy_port']))) { | |
curl_setopt($ch, CURLOPT_PROXY, $datas['proxy_url']); //your proxy url | |
curl_setopt($ch, CURLOPT_PROXYPORT, $datas['proxy_port']); // your proxy port number | |
unset($datas['proxy_url']); | |
unset($datas['proxy_port']); | |
if (isset($datas['proxy_userpwd'])) { | |
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $datas['proxy_userpwd']); //username:pass | |
unset($datas['proxy_userpwd']); | |
} | |
} | |
curl_setopt($ch, CURLOPT_URL, $url); | |
if (isset($datas['CURLOPT_CAINFO'])) { | |
curl_setopt($ch, CURLOPT_CAINFO, $datas['CURLOPT_CAINFO']); | |
} | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
// curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($datas)); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $datas); | |
$res = curl_exec($ch); | |
if (curl_error($ch)) { | |
// var_dump(curl_error($ch)); | |
return curl_error($ch); | |
} else { | |
return json_decode($res); | |
} | |
} | |
function telegramHTTPRequest($token, $method, $datas = null) | |
{ | |
$url = "https://api.telegram.org/bot" . $token; | |
return isset($datas) ? makeHTTPRequest($url, $method, $datas) : makeHTTPRequest($url, $method); | |
} | |
// static function methodParamArgArr | |
function removeNullValue(array $array) | |
{ | |
$temp = []; | |
foreach ($array as $key => $value) { | |
if (!is_null($value)) { | |
$temp[$key] = $value; | |
} | |
} | |
return $temp; | |
} | |
function upload($type, $path = null, $fields = [], $file_id = null,$token=null) | |
{ | |
$allFields = [ | |
'chat_id' =>null, | |
'photo' => null, | |
'caption' => null, | |
'parse_mode' => null, | |
'disable_notification' => null, | |
'reply_to_message_id' => null, | |
'reply_markup' => null, | |
'audio' => null, | |
'duration' => null, | |
'performer' => null, | |
'title' => null, | |
'document' => null, | |
'video' => null, | |
'width' => null, | |
'height' => null, | |
'supports_streaming' => null, | |
'voice' => null, | |
]; | |
if(! empty($fields)){ | |
foreach ($fields as $key => $value) { | |
$allFields[$key] = $fields[$key]; | |
} | |
} | |
if (is_null($file_id)) { | |
//Upload to Telegram | |
if (function_exists('curl_file_create')) { // php 5.6+ | |
$cFile = curl_file_create($path); | |
} else { | |
$cFile = '@' . realpath($path); | |
} | |
$allFields[$type] = $cFile; | |
} else { | |
$allFields[$type] = $file_id; | |
} | |
// if (isset($message->caption)) { | |
// $allFields['caption'] = $message->caption; | |
// } | |
// if (! is_null($ReplyMarkp)) { | |
// $allFields['reply_markup'] = json_encode($ReplyMarkp); | |
// } | |
if ($type != 'video_note') { | |
$method = 'send' . ucfirst($type); | |
} else { | |
$method = 'sendVideoNote'; | |
} | |
// \Ybagheri\EasyHelper::telegramHTTPRequest($this->environment["BOT_TOKEN"),'sendMessage',['chat_id' => $this->fromId,'text' =>var_export($allFields,true)]); | |
//var_dump($allFields); | |
$allFields = removeNullValue($allFields); | |
//var_dump($allFields); | |
echo 'token '.$token.PHP_EOL; | |
echo 'type '.$type.PHP_EOL; | |
var_dump($allFields); | |
return telegramHTTPRequest($token, $type, $allFields); | |
} |
How to use it? Does it work?
add bot token here
$token = YOUR_BOT_TOKEN;
promote a bot to a both channel/group as admin,
insert channel ID here
$original_from_chat_id = ORIGINAL_CHAT_ID; // like this -1001122688109
$target_chat_id = TARGET_CHAT_ID; // like this -1001131688104
from which post to your desire post, update this line
$forward_from_message_id_start = 2356;
$forward_from_message_id_finish = 2501;
then run php file in cmd
php forward.php
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use it? Does it work?