Build a Twitter/X Scheduler with .NET 8 & Hangfire ๐Ÿฆ

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey friends Patrick here welcome to another Twitter API with net tutorial now in this tutorial well you read the title and saw the thumbnail we're going to build our own Twitter scheduler so how would you work with net the Twitter API version 2 that's important right and Hang Fire to schedule your tweets and well publish them into the world this is what we're going to do in this tutorial here and if you like this tutorial and learned something guys I'd really appreciate it if you just just click that like button smash it if you want to and maybe you even want to subscribe to my channel it would mean the world to me and it does make a difference and if you want to support my work even further then scroll down to the video description and there's my new patreon page and thank you so much to my four patrons already guys I love you forever thank you so much for that and now let's start with the tutorial alright first a quick recap you can check out the time codes of course if you already watched the first tutorial in the series what we had to do is well we just created a donated web API and sorry if you hear the loud cars outside it is really really hot in here so we had to open the window hope you can accept my apologies but anyways regarding the dependencies you see here packages we've got tweet Envy the tweet in the API this is what we are using to post tweets right so here installed you can see tweet in the API that's it they unfortunately stopped developing that thing see it here in April 2021 so version two of the Twitter API is not fully supported so we had to build something ourselves and yes I know this is a fat controller but I think for this tutorial it's enough and maybe I built a bigger thing out of that and then of course I will use services for instance anyways here in that controller first thing you have to do in our post Tweet endpoint is set all your clients well get the client and set all your keys again did that in the first tutorial you've got an API key API secret and also then uh client key and a client secret token that's the right term I think and with that then we execute an advanced request meaning we have a custom request this is what is necessary with tweet NV because they haven't implemented a publish or a post method yet maybe they do it sometime I'm not sure about that but if you wanna do it meaning publish a tweet then you have to do it that way and for that we have in this little method here build Twitter request and this thing now well edit a model here the post Tweet request dto nothing more than just the text property and this thing as you can see here we serialized that to have a Json body right and then we create a string content out of that encoded with ETF 8 and a set the media type to application and Json and then this is the the URL the route from the Twitter API we set the HTTP request method to post and then set the content there and with that we can Will publish a tweet Let's test that real quick when we start this we see our endpoint here try this out and play around with the Twitter API again while recording the next YouTube tutorial .net why not hit execute and we see this is the result we have the ID we have the text and we go here now to my profile we can now see this beautiful post see it here 10 seconds ago and we can click on that thing and this is then the same ID right I can enter it here as well and we get it isn't that nice so this works already but now I want more I want to build my own Twitter scheduler and for that I'm using Hang Fire real quick I'm currently creating a.net web devjumps.course which will be available for free we're gonna have a look at web apis and Entity framework there for instance and if you want to be the first to know when this thing is available then I recommend just check out the link in the video description and I will send you an email when you can get it so scroll a bit further down and then you know what to do and now let's continue with the tutorial all right that's the wrong tab so we have to install some packages right click the Twitter X scheduler project in my case manage nougat packages and now here first thing we browse for Hang Fire all right and what we actually actually need is here Hank fire ASP net core almost 60 million downloads so with that you know that this is the right thing we just click on install accept everything and after that we are not done oh no we need more the next thing is Hang Fire SQL Server because I want to use SQL server for that I downloaded and installed SQL Server Express for that you can do that too real quick if you want to know how to get it SQL Server Express you can just Google that and then you see the downloads and it you can then even use the developer version full featured free edition didn't want to download it now but still here also Express a free edition of SQL Server idea for a development production for desktop weapons one server applications right this is what I use and when you also download this thing then I also recommend the SQL Server management Studio that'll be this thing here all right and with that you can manage your database don't have to use it you can also use visual studio for that but I just like it and you will see in a sec what is going on when we use it so Hank fire SQL Server is have I installed that no I did okay then to be able to use that meaning Hang Fire with SQL Server we also need a SQL client let me have a quick look what is going on here got a warning I'm not sure about that let's just continue so for that to make this work we also need something else and that would be the Microsoft data SQL client all right so this is used by hangfire and that's why we need it there's a pre-release version we can do it because I'm also using the preview of.net or RC no not rc1 in this case it's the preview Edition I think because I started this before rc1 was released so let's just install this although now I've got rc1 installed so yeah rc1 should be used anyways let's just try that we'll see in a minute if this is working so we've got now Hang Fire asp.core Hang Fire SQL server and tweet Envy of course and data SQL client what else well not really necessary but there is one situation why I wanna where I wanna map a dto so let's just install maps to Loft this little Library here 17 million downloads your decision if you want to use maps or Auto map all right so now I'm using mapster here as well okay and now we can go back to our code almost first thing we have to do is create our database right because now Hang Fire is using our database to will make the schedule magic happen you don't need a cron drop any kind of Windows Server drops anything like that not necessary you can run this on your local machine if you want to but of course you can also deploy it then but still even then you don't have to use the any kind of jobs all right so here video game database we can actually remove this tweets is the one that I also want to remove because I'm adding this time tracker is from the dotted Web Academy just ignore this thing but now here I will again create my tweets database all right so this is just an empty empty database this is necessary you have to create it up front because Hang Fire will not create the database by itself no tables here so far but there will be some in a sec so we've got our packages we've got our database now we have to register Hang Fire so back to the program CS now and in here we say Builder services and then add hang file yeah we need the using director here actually hang fire it is using Hang Fire this looks great add Hang Fire and then here configuration and then for the configuration we actually say use SQL Server storage and here now we need a connection string and in my case again since I'm using SQL Express locally this could look like that so server would be period backslash backslash SQL Express for the one on a localhost then the database name which is tweets then we say trusted underscore connection and set this to true and Trust server certificate also true all right hope this works trust server that's what I was looking for at the semicolon so again we register with Builder services at Hank fire then configuration is configuration use SQL Server storage and then the connection string which is the server equals period backslash backslash SQL Express then database not basis database is tweets our tweets database we just created then we set the trusted connection to true and also trust server certificate also true and with that this should work and the last thing we have to do here is Builder services and Hang Fire server that's it what is this doing well we don't see it here but you will see what this is doing in a minute but additionally to the server we also need a dashboard where we can then see what is currently actually scheduled and which is also simply amazing when you use Hang Fire so here now we just say app and then use Hang Fire dashboard that's the one all right so with that add Hang Fire as a service add Hang Fire server and then use Hang Fire dashboard this is everything we need and there's one more thing we have to change in regards let's say to the configuration before we can actually Implement our scheduling feature and this is in our project file we see invariant globalization this is set to true and this doesn't work with uh this package here or in essence with the function that this package here is using meaning it relies on certain globalization features this package here but they are unavailable due to this invariant mode so we have to set this to false try it out yourself please feel free to do that and then you will see the error you get but now everything should work just fine and I suggest we just reset our application now and we see something else is happening not only the I don't know four or five infos in the lock see it here Hang Fire background job server using the following options for Hang Fire Work account we have a shutdown timeout we have a scheduled polling interval and so on here are the background server processes yeah so it seems to work right and now when we go back to the browser there it is actually here simply say Hang Fire and there's all my hangfire dashboard isn't that nice and here you can see the drops and here then we will see the schedule drops for instance succeeded processing failed and so on so I'd say let's try that first things first we need a new model where we add our date time value so the date and the time where we want to publish our tweet because here when I would just add it here then the Twitter API won't be able to work with that so we just copy and paste this in essence and add the schedule for property no it works okay so this is a copy and let's just call this now post scheduled tweet Maybe post scheduled tweet and here now we add another property which is of type this one here date time schedule four for instance all right and with that we get our dates and with that now also we can go back to the controller and add another endpoint here so now this is also a post or the post method is used but regarding the routes we call this schedule for instance not using or doing anything asynchronous in this method so this is simply public I action result is returned scheduled to read is the name I like for that method and here again we're using the post scheduled tweet request dto and this is again a new tweet all right now how do we get the the correct date and time well in Hang Fire we have the method to delay a certain job all right or give a scheduled job a time span another date or time all right so we have to first calculate the actual delay for that we just say VAR delay not delay delay is new tweet schedule 4 minus date time UTC now all right with that you can see it here this is now a time span and next we check if the delay is greater than zero so if we actually requested or edit provided a proper date and time value so if delay every time they lie why do lie it's delay greater than time span zero only then we say background job and then schedule all right and then in the Lambda expression we say post Tweet this is the method we already got new tweet okay and now comes map star where we use the beautiful Adept method to well map the DDOS and this shell now only be a post Tweet a request in dto like that and here now enqueue adds we add our delay it is not the delay and when this is done we simply return OK and then here for instance tweet schedules awesome huh and if it's not zero then we return a bad request saying not new tweets but we're saying here please enter well it's date and time something like that all right one more thing since we are calling this method in essence here in our scheduler we can change the automatic retries on fail right so by default I think Hank fire is trying to uh schedule or run the the method here the post method post Tweet method 10 more times but let's just say we reduce that for instance to automatic retry attempts zero okay so if it doesn't work it doesn't work and that's it and now we can already test that so restart the application there we are we've got our new endpoint here schedule it's called let's just try this and here we say well this is a scheduled tweet posted while recording another YouTube video and I like this hashtag here and this is all most the correct date and time let's say we make this a seven and we hit execute to be scheduled this is nice we can go to the schedule here you already see it right in 10 in two minutes we will see this thing now of course I will edit this video so you do not have to wait for two minutes now it says in a minute now it says in one minute in a few seconds and a few seconds ago isn't it crazy so we can have a look here what is actually going on so this is the the body here right so this is the text that we want to send and it did already send this state succeeded Isn't it nice took 516 milliseconds all right and here you can also see succeeded this is what uh we scheduled this job here and now it is finished and we now have a look again at my profile we see this is a scheduled tweet posted by recording another YouTube video isn't that nice so this just like that already works but there's one more thing I want to show you how about scheduling a complete list of tweets so for instance you want to work with your social media Brands I don't know the content and you just want to sit down one day prepare a couple of tweets and then send them with one request to your Twitter scheduler well for that we need something else we need a new model where we have a list of tweets and then another endpoint where we have this list and then with the for each we go through this so let's do that as well again next step a new model so right click add a new item and now let's call this beautiful name post scheduled tweet a list or request in detail post to schedule to tweet list request video This is this is a great name just one property a list all right so a list of post get yields tweet request dto and these are now the tweets and let's initialize that thing with a new list there it is all right and now already the end point so here in the Tweet controller that or the treats controller again HTTP posts and it should be post it is but this time let's call this bulk all right so we can just send a big list of tweets too our web API again public I action results schedule two weeks now not just one tweet tweets post scheduled yep that's the one and let's call this now maybe just requests all right because it's not only one new tweet a couple of tweets so what I want to do now here still is I want to check if date time the date time value is correct so what I uh would suggest is to create only one for each we go through everything if a tweet is invalid because of the date and time value then we add these to the list and then we also count the tweets that have been scheduled and in the end we return just one message that is telling us okay I have scheduled five tweets for instance out of these 10 that you sent me and the other five are invalid something like that all right so first the invalid ones so if bar invalid tweets is new lists off posts that's the one okay and then simply scheduled tweets is zero then a four each where we say for each tweet in the request tweets we say again the time span so time span delay Jesus is sweet schedule four date time UTC now by the way again I have my baby in the baby sling here so if you're hearing some baby noises well that's because of her sorry about that then if the time span is small or equal to zero then we say invalid tweets add tweet and we just continue but if everything is correct then we say a background job can you and then again the Lambda expression post tweet tweet adapts to uh posts schedule tweet request not scheduled post Tweet request dto like that and here it is the delay all right and you know we say scheduled tweets plus plus and this is the first time I'm actually seeing this sticky header here is this new in Visual Studio 17.8.0 preview 2. did not see I haven't seen that before maybe I missed something please tell me have you seen that before for me that's that's pretty new all right so schedule tweets plus plus that's it and now when the 4-H is done then we create a message so string message and if now we've got any invalid tweets so invalid tweets any all right then we say our message is scheduled tweets tweets scheduled successfully for instance and then we also add I know we could use some other syntax here but I'm still used to that one sorry about that inverted tweets count tweets had invalid dates and we're not we're not scheduled all right something like that and in the other case we just say message is something like all scheduled tweets tweets get healed successful and actually schedule tweets is not the best name let's rename this to scheduled count maybe yeah I think that's better alright and in the end we return okay with that message great so again HTTP post bulk the routes we get the list or use the list dto we have invalid tweets a scheduled count for the successful tweets we check if the date is correct if it is or if it is not then we add this tweeted to our invalid tweets list then if date and time is correct again we schedule this thing we increase our counter and in the end when the for each is through we create our message here saying either so and so tweets scheduled successfully but so and so tweets had inverted dates and were not scheduled or otherwise all so and so tweets scheduled successfully and return that alright that's it let's try it so our app is still running we have no scheduled tweets but one succeeded one succeeded one so yeah okay and our new endpoint bulk all right and here you can see now this is an array so let me just let's just try it three maybe all right so here now scheduled tweet number one and copy that schedule tweet number two schedule tweet number three now here I would say I make this now 15 or three o'clock P.M and this just one minute later and this now just means it's in the past so this should be invalid all right so let's just execute this and we see two tweets successfully a scheduled success successfully one tweet tweet tweets all right add invalidates and we're not scheduled semicolon right we can replace this with a period of course and here we see we now have our two scheduled tweets and just like that I refresh the page and we see no tweets are scheduled but we've got three succeeded and again when we refresh my profile we see is scheduled tweet number one and scheduled tweet number two isn't that nice so this is how it is done scheduling tweets with tweet envy and Hang Fire and of course if you wanna have more features please tell me that I would like to actually build a.net8 Blazer client for this thing so where you can really manage your scheduled tweets because when you have a look here now we just refresh the database and you see the tables here right we've got jobs job parameters and so on do not really have to access these tables but I think it would be interesting to create another table where we put our scheduled tweets so we can then see uh what's the text what is the date and time of a tweet so we have an overview by ourselves maybe we can I don't know add templates for tweets something like that so it is even easier for us to create tweets schedule them you know so that something is going on on our profile wouldn't that be nice so these are just some ideas if you have more ideas for this application some features again please tell me that in the comments below and guys if you liked this tutorial here please hit the like button smash it if you want to please subscribe to my channel it really does make a difference means the world to me and again thank you so much to all my patrons supporting me for my work with your help I am able to create more and more tutorials like this one here so I love you guys forever thank you very much and now I can just say thank you again for watching and I hope I see you next time take care
Info
Channel: Patrick God
Views: 2,100
Rating: undefined out of 5
Keywords:
Id: nnNbY5fX7eA
Channel Id: undefined
Length: 30min 25sec (1825 seconds)
Published: Tue Sep 26 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.