در حال بارگذاری ...

Testing Slack Webhook0:44

that we have this unique endpoint. So, now check this out. From the terminal, I'm going to submit a POST request, and I like to use this tool called HTTPy. So, you can grab that on GitHub, takes five seconds, or you can manually submit a curl request, but this is a little more sane to me. So, I'm going to submit a POST request where, and if we come back, you can see we can pass through some text content. So, I could say text equals I am a notification. Okay, and sure enough, I have Slack notifications turned on, so we see it there, and if I switch back over, sure enough, it works. I mean, it's really very, very simple. So, now, yeah, with Laravel, we only need to select the Slack notification channel. Let's try it out. We're going to make a notification, and we'll call it PaymentReceived. Okay, back to Sublime. So, that'll be an app/Notifications/PaymentReceived, and specifically, we could do mail and Slack. So, that means every time a payment

Creating Slack Notification1:31

and we'll call it paymentReceived. Okay, back to Sublime. So, that'll be an app, notifications, paymentReceived, and specifically, we could do mail and Slack. So, that means every time a payment comes in, we're going to fire off an email to the administrator, as well as a Slack notification, or let's just stick with one for now. Okay, so that means all of the toMail specific information can leave, and in fact, because I'm only using one channel, twoArray can leave as well. Okay, so what do we need here? We're going to have a, of course, toSlack method because the channel is named Slack, so we define a toSlack message that returns an instance of our MailMessage. Return new SlackMessage, and I'm going to import that at the top. There we go. Okay, so let's do this. I'm going to send a successful Slack notification where the content is, and I'm just going to hard code it for now. A new payment was just processed. Okay, and it's as easy as that.

so let's do this. I'm going to send a successful Slack notification where the content is, and I'm just going to hard code it for now. A new payment was just processed. Okay, and it's as easy as that to start. However, what about that webhook endpoint that we need? To do that, well, these methods will receive a Notifiable instance, which typically will be your User class, but not always. But anyways, in this case, it will be the User, so we need to define the endpoint, and I'm going to hard code it in this case, but for your project, you probably want to have some form where the user can register a Slack webhook, and then they just paste that into your form, and that's their way of saying, I want notifications sent to this webhook, and that way for every user, yeah, that Slack webhook we hit will be unique to them. So we're going to set routeNotificationForSlack, and yeah, you would maybe store a column on your User model called slack_url or slack_webhook, you know,

Setting Webhook Routing3:09

webhook we hit will be unique to them. So we're going to set route notification for Slack, and yeah, you would maybe store a column on your User model called Slack URL or Slack webhook, you know, whatever you want to call it. In my case, though, I'm just going to hard code it here to keep it quick. So we go back to Chrome. Here is our webhook URL. Switch back, and I'm going to, once again, return that hard coded. Naturally, you would not want all users to hit this endpoint, so it only applies to the administrator. So make dynamic is a little instruction to you. Okay, but that's it really. Define a notification, set the channel, return a new Slack message with your content, and then on your notifiable entity, determine where this webhook endpoint will be, and we're done. So now let's go to our routes file and just set up a quick demonstration here. So maybe we'll find the administrator, and we'll just assume that's the User with an ID of 1,

Triggering Notification in Routes3:59

and we're done. So now let's go to our routes file and just set up a quick demonstration here. So maybe we'll find the administrator, and we'll just assume that's the User with an ID of one, and we're going to say admin. I want to notify them that a new payment was received, and I will import that at the very top. Now, in real life, you might pass through like a payment object or something like that. For now, why don't we just send through the User who made the payment? So we'll say user, and this will just be a different User, and I'm going to pass that in. Okay, so we'll come back here. We will define it, user, and then accept that. So this is the User who made the payment. That means when we hit the homepage, we will notify the administrator that a new payment was received, and specifically we're going to notify them via Slack. Okay, let's try it. Laravel53.dev, and there we go. We get a web and an app-specific notification.

Adding Dynamic Attachments4:44

that a new payment was received, and specifically we're going to notify them via Slack. Okay, let's try it. Laravel53.dev, and there we go. We get a web and an app-specific notification because I've downloaded the Slack Mac app. But anyways, yeah, if we come back, there it is. So when I say you can do this in one or two minutes, I'm not joking. It's really very simple. So why don't we make it a little bit more dynamic? We could say for the content, a new payment was received from, and then we can grab the $user's name, and further with Slack, we have this concept of attachments. So this would be like if you want to add a link and then a table of all the information. Let me just give you an example here. We're going to accept the attachment, and then specifically we'll say the title will be payment, and the URL for it will be, I don't know, some link to our app. Maybe we can view

We're going to accept the attachment, and then specifically we'll say the title will be payment, and the URL for it will be, I don't know, some link to our app. Maybe we can view all payments with /payment/, and the ID of the payment that I will hard code. Next, we're going to set the fields for, basically it's going to render as like a little table here. So I'm going to set some fields, and this can be anything I want. Basic key value pairs. For example, I could say the amount would be, and once again, I will hard code that, but once again, you would maybe accept the payment object, and then you would fetch that here. And then finally, let's just stick with two. Who is it from? This will be, once again, this user name, and how about we change this to you have moolah. Okay, so now they get a notification that they have money, and specifically, they will have a link to the payment itself, and then finally, two custom fields. Okay, so let's go

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟