SteemDM 0.2.0: say hello to the new Telegram bot !

in #utopian-io6 years ago (edited)

header

Click on the links below to test the available bots.
Telegram | Twitter

Just like last time, I've been working for about two weeks on the new big addition to SteemDM. This thing I've been working on is a Telegram bot that's been added to the project. If you are a Twitter user of the app, this update doesn't really change anything for you (some bug fixing, a new way of splitting text and an improved Markdown parser). I said in the last update post that I planned on making SteemDM available on as many platforms as possible, this new Telegram bot is the first step in that direction. I made this one first because it was requested to me by @kaliangel but more bots will get released later of course ! Since the old project name was related to Twitter (Steem Twitter Bot) I decided to change its name to SteemDM ("DM" meaning "direct messages"). I'm well aware that this name may be confusing for some people who would think that it's a direct messaging system for Steem but it isn't. If you are looking for such a project, I'd suggest taking a look at SteemMessenger instead. To reflect the project name change, the Twitter bot account has been renamed. It is no longer @SteemBot_ but @SteemDM. Telegram made it possible to make some of the commands more interactive as you will see in this post, it's still quite limited but I will improve on it later.

What's new ?

  • Overall

    • New code structure

      A code structure change was necessary for the project to add new bots easily in the future. I've been working on that a lot and it's now ready, there is nothing to show really since everything changed, which makes it hard to pinpoint something specific to talk about. I can however describe how the old process worked as well as the new process. The old process was pretty simple. The bot and all its main functions (that were able to send messages) were sitting in the app.js file, since only one bot existed, it was working fine. However, with another bot added to the project, I couldn't keep going like that since it would make the code very repetitive. Therefore, I decided to use the app.js file only to execute each bot script, it now contains just three lines of code. Each bot is in its own file and requires an instance of the handler object, this object is what's used to get data from Steem and to return it as a promise. In other words, it's used to get the result of a command typed by the user.
    • "Smart" text splitting

      Any website or app that has direct messages available sets a maximum message length. For Twitter, it's at 10000 characters while it's at 4096 for Telegram. The bot already took that into account when sending the results of a command as a message but it was just splitting the text every 10000th character, no matter what the context around this character was. This approach caused a lot of problems. Some links and special characters were split in half and the text was sometimes hard to read (nobody likes to read a word that is split in half !). The bot now is a bit smarter and will only split the text if the character is a whitespace character (space, new line...), else it will backtrack until it comes across one.
    • Better Markdown parser

      Just like the app itself, the Markdown parser has benefited from a better code structure too. It's now an object containing four keys. The first key is convertHTML, it is associated to a function that converts HTML to Markdown. The second key is parse, it is associated to a function that runs a parsing routine that depends on the kind of website or app used. The third key is parser which is associated to an object containing several functions to parse some text. The two functions below come from that object.
      parser.style is only called for direct messages that have some kind of support for Markdown. Since several flavors exist, this function transforms the text in the flavor used by Steem apps into the correct flavor. Telegram for example uses only one asterisk for bold and one underscore for italic. parser.url is called at the beginning of every parsing routine and transforms ambiguous characters contained in links into their UTF-8 references. The fourth key is replacer which may soon become useless so I won't bother explaining it.
    • Versions

      It's something I totally forgot to do until now, the project now keeps up to date version indications. The numbers used to indicate the version follow a pattern specific to SteemDM and inspired by the classic MAJOR.MINOR.PATCH pattern. The specific pattern used is MAJOR.BOT.COMMAND-FUNCTION.PATCH (with the 'PATCH' part only being shown on the GitHub page of the project). 'MAJOR' will be incremented when big events happen like adding SteemConnect V2 to the project or getting to a substantial amount of bots available. 'BOT' will be incremented for each new fully operational bot. 'COMMAND-FUNCTION' will be incremented for each new command or important function. 'PATCH' will be incremented for each significant command/function improvement. The full version indicator is currently 0.2.0.0 since the project now has two fully operational bots available.
  • Telegram

    • Operational bot

      The Telegram bot should be completely operational, you can interact with it by clicking on the link at the very beginning of this post. It hasn't been tested in group chats yet though and I frankly don't believe it will work like that for now but a support for group chats should be added soon. All the commands that are available for the Twitter bot are available for this one too. You can see below the code behind one of the simplest command.
      This code corresponds to the basic logic on top of which most of the other commands build. The app splits the command received from the user in multiple parts just like for the Twitter bot. Then, it sends the useful informations about that command to the correct function in the handler object and waits for the returned promise to resolve or to reject an error. If everything went well, the result is sent to the user. Otherwise, it's the error that's sent to the user. Sending the error is mainly meant to indicate to the user that something went wrong and that he shouldn't wait for a result. I encourage you to send me informations about any error you receive (except SteemData related ones) so that I can try to fix it for everyone.
    • Interactive buttons

      Since Telegram allows for buttons to be added at the end of sent messages, I decided to make use of that as much as possible with the already available commands. To better understand how those buttons work, let's take a look at the 'mentions' command for example. Here is the piece of code that calls handler.mentions and works with the received data.
      As you can see, the basic logic is the same as the one shown above. However, handler.mentions doesn't return a string but an object containing requested data as a string, as an array and as received (raw). The code that you see creates a button for each element of the array in the received object, it then sends a message to the user with the list of generated buttons attached to it. Once a button is clicked, it sends some data defined beforehand, in this case it is the index of the associated post. The following code catches any sent data that is a number.
      This piece of code is pretty classic. It calls the handler.open function and then splits the result into parts of authorized length. Then, it calls sendPart
      This function is called for any post that's about to get opened. It calls itself recursively (and makes sure messages are sent in the right order) until there is only one part of the text left. At that time, it creates an array containing a 'close' button. It also adds the 'previous' and 'next' buttons to that array if they are of any use. Then, it simply sends a message with the buttons attached. Everything that is available through the means of buttons is also available through text commands like the ones used by the Twitter bot. The 'set' command also has some neat buttons but I will let you discover them by yourself because this post is getting a bit too long.

Contributions

If you want to contribute to this project or talk about an issue it has, feel free to visit its GitHub page. You can also clone it and follow the instructions wrote there to get it running. My social medias are listed at the end of the README.md file. If you add me on Steam, tell me the reason why on my wall, otherwise I won't accept your friend request.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @ragepeanut I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Congratulations! Your post has been selected as a daily Steemit truffle! It is listed on rank 1 of all contributions awarded today. You can find the TOP DAILY TRUFFLE PICKS HERE.

I upvoted your contribution because to my mind your post is at least 17 SBD worth and should receive 122 votes. It's now up to the lovely Steemit community to make this come true.

I am TrufflePig, an Artificial Intelligence Bot that helps minnows and content curators using Machine Learning. If you are curious how I select content, you can find an explanation here!

Have a nice day and sincerely yours,
trufflepig
TrufflePig

Coin Marketplace

STEEM 0.29
TRX 0.12
JST 0.033
BTC 62937.86
ETH 3092.40
USDT 1.00
SBD 3.87