curse_24x24.jpg Curse users click here! | Frequently Asked Questions | Step-by-Step Installation Instructions | In-Game Help | Known Issues | Extending TweetCraft

Extending TweetCraft Overview

Adding additional AutoTweet sources is easy! If you have an event that you want to let the whole world know about, all you have to do is follow these two simple steps and you'll be able to queue up an AutoTweet in response to anything you want.

Note: This article assumes you know a bit about addon programming for World of Warcraft. If you're a complete newbie, check out some of the great resources around the web like the WoW UI Forums and the great World of Warcraft Programming book and their web site.

Stand Alone AutoTweet Addons

The first kind of AutoTweet addon you can create is a stand alone add on whose sole purpose is to send AutoTweets. This is a great way to get started sending AutoTweets. You can download the sample addon we'll create here in the Downloads section and you can also get a template addon that has a TOC file with the right dependencies set and a LUA file with some stock code to get you started. You can get the files from TweetCraft 1.0.

For our example, we'll be writing a mod that sends an autotweet at the end of a battleground match indicating both wins and losses. You can start with the completed addon and follow along or grab the template and code it up yourself as we go.

Step 1 - Register your addon with TweetCraft

The first (and probably easiest) part of writing your mod is to register yourself with TweetCraft. Open up your LUA file and take a look at the first few lines:

-- Get a reference to the library
local LibTweetCraft = LibStub:GetLibrary("LibTweetCraft-1.0")

TweetCraft uses a mod called LibStub to load libraries. This first line gets you a handle to LibTweetCraft which is the way you can begin to communicate with TweetCraft.

First, let's tell TweetCraft who we are and what we want to do.

-- Register our autotweet source
local AddOn = LibTweetCraft:RegisterAutoTweetAddOn("Battlegrounds", "Sends tweets related to Battlegrounds")

The RegisterAutoTweetAddOn takes one required parameter and two optional. The first is the name of the addon. This will be used in lots of places but the main one you'll see is the options UI. The second tells is a description of the addon which will be shown as the tooltip for your checkbox in the interface options. Finally, you can also pass a boolean value to indicate if you want the addon to be enabled by default. The user can override your setting but this allows you to turn an addon that may be a bit chatty off unless the user wants it.

This function also returns an important object: your addon. This is the primary way you'll communicate with TweetCraft from now on. You'll customize your registration here and also use it to send tweets. But before we send anything, lets add some categories of autotweets. We'll keep it simple and go with one for winning a battleground and one for losing.

-- Define our autotweet categories
BATTLEGROUNDTWEETS_WIN = "Win"
BATTLEGROUNDTWEETS_LOSE = "Lose"

-- Register our autotweet categories with tweetcraft
AddOn:AddCategory(BATTLEGROUNDTWEETS_WIN, "Win a battleground", "Automatically tweets when you win. Go you!")
AddOn:AddCategory(BATTLEGROUNDTWEETS_LOSE, "Lose a battleground", "Automatically tweets when you lose. Don't hide your shame!")

AddCategory takes 3 required arguments and one optional one.

The first is the name of your category. This is passed to TweetCraft whenever you want to send a tweet. It's never seen by the user but it's very important to your code as it allows TweetCraft to determine if it should send the tweet based on the users options. That's why we've created some constants for it; you'll need them later.

The second is the display name of your category. It's used in the options panel for the label of the checkbox. The third, the description, is also used by the options panel to provide the tooltip. Finally, just like the addon registration, you can pass a boolean to configure if the category should be enabled or disabled by default.

If you stopped here, you'd have everything registered with TweetCraft (even though you're not sending tweets yet). If you load up WoW after you've installed your AddOn, you should see the following in your options panel:

BattlegroundTweets.jpg

Step 2 - Send Tweets

Now comes the tricky part: hooking up to a WoW event and sending tweets in response. First, let's wire up the event

AddOn.EventHandler:RegisterEvent("UPDATE_BATTLEFIELD_STATUS")

TweetCraft provides you with a simple way to register events so you don't have to go to the trouble of creating your own Frame. We'll be looking for the UPDATE_BATTLEFIELD_STATUS event which fires for many reasons but the one we're concerned with is the end of a battleground. To handle this event, we define a function on our AddOn object's EventHandler with the same name as the event:

-- Handle the update event
function AddOn.EventHandler:UPDATE_BATTLEFIELD_STATUS(...)

end


This function will get called every time the event is fired by the game. Now, time to flesh it out. Since this is more about sending autotweets than the finer points of the battlegrounds APIs, I'll just toss the whole thing in for you.

-- Handle the update event
function AddOn.EventHandler:UPDATE_BATTLEFIELD_STATUS(...)
	local winner = GetBattlefieldWinner()
	if winner ~= nil then
		for index=1, MAX_BATTLEFIELD_QUEUES do
			local status, map, _ = GetBattlefieldStatus(index)
			if status == "active" then
				local playerFaction, _ = UnitFactionGroup("player")
				local playerName = UnitName("player")
				if playerFaction == "Alliance" and winner == 1 or playerFaction == "Horde" and winner == 0 then
					-- Send victory tweet
					AddOn:Tweet(BATTLEGROUNDTWEETS_WIN, playerName .. " has won " .. map)
				else
					-- Send loss tweet
					AddOn:Tweet(BATTLEGROUNDTWEETS_LOSE, playerName .. " has lost " .. map)
				end
			end
		end
	end
end


We get the winner of the battleground via GetBattleFieldWinner() it returns 1 for Alliance, 0 for Horde, and nil for "no winner yet". As long as we have a winner, we get the battleground that is currently active (since you can queue for lots at once) and grab the name of the map we're on. We grab the player name and their faction and if they match the winning faction we send a victory tweet and if they don't we send a losing tweet.

The Tweet method takes two parameters. The first is the category of tweet we're sending. This allows TweetCraft to supress the tweet if the user has turned off that category. The second is the message itself. We build that up here using the player's name and the map they won or lost. It's as simple as that!

Now that you've got it working, log in and hit a battleground to mix it up. I did and I have to say it went pretty well. You might even say I got a bit carried away infact. Win or lose, you should see a message to your chat window letting you know there's an autotweet as well as a tweet in your Outgoing window in the main TweetCraft UI.

Congratulations, you've extended TweetCraft!

Have you got a cool addon that you've written? Autotweets for boss kills? Looting epics? Arena matches?

Have questions, suggestions, or bug reports?

Let us know in the comments below!

Last edited Feb 3, 2010 at 8:37 AM by danielfe, version 6

Comments

Donhorn Jul 10, 2009 at 1:44 AM 
Just want to throw out there since it wasn't obvious in the instructions, these extenders need to be placed in the Interface folder, not the TweetCraft folder. You treat it as it's own separate addon.