first attempt at a discord bot with nodejssteemCreated with Sketch.

in palnet •  9 months ago  (edited)

the other day I finally started a discord bot.
You can find it here in my discord test server. I want to add more functionality to it, and looking for suggestions, before I ask others to put it in their rooms.

The actual setting up a bot doesn't take very long, and there are online manuals everywhere. Kind of had to use a mix of two manuals to get it going. The link below is basically the hello world type example I build upon.
I forget the second link, but my use for it was just to learn how to create a discord server. You must create your own "server" on discord, before you can link your own bot to it.

The first thing I wanted to so was read internet data. So I tried to read the drudgereport.
So from the example in the manual you could just do a case 'drudge' and insert code between the case and the switch, or chose a separate selection method. Many of the online samples of how to read websites I find didn't work-they didn't synchronizing correctly as the end event failed to start. I did find one mechanism that worked. Anyways, i since deleted that drudge [hello world] code, but will show a youtube example instead. My next project was create a primative get song on youtube by title just to scrape the first tailored result, and refined it later (it has since been refined).

so anyways, the refined search string is cc, and my final path for the youtube video was named lll. I wanna keep the coding concepts to the point, so I omitted the parsing of how lll came to be.

request.get(''+cc, (error, response, body) => {ll=body;
bot.sendMessage({to: channelID, message: ""+lll});

It's not that difficult. There are many ways to butcher a delimited input string. Personally I like the [string].split method. Since I built upon the basic ping pong file, I continued to use message as the input string.

var aa=message.split(" ");

Just a matter of choice on how you want to deal with the array after that.

My next project was, at the recommendation of chat, to write a bot that charts steem engine tokens. The method to do this was more insidious. But to make things easier, I visit the exchange page, open up development tools, network tab, and search for the price of something in the history, preferably something unique. You get an idea about what pages you have to load from the results, and what you have to send.

Sometimes the search reveals multiple results-when not unique. So inspect each one. You can inspect by right clicking and choose reveal. Then make sure the the data is what you are looking for.

Then you click the headers tag, scroll down and look for request payload. Click view source if it appears.

Then go ahead and paste that into a textfile to use later.

From that information, what I found online to work is to create a string array that established what all will be sent to the request function. As you can see the last line starts with a "json: {" followed by what what was in the payload line and the closing brackets. I did modify the payload line to load up to 200 results from the original 30.

Now if you ask why did I have you go through all that, is so the reader can figure out how to proceed to scrape data from other sites. Some sites may use tokens created from obfuscated code, or one time use tokens, or temporary use to prevent scraping.

var options = {
uri: '',
method: 'POST',
headers: {'Content-Type': 'application/json'},
json: {"jsonrpc":"2.0","id":20,"method":"find","params":{"contract":"market","table":"tradesHistory","query":{"symbol": token.toUpperCase() },"limit":200,"offset":0,"indexes":[{"index":"timestamp","descending":false}]}}};, (error, response, body) => {ll=body;
var kk=JSON.stringify(ll);

It's return data will have to be parsed out. Nothing a few for loops, [string].length, [string].indexOf, [string].substring, and [string].split can't handle.

The next hardest thing was handling the images. It's been a while since I used imagemagick. And to get imagemagick to work right on windows 8 or higher (you may even need imagemagick 6 on windows) you have to play with your system paths so that the path to imagemagick is first. Close the console window, open it again, and hope the command prompt sees convert.exe in your image magick folder. you will also have to npm install the gm package to use image magick. see also

Unfortunately, there seems to only load about 24-25 hours worth of history that can be obtained in the tradesHistory of steem-engine this way. Maybe there will be better results in the future as new api tools are being built. see . The in test alternative doesn't seem to fit my needs yet.

To build charts is high risk-you really don't want anyone on the internet writing to your computer, and you really don't want to allow them to name files for you because they can always send dangerous characters. But alas, I don't see that nodejs/discord will allow the user to pass streams or buffers straight to discord. So the charts have to be build locally (or maybe I could turn on apache and run it through php). Storing a physical copy could spell out problems both in terms of high volume or redundantly speaking security. once the files are saved, they can then be send to discord. One more thing about charts, they are not synchronous. So you have to wait for a file to finish loading before you can send the file, else a previous version may load [such as when I had all charts saved as a specific test file].

notice I told it to load pal, but it loaded the previously stored chart for eng.

There is a fix, and that is to use I guess some built in methods.

gm.write(token+".png", function(err) {if (!err) {console.log('done');bot.uploadFile({ to: channelID, file : token+'.png' });} if (err) {console.log(err);}});

storing things as [token].[format] is pretty bad. What if two people run it at the same time? There is always a difference between making a site for a few users and a site for tens of thousands at a time. Coding basic concepts is easy; making things to scale is often just one of those things small guys don't have much chance or purpose to do.

much better

I've later added a dictionary. Basically the some model I used for youtube, except I tailored it for merriam-webster dictionary.

I am into developing more bots. I am not yet ready to do anything that does block chain, steemconnect, or real world transactions. But if you want something feel free to recommend it, I want to build.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

I adjusted the playlist bot. the ranking system is terrible at youtube; a search for "call me call me"+music produces results for blondie's "call me" and various other false positives required me to stack filter after filter. I added an extra flag to at least search by artist, -[artist], which still ended up promoting blondie over conte. I had to switch to youtube intitle option to try to fix, that and gradually remove the filters that filtered out too much positive content. Ultimate I had to search by views to get rid of most of the garbage results.

The artist flag doesn't have to be an artist, it is just a separate search term not put in quotes.

this morning I decided to see if I could get a telegram bot operating-I just left it as the demo starter version. just went through telegram's bot father, installed the npm package for telegram, then ran a hello world program and gave it a quick run.

to get started you join the botfather channel.
type /start
type /newbot

discover every name you think of has been taken

until you chose something stupid and you get your api/token key.

the npm pacjage for telegram with code example

Be sure to copy and paste the code example from this page, and store it as a file in the folder you are going to work with.

do the npm [file.js] on the command line in your work[ing] folder, and you can play with the bot. The started bot allows you to dm it, and will also respond each time it sees "/echo". Of course you can expand its vocabulary to do other things.

I think it should be possible to have one script interacting with multiple platforms if one script is allowed to control to multiple bot instances, but I digress.

Oh yeah, you also need to create your own room. To create your own room, you have to invite a user. So either invite an image bot, or your own bot. Kind of lame that telegram requires you to invite someone to create a group.

Ultimately you need to invite your bot to your room to play with it.

added a language translator. Yandex offers a free translator api.

Congratulations @firstamendment!
You raised your level and are now a Minnow!

Vote for @Steemitboard as a witness to get one more award and increased upvotes!