From ada9dac04ca03971bff9110451a428c8829d6ebd Mon Sep 17 00:00:00 2001 From: jowj Date: Thu, 25 Jan 2018 15:52:11 -0600 Subject: [PATCH] adding new blogpost, changing title --- README.md | 3 +- blogposts/mojojojo-bot.md | 85 +++++++++++++++++++++++++++++++++++++++ index.md | 3 +- 3 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 blogposts/mojojojo-bot.md diff --git a/README.md b/README.md index 79f17bd..d0f2b95 100644 --- a/README.md +++ b/README.md @@ -1,2 +1 @@ -# jowj.github.io -Public Blog +# me.jowj.net \ No newline at end of file diff --git a/blogposts/mojojojo-bot.md b/blogposts/mojojojo-bot.md new file mode 100644 index 0000000..f92ef53 --- /dev/null +++ b/blogposts/mojojojo-bot.md @@ -0,0 +1,85 @@ +# mojojojo-bot +this was relatively easy to build initially (straight from a template), but building any functionality that I wanted into was, as expected, hard. because i am garbage at python. + +## learnings + +### 1. import pdb (python debugger) + pdb.set_trace + SETS A BREAK POINT IN CODE AND I CAN WRITE CODE THERE AND INTERACT WITH IT HOOOO MY GOD + +this is probably the biggest thing i learned; para exemplar say you have a bit of code that defines a variable eq to individual objects you iterate through one at a time: +``` +events = slack_client.rtm_read() +for event in events: + thing = event.property + thing2 = event.otherproperty + do stuff with thing / thing2 +``` +sometimes (all the time) you run into problems where maybe one of the variables you defined doesn't have the data you expect. so in order to troubleshoot you open you repl and try and do some adhoc definitions of variables and tokens so you can see what the deal is. as you might expect, that's bad and doesn't work very well. but with pdb.set_trace you can define a break point in your code that will give you a prompt, and then you can type "interact" into the prompt and it will let you type python into a repl provided with the state inherent in that point in your code! how fucking cool is that?? + +``` +import pdb +events = slack_client.rtm_read() +for event in events: + thing = event.property + thing2 = event.otherproperty + pdb.set_trace + do stuff with thing / thing2 +``` +now the code looks like this and you can just call dir(events) and view its properties!! + +### 2. accessing data in dict of dicts +python is not powershell python is not powershell python is not powershell python is no + +so in powershell if you're given a dict you can access its indexes using dot params. like +``` +PS /> $dict = @{} +PS /> $dict.Add('firstname','jowj') +PS /> $dict.Add('pet','metroid') +PS /> $dict + +Name Value +---- ----- +pet metroid +firstname jowj + + +PS /> $dict.pet +metroid +PS /> +``` + +in python you cannot do this. in python, in order to pull data for a particular key/value pair, you must index using they key value. +``` +>>> dict = {} +>>> dict +{} +>>> dict = {"firstname" : "jowj", "pet" : "metroid"} +>>> dict +{'firstname': 'jowj', 'pet': 'metroid'} +>>> dict.firstname +Traceback (most recent call last): + File "", line 1, in +AttributeError: 'dict' object has no attribute 'firstname' +>>> dict[0] +Traceback (most recent call last): + File "", line 1, in +KeyError: 0 +>>> dict["firstname"] +'jowj' +>>> +``` + +it took me. so. so. so. long. + +## what's next: +i gotta finish fixing this bot to get everything i want into 1 file (right now i have some of the functionality i want in two different scripts). then maybe add some extra functionality like: +* commanding to my twitter bot +* ...other stuff. + + +## resources: +* basic tutorial / code i stole - https://www.fullstackpython.com/blog/build-first-slack-bot-python.html +* to set an environment variable in powershell in userscope, you gotta go out to .net - [Environment]::SetEnvironmentVariable("SLACK_BOT_TOKEN", "Test value.", "User") +* responding to messages with specific text - http://pfertyk.me/2016/11/automatically-respond-to-slack-messages-containing-specific-text/ +* api docs - https://github.com/slackapi/python-slackclient diff --git a/index.md b/index.md index ba97803..b5a7210 100644 --- a/index.md +++ b/index.md @@ -1,4 +1,5 @@ Josiah Ledbetter I do things with computers and write about them here. -[Hosting Static Sites On S3](/blogposts/hostingStaticSitesOnS3.md) \ No newline at end of file +[hosting static sites on s3](/blogposts/hostingStaticSitesOnS3.md) +[building a slack bot](/blogposts/mojojojo-bot.md) \ No newline at end of file