Examples Delphi

Title: Make AIM(AOL Instant Messenger) Messenger Robot by yourself.
Question: How can I make a AIM(AOL Instant Messenger) Messenger Robot?
Answer:
unit AIMRobotDemoUnit;
interface
uses
Windows, Messages, SysUtils,Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ExtCtrls, BSAIMMessenger;
type
TfrmAIM = class(TForm)
Panel1: TPanel;
GroupBox1: TGroupBox;
Label1: TLabel;
Label2: TLabel;
edtAccount: TEdit;
edtPwd: TEdit;
btnLogin: TButton;
btnLogout: TButton;
lvBuddies: TListView;
btnDeleteBuddy: TButton;
btnAddBuddy: TButton;
GroupBox2: TGroupBox;
memLog: TMemo;
Panel2: TPanel;
Label4: TLabel;
edtMessage: TEdit;
btnSendMessage: TButton;
StatusBar1: TStatusBar;
AIM: TBSAIMMessenger;
Timer1: TTimer;
procedure btnLoginClick(Sender: TObject);
procedure AIMLoginStatus(Sender: TObject; const Status: string);
procedure AIMBuddyList(Sender: TObject);
procedure AIMLogout(Sender: TObject);
procedure AIMError(Sender: TObject; ErrorCode: Integer;
ErrorMessage: string);
procedure btnLogoutClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure AIMBuddyStatusChanged(Sender: TObject; Buddy: string;
const Status: TAIMStatus);
procedure AIMMessage(Sender: TObject; const Buddy, Message: string);
procedure Timer1Timer(Sender: TObject);
procedure btnAddBuddyClick(Sender: TObject);
procedure btnDeleteBuddyClick(Sender: TObject);
procedure btnSendMessageClick(Sender: TObject);
procedure AIMLogin(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmAIM: TfrmAIM;
implementation
{$R *.dfm}
procedure TfrmAIM.btnLoginClick(Sender: TObject);
begin
AIM.AIMID := edtAccount.Text;
AIM.AIMPassword := edtPwd.Text;
AIM.Login;
end;
procedure TfrmAIM.AIMLoginStatus(Sender: TObject; const Status: string);
begin
StatusBar1.Panels[0].Text := Status;
end;
procedure TfrmAIM.AIMBuddyList(Sender: TObject);
var
i: integer;
item: TListItem;
Buddy: TAIMBuddy;
Group: TAIMGroup;
begin
lvBuddies.Items.Clear;
for i := 0 to AIM.Buddies.Count - 1 do
begin
Buddy := AIM.Buddies.ParamByIndex(i).AsObject as TAIMBuddy;
item := lvBuddies.Items.Add;
item.Caption := Buddy.Account;
if AIM.Groups.ParamByNameExisted(IntToStr(Buddy.GroupID)) then
begin
Group := AIM.Groups.ParamByName(IntToStr(Buddy.GroupID)).AsObject as TAIMGroup;
item.SubItems.Add(Group.GroupName);
end
else
item.SubItems.Add('');
item.SubItems.Add(AIMStatusToStr(Buddy.Status));
if i = 0 then
item.Selected := true;
end;
end;
procedure TfrmAIM.AIMLogout(Sender: TObject);
begin
lvBuddies.Items.Clear;
StatusBar1.Panels[0].Text := 'Logout';
end;
procedure TfrmAIM.AIMError(Sender: TObject; ErrorCode: Integer;
ErrorMessage: string);
begin
StatusBar1.Panels[0].Text := 'ErrorCode: 0x' + IntToHex(ErrorCode, 4);
end;
procedure TfrmAIM.btnLogoutClick(Sender: TObject);
begin
AIM.Logout;
end;
procedure TfrmAIM.FormClose(Sender: TObject; var Action: TCloseAction);
begin
AIM.Logout;
end;
procedure TfrmAIM.AIMBuddyStatusChanged(Sender: TObject; Buddy: string;
const Status: TAIMStatus);
var
i: integer;
begin
for i := 0 to lvBuddies.Items.Count - 1 do
if (lvBuddies.Items[i].Caption = Buddy) then
begin
lvBuddies.Items[i].SubItems[1] := AIMStatusToStr(Status);
end;
end;
procedure TfrmAIM.AIMMessage(Sender: TObject; const Buddy,
Message: string);
begin
memLog.Lines.Add(Buddy + ' Said: ' + Message)
end;
procedure TfrmAIM.Timer1Timer(Sender: TObject);
begin
{if AIM.Logined then
AIM.SendKeepAlive; }
end;
procedure TfrmAIM.btnAddBuddyClick(Sender: TObject);
var
b: string;
begin
if aim.Logined and InputQuery('Add buddy...', 'AIM ID:', b) then
begin
btnAddBuddy.Enabled := false;
try
aim.AddBuddy(b);
finally
btnAddBuddy.Enabled := true;
end;
end;
end;
procedure TfrmAIM.btnDeleteBuddyClick(Sender: TObject);
var
i: integer;
begin
if aim.Logined and (lvBuddies.SelCount 0) then
begin
btnDeleteBuddy.Enabled := false;
try
for i := 0 to lvBuddies.Items.Count - 1 do
if lvBuddies.Items[i].Selected then
aim.DeleteBuddy(lvBuddies.Items[i].Caption, lvBuddies.Items[i].SubItems[0]);
finally
btnDeleteBuddy.Enabled := true;
end;
end;
end;
procedure TfrmAIM.btnSendMessageClick(Sender: TObject);
var
i: integer;
begin
if aim.Logined and (lvBuddies.SelCount 0) then
begin
btnSendMessage.Enabled := false;
try
for i := 0 to lvBuddies.Items.Count - 1 do
if lvBuddies.Items[i].Selected then
aim.SendMessage(lvBuddies.Items[i].Caption, edtMessage.Text);
finally
btnSendMessage.Enabled := true;
end;
end;
end;
procedure TfrmAIM.AIMLogin(Sender: TObject);
begin
ShowMessage('OK');
end;
end.