Skip to content

Instantly share code, notes, and snippets.

@manusreload
Created October 2, 2016 18:50
Show Gist options
  • Save manusreload/b5460c8910b2195415499703aa6aa69f to your computer and use it in GitHub Desktop.
Save manusreload/b5460c8910b2195415499703aa6aa69f to your computer and use it in GitHub Desktop.
Telegram Simple API
<?php
/**
* Created by PhpStorm.
* User: manus
* Date: 2/10/16
* Time: 20:26
*
* General Propose Functions
*/
function post($url, $fields, $header = array())
{
$fields_string = "";
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return $result;
}
function get($url, $header = array())
{
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return $result;
}
<?php
/**
* Created by PhpStorm.
* User: manus
* Date: 2/10/16
* Time: 20:26
*/
include "functions.php";
define("ENDPOINT", "https://api.telegram.org");
define("API_TOKEN", "294405723:AAEd1GYu6vsm7oJYKBlvAYRvvsfTAodJOps");
function telegramRequest($function, $arguments = array())
{
$url = ENDPOINT . "/bot" . API_TOKEN . "/$function";
$response = post($url, $arguments);
return json_decode($response);
}
// Make request
$response = telegramRequest("getMe");
if($response->ok)
{
echo "Enviando mensaje...\n";
$response = telegramRequest("sendMessage", array(
'chat_id' => "manus_reload",
'text' => "Hola mundo bot!"
));
print_r($response);
}
else
{
echo "Bot mal configurado\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment