测试发送消息到Microsoft Teams
创建测试频道:
点击团队右侧“···”,点击添加频道:
然后完善频道信息:
创建好频道之后,点击频道右侧“···”管理频道:
可以进行频道人员的添加与删除管理:
点击频道右侧“···”连接器:
在搜索框中,搜索webhook进行搜索:
将Incoming Webhook 添加到频道中:
可以对频道的名称和LOGO进行编辑,编辑成功后点击创建:
然后可以看到频道的URL地址,复制这个地址,再点击完成:
然后返回频道里面就可以看到:
尝试代码中调用:
首先在.env中,配置:
TEAMS_WEBHOOK_URL_KAKA_TEST="https://XXXX"
然后在config/app.php中设置:
'kaka_test' => env('TEAMS_WEBHOOK_URL_KAKA_TEST', 'https://XXXX),
然后在config/services.php中添加teams服务:
1 <?php 2 3 return [ 4 5 /* 6 |-------------------------------------------------------------------------- 7 | Third Party Services 8 |-------------------------------------------------------------------------- 9 | 10 | This file is for storing the credentials for third party services such 11 | as Mailgun, Postmark, AWS and more. This file provides the de facto 12 | location for this type of information, allowing packages to have 13 | a conventional file to locate the various service credentials. 14 | 15 */ 16 17 'teams' => [ 18 'webhook_url' => env('TEAMS_WEBHOOK_URL_KAKA_TEST'), 19 'driver' => 'webhook', 20 ], 21 22 ];
找一个代码进行测试调用,我找的是我的FP2.0API项目下面/app/Console/Commands/BirthdayCheck.php 简单进行修改测试的。在Laravel中,支持的通知驱动程序包括mail
、database
、broadcast
、nexmo
等等。如果想通过Microsoft Teams发送通知,可以考虑使用其他方法,如Webhook或Microsoft Teams的API。
以下是我的解决方案:
使用Guzzle库来发送HTTP请求到Microsoft Teams的Webhook URL:
如果没有Guzzle库,可以使用composer进行安装:
composer require guzzlehttp/guzzle
在BirthdayCheck.php
文件中,使用Guzzle库来发送HTTP请求到Microsoft Teams的Webhook URL。以下代码作为参考:
1 use GuzzleHttp\Client; 2 3 public function handle() 4 { 5 $today = date('md'); 6 $birth_staffs = User::where('status', '=', 1)->pluck('name')->toArray(); 7 8 if (!empty($birth_staffs)) { 9 $webhookUrl = config('app.kaka_test'); 10 $client = new Client(); 11 12 foreach ($birth_staffs as $staff) { 13 $message = "Today is the birthday of $staff."; 14 15 $client->post($webhookUrl, [ 16 'json' => [ 17 'text' => $message, 18 ], 19 ]); 20 } 21 } 22 }
然后在终端命令“php artisan BirthDayCheck”进行测试:
返回频道查看效果:
成功收到提示信息。