lendingsilikon.blogg.se

Slack client python bot conversation
Slack client python bot conversation







slack client python bot conversation
  1. SLACK CLIENT PYTHON BOT CONVERSATION INSTALL
  2. SLACK CLIENT PYTHON BOT CONVERSATION GENERATOR
  3. SLACK CLIENT PYTHON BOT CONVERSATION UPDATE

# Messages will get queried by a different auth token Sc.rtm_send_message(channel, "Leveling up.")

SLACK CLIENT PYTHON BOT CONVERSATION UPDATE

Reports back to the channel where the update was requested on status. Queries for new messages and adds them to the 'database' object if new ones are found. Return markovify.Text(" ".join(messages.values()), state_size=2)ĭo any formatting necessary to markon chains before relaying to Slack.Ĭleaned_message = re.sub(r'', '\1', original)

SLACK CLIENT PYTHON BOT CONVERSATION GENERATOR

Read the latest 'database' off disk and build a new markov chain generator model. # get all messages, build a giant text corpus Search through an API response and add all messages to the 'database' dictionary.įor match in new_messages: Return client.api_call('ssages', query=MESSAGE_QUERY, count=MESSAGE_PAGE_SIZE, page=page)ĭef _add_messages(message_db, new_messages): Takes a dictionary keyed by unique message s and writes it to the JSON 'database' onĬonvenience method for querying messages from Slack API. With open('message_db.json', 'w') as json_file: With open('message_db.json', 'r') as json_file: Returns a dictionary keyed by unique message s. Reads 'database' from a JSON file on disk. MESSAGE_QUERY = "from:username_to_parrot" Take a look at chain.py, which pulls messages from the Slack RTM API, builds a markovify model, and stores messages to disk in a JSON file with some simple data access functions. By using Python's built in JSON parsing module, this can be loaded into a dictionary and new messages will be written idempotently (duplicate messages simply overwrite any previous keys) and simple dictionary operations can give you all the details you need about your text corpus ( len(corpus.keys()) is the number of messages, corpus.values() is a list of all text in the corpus, etc). All slack messages have a unique parameter that we can treat as a unique identifier. We'll accomplish that by seeding the model with messages pulled from Slack's API.Ī quick way to store these messages is via JSON. But we'd like something more responsive something that can learn over time continually adding new text to the model. If you had all of the text you wanted to seed the generator with laying around, you could toss it in as a string and get straight to business. For this reason, it is smart to format your query to limit its search to public channels ('from:username in:general', for example) to ensure generated Markov chains do not contain private information. This means when you query the API for messages, it will return anything you have privileges to see, including private messages with your target user. Keep in mind that API calls using this token will be authenticated as you. The easiest way to generate a user token for yourself is through the test token generator. Bots don't get this privilege (the ssages API method), so we'll need to authenticate those requests as a standard user. This token lets us authenticate as the bot, but we're also going to use the Slack API to search for messages within our team. You can create bot users and tokens at Slack's API site. It allows our script to connect to slack as our bot user and post messages on its behalf.

slack client python bot conversation

We'll also need to generate two different authentication tokens for Slack.

SLACK CLIENT PYTHON BOT CONVERSATION INSTALL

Let's Get StartedĬreate a new Python virtual environment and install our two project dependencies: markovify, and slackclient:

slack client python bot conversation

We'll be using a Python library called Markovify that implements these chains, but it's important to understand at least conceptually the math behind it-especially if you want to tune the model. Now, if the math is a little fuzzy to you, don't worry. The chain is terminated when punctuation that terminates the sentence is encountered. This is done by selecting a word (the current state) and then selecting the next word (the next state), based on the probability that word B follows word A in our training data. These chains can be applied in computational linguistics to generate random sentences that mimic the author, given a large corpus of text they have written. Markov chains can be applied to any dataset that has discrete probabilities for a new state, given the current state. The math Markov pioneered is pretty fascinating: check out this page for a very good explanation and visualization of how they work. We're going to do exactly that, and we're going to do it using Markov chains. What if you could program a Slack bot that randomly generates messages that were undeniably Jeff? Maybe you've even coined a term amongst your group: a Jeffism. Everything that Jeff says is patently Jeff.

slack client python bot conversation

Imagine in your company slack team there's this person (we'll call him Jeff). Rather than having this bot respond to simple commands, though, let's do something more. In this post, we're going to explore building a bot for Slack.









Slack client python bot conversation