You are here

function _scn_send_telegram in Simple Comment Notify 8

Send to telegram common function.

1 call to _scn_send_telegram()
scn_entity_insert in ./scn.module
Implements hook_entity_insert().

File

./scn.module, line 57
Main file for the scn module.

Code

function _scn_send_telegram($url, $token, $chatid, $proxy = FALSE, $proxy_server = NULL, $proxy_login = NULL, $proxy_pass = NULL) {
  $curl = curl_init();
  if ($curl) {
    $text = t('New comment on @siteName: @url', [
      '@siteName' => \Drupal::config('system.site')
        ->get('name'),
      '@url' => $url,
    ]);
    $query = "https://api.telegram.org/bot" . $token . "/sendMessage?disable_web_page_preview=true&chat_id=" . $chatid . "&text=" . urlencode($text);
    curl_setopt($curl, CURLOPT_URL, $query);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_POST, TRUE);
    if ($proxy) {
      curl_setopt($curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
      curl_setopt($curl, CURLOPT_PROXY, $proxy_server);
      if ($proxy_login) {
        curl_setopt($curl, CURLOPT_PROXYUSERPWD, $proxy_login . ':' . $proxy_pass);
      }
    }
    if (curl_exec($curl) === FALSE) {
      \Drupal::logger('Simple Comment Notify')
        ->notice(curl_error($curl));
    }
    curl_close($curl);
  }
  else {
    \Drupal::logger('Simple Comment Notify')
      ->notice(t("Can't initialize cURL. Is it installed on the server?"));
  }
}