I’m not able to trigger a message to a group chat from my Azure-Bot for Microsoft Teams from my PHP application and don’t know why. Tried a lot of things now and asked GPT – but nothing worked. But when I’m using the Botbuilder framework in a javascript environment, the bot answers to my messages.
The credentials are valid and API permissions are also fine. The error I received:
URL: https://smba.trafficmanager.net/emea/v3/conversations/19:[email protected]/activities HTTP response headers: HTTP/1.1 401 Unauthorized Keep-Alive: true Content-Length: 61 Content-Type: application/json; charset=utf-8 Server: Microsoft-HTTPAPI/2.0 MS-CV: VWPVTjV2B0+EmgoDiiux+w.0 Date: Fri, 07 Jun 2024 07:55:01 GMT Connection: close Response: {“message”:”Authorization has been denied for this request.”} Array ( [message] => Authorization has been denied for this request. )
<?php
public function bot_reporting()
{
$params = array(
'tenant_id' => 'xxx',
'client_id' => 'xxx',
'client_secret' => 'xxx',
'bot_service_url' => 'https://smba.trafficmanager.net/emea',
'chat_id' => '19:[email protected]',
'bot_id' => 'xxx'
);
$this->load->library('bot', $params);
try {
$response = $this->bot->sendMessage('Test!');
print_r($response);
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
}
?>
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Bot
{
protected $ci;
private $tenantId;
private $clientId;
private $clientSecret;
private $scope;
private $botServiceUrl;
private $chatId;
private $botId;
public function __construct($params = array())
{
$this->ci =& get_instance();
// Setze die Konfigurationswerte
$this->tenantId = isset($params['tenant_id']) ? $params['tenant_id'] : '';
$this->clientId = isset($params['client_id']) ? $params['client_id'] : '';
$this->clientSecret = isset($params['client_secret']) ? $params['client_secret'] : '';
$this->scope = 'https://graph.microsoft.com/.default';
$this->botServiceUrl = isset($params['bot_service_url']) ? $params['bot_service_url'] : '';
$this->chatId = isset($params['chat_id']) ? $params['chat_id'] : '';
$this->botId = isset($params['bot_id']) ? $params['bot_id'] : '';
}
// Funktion, um ein Access Token von Azure AD zu erhalten
private function getAccessToken()
{
$url = "https://login.microsoftonline.com/{$this->tenantId}/oauth2/v2.0/token";
$data = array(
'grant_type' => 'client_credentials',
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'scope' => $this->scope
);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencodedrn",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) {
throw new Exception('Error retrieving access token');
}
$response = json_decode($result, true);
if (isset($response['error'])) {
throw new Exception('Error retrieving access token: ' . $response['error_description']);
}
return $response['access_token'];
}
// Funktion, um eine Nachricht zu senden
public function sendMessage($messageContent)
{
$accessToken = $this->getAccessToken();
// Debugging: AccessToken ausgeben
echo "Access Token: " . $accessToken . "n";
$url = "{$this->botServiceUrl}/v3/conversations/{$this->chatId}/activities";
// Debugging: URL ausgeben
echo "URL: " . $url . "n";
$data = array(
'type' => 'message',
'text' => $messageContent
);
$options = array(
'http' => array(
'header' => "Authorization: Bearer $accessTokenrn" .
"Content-type: application/jsonrn",
'method' => 'POST',
'content' => json_encode($data),
'ignore_errors' => true // To capture the response in case of error
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
// Capture the HTTP response headers
$http_response_header = isset($http_response_header) ? $http_response_header : [];
if ($result === FALSE) {
$error = error_get_last();
echo 'HTTP response headers: ' . implode("n", $http_response_header) . "n";
throw new Exception('Error sending message: ' . $error['message']);
}
echo 'HTTP response headers: ' . implode("n", $http_response_header) . "n";
echo 'Response: ' . $result . "n";
return json_decode($result, true);
}
}
?>