Have you ever found yourself wanting to share a trending topic hashtag in your slack channel? Or maybe the latest derby match tweets? Now you can! In our examples repository, you can find a chatbot example that does exactly this.

As seen in the image above, you can ask the bot whatever you want to search for and the “Twitter to Slack” chatbot will automatically find the tweets that match your search terms in Twitter and post them in the Slack channel. This bot example retrieves the first 15 results that match your search terms. But our  Xatkit Twitter platform offers other predefined actions (e.g. post direct messages) that you could use to create many other exciting bots.

Let’s take a quick look at this Twitter-to-Slack chatbot. As all Xatkit bots, the bot is defined via two main files: a file to define the intents the bot should match and a file to tell Xatkit what to do in response of each matched intent.

For this bot, we just have one main intent: asking the chatbot to look for tweets for a specific subject

intent SearchTweets{
	inputs{
		"Can you show tweets about TWEETS"
		"Can you search for TWEETS"
 
		“Search for tweets TWEETS”
		“Search for tweets of TWEETS”
		"Search tweets with TWEETS"
 
		“Look for TWEETS”
		"Look for tweets TWEETS"
 
		"Find TWEETS"
		"Find tweets TWEETS"
	}
	creates context Tweets {
		sets parameter query from fragment "TWEETS" (entity any)
	}
}

The intent SearchTweets will be matched any time we write in the Slack channel the bot is listening to a sentence like the ones listed in the training inputs. For visualizing and storing the Tweets to search context information there are 2 ways, the one showed in this blog the one shown here, relying on the create context reserved word. Within this context, we define the parameter “query” that will contain the text that matches the fragment “TWEETS” in our training inputs.

Then, we just need to set in the execution file that every time we match a  SearchTweets intent we should ask Twitter to return the results for the query parameter saved in the matching process and post a message in Slack with the resulting tweets:

on intent SearchTweets do
	def contentQuery = context(Tweets).get("query") 
	if(contentQuery != ""){
		def result = action TwitterPlatform.LookForTweets(query : contentQuery)
		if(result == "1"){
			action SlackPlatform.Reply(message : "I couldn't do that search")
		}else{
			if(result == "0"){
				action SlackPlatform.Reply(message : "No results for: _" + contentQuery + "_")
			}else{
				action SlackPlatform.ReplyAttachmentsMessage(attachments : result)
			}
		}		
	}else{
		action SlackPlatform.Reply(message : "I can't search for that")
	}

The bot will show you the tweets if there are matches, or it will inform you if there aren’t. In addition, if the search term isn’t suitable it will let you know that the chatbot can’t search for that.

Note how thanks to our predefined Slack and Twitter support, the actual code is rather simple leaving the complexity of the interaction with those platforms on us.

This is another example of how you can combine different platforms and develop interesting chatbots, the only limit now is your imagination! If you create any Xatkit bot let us know! Who knows, maybe the next example featured here could be yours!

Pin It on Pinterest

Share This