Chatbot development (telegram + youtube)

Why did it even appear here?



I recently wrote an article about php bot development using laravel + botman for telegram. The very first thing that I wrote, I quote, is “shooting from a cannon at sparrows”. I agree with this, I even realized it during development, but I was interested in laravel.



image






Now I was developing a bot on pure php (without strapping) and the telegram API, and since there are a lot of articles on a similar topic, we’ll touch the google API (youtube api) a bit. I hope you find it interesting and useful to learn about the youtube API.



A small digression. At the moment, I work as a frontend programmer and work with php only for my own pleasure (although what a pleasure it is). There may be stupid errors on the server side, but it works and that is enough for me at the time of training.



Formulation of the problem



One evening I got the idea to connect the bot and youtube, at that time I did not even suspect the existence of api google. It requires a bot that, when a new video is released on the youtube channel, will push notification in my bot.



In principle, the functionality is quite simple, but how to work with youtube? It turns out that google itself has a solution to this problem and its name is google API. It allows you to work with all applications from the company, but I was only interested in youtube.



Customization and Development



It so happened that during the study of youtube api, an elegant solution to my problem was found, it was offered by the api documentation itself. But first about the api. To start working with it you need to go to the google console and select the api option you need. After that, the path lies only in the documentation or on stackoverflow. I must say right away that there is no Russian version of the documentation, there are no lessons either. It is advisable to have minimal knowledge of the English language or to act by typing.



It looks like this. You go into the google console, create your project and select the desired API.



image



And now to solve the problem. While reading the documentation, I found (google myself showed) the pubsubhubbub.appspot.com/subscribe service , it allows you to connect youtube and your application. How does it work? It works on the principle of webhook. You insert the youtube of the channel that you want to listen to and each time with any actions on the channel (add video, delete, change), receive data to your script.



image



Callback URL - a script that will receive data from google.

Topic URL is the channel you want to listen to.



Well, the Mode field allows you to choose what you want, subscribe or unsubscribe from listening.



Let's start writing the code, for a start, let's set up our script for working with youtube. I wrote everything in one file since it was a test + code there really is not enough. After you submit a subscription request, you need to confirm it. Googling, you can find the answer on everyone’s favorite site, one of the comments on stackoverflow suggests.



$video = "null"; if (isset($_GET['hub_challenge'])) { echo $_REQUEST['hub_challenge']; } else { $video = parseYoutubeUpdate(file_get_contents('php://input')); } function parseYoutubeUpdate($data) { $xml = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA); $video_id = substr((string)$xml->entry->id, 9); $channel_id = substr((string)$xml->entry->author->uri, 32); $published = (string)$xml->entry->published; return array( 'video_id'=>$video_id, 'channel_id'=>$channel_id, 'published'=>$published ); }
      
      





Here we create for convenience a variable in which we will need the info about the video. The next two lines allow you to determine whether it is possible to confirm the request from pubsubhubbub, if so, then confirm, if not, it means that the data came from youtube and we need to process it. In the parseYoutubeUpdate function, we process the response, we get all the information we need.



Here it’s worth digressing and telling about a special (in my opinion, I can be mistaken) form of data used by youtube (for answers) it is called, as I understand it, atom (not to be confused with the IDE or this is a regular xml ... there was little experience with it) . It looks something like this (a small piece of the answer from youtube):



 <id>yt:video:eE5mpblYpdY</id> <yt:videoId>eE5mpblYpdY</yt:videoId> <yt:channelId>UCGqKr3O5ub-O7zEKx_qeHUQ</yt:channelId> <title>20b3560a49 1080</title> <link rel="alternate" href="https://www.youtube.com/watch?v=hgCQ378xNus"/> <author> <name>not Epic</name> <uri>https://www.youtube.com/channel/UCGqKr3O5ub-O7zEKx_qeHUQ</uri> </author> <published>2019-07-14T05:10:49+00:00</published> <updated>2019-07-14T05:11:07.600177664+00:00</updated>
      
      





Back to parseYoutubeUpdate ().



In a function, we convert the response to xml format.

We write the necessary data to the variables.

And then we return an array with this data.



We pass to telegram. For starters, I want to say, this ****** is easier to work than with botman. If you want to develop bots only for telegram, then do not use libraries that allow you to develop bots for different platforms .... it makes no sense ... there will be more problems. The telegram API is very clear and simple even for a beginner who has just met php and has minimal OOP knowledge.



So, closer to the point, closer to the code. We need to create a bot and bind it to our script. I hope you can create a bot. We write a token from the bot and create a request



 const TOKEN = "6826815*******Yme99*****9kjzgVi*****3S******"; $url = 'https://api.telegram.org/bot' . TOKEN . '/sendMessage';
      
      





Now we only need to send a request to the telegram server.



 $params = [ 'chat_id' =>712531723, "text" => $linkVideo, ]; $url = $url . '?' . http_build_query($params);
      
      





We create the necessary parameters, for me it’s a chat id (if it displays a bot on production, then it is necessary to save all id in the database to spam) and a text message.



As you may have noticed, I pass the link as a simple message, not a video. As I understand it, telegram transmits video only if it is uploaded to the server, otherwise not ... + I need to see a preview picture for the video.



Now our bot will receive notifications of exit / video editing. It looks something like this:



image



Game over I have it all. The biggest drawback of this code is that it is in a single file, but it didn't make sense for me to split the 10-line code.



Sources - github .



All Articles