Examples Delphi

Title: Make YAHOO! Messenger Robot by yourself.
Question: How can I make a YAHOO! Messenger Robot?
Answer:
unit YahooRobotDemoUnit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ComCtrls, BSYAHOOGlobal, BSYAHOOMessenger;
type
TForm1 = class(TForm)
Panel1: TPanel;
GroupBox1: TGroupBox;
Label1: TLabel;
edtYahooAccount: TEdit;
Label2: TLabel;
edtYahooPwd: TEdit;
ProgressBar1: TProgressBar;
Label3: TLabel;
btnLogin: TButton;
btnLogout: TButton;
StatusBar1: TStatusBar;
GroupBox2: TGroupBox;
Panel2: TPanel;
Memo1: TMemo;
Label4: TLabel;
edtMessage: TEdit;
Button1: TButton;
ym: TBSYAHOOMessenger;
lvBuddies: TListView;
Button2: TButton;
Button3: TButton;
procedure btnLoginClick(Sender: TObject);
procedure ymLoginProgress(Sender: TObject; const Progress: Integer);
procedure ymLoginStatus(Sender: TObject; const Status: string);
procedure ymMessage(Sender: TObject; const Buddy, Message: string);
procedure ymBuddyTyping(Sender: TObject; const Buddy: string);
procedure Button1Click(Sender: TObject);
procedure ymLogout(Sender: TObject);
procedure ymBuddyList(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure btnLogoutClick(Sender: TObject);
procedure ymBuddyAddYouRequest(Sender: TObject; const Buddy: string;
RequestMessage: string);
procedure ymBuddyAddedYou(Sender: TObject; const Buddy: string);
procedure ymDeletedBuddy(Sender: TObject; const Buddy, Group: string);
procedure Button3Click(Sender: TObject);
procedure ymBuddyStatusChanged(Sender: TObject; const Buddy: string;
const Status: Integer);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses ParamsUnit;
{$R *.dfm}
procedure TForm1.btnLoginClick(Sender: TObject);
begin
if not ym.Logined then
begin
ym.YAHOOID := edtYahooAccount.Text;
ym.YAHOOPassword := edtYahooPwd.Text;
ym.Login;
end;
end;
procedure TForm1.ymLoginProgress(Sender: TObject; const Progress: Integer);
begin
ProgressBar1.Position := Progress;
end;
procedure TForm1.ymLoginStatus(Sender: TObject; const Status: string);
begin
StatusBar1.Panels[0].Text := Status;
end;
procedure TForm1.ymMessage(Sender: TObject; const Buddy, Message: string);
begin
Memo1.Lines.Add('From ' + Buddy + ': ' + Message);
end;
procedure TForm1.ymBuddyTyping(Sender: TObject; const Buddy: string);
begin
StatusBar1.Panels[0].Text := Buddy + ' Typing...';
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
if lvBuddies.SelCount begin
MessageDlg('Please select one or mutli buddy in buddy list view at first.', mtWarning, [mbOK], 0);
exit;
end;
for i := 0 to lvBuddies.Items.Count - 1 do
if lvBuddies.Items[i].Selected then
ym.SendMessage(lvBuddies.Items[i].Caption, edtMessage.Text);
end;
procedure TForm1.ymLogout(Sender: TObject);
begin
StatusBar1.Panels[0].Text := 'Logout';
lvBuddies.Items.Clear;
end;
procedure TForm1.ymBuddyList(Sender: TObject);
var
i: integer;
item: TListItem;
bud: TYAHOOBuddy;
begin
lvBuddies.Items.Clear;
for i := 0 to ym.Buddies.Count - 1 do
begin
item := lvBuddies.Items.Add;
bud := TYAHOOBuddy(ym.Buddies.ParamByName(ym.Buddies[i]).AsObject);
item.Caption := bud.ID;
item.SubItems.Add(bud.Group);
item.SubItems.Add(YAHOOStatusAsString(bud.Status));
end;
if lvBuddies.Items.Count 0 then
lvBuddies.Selected := lvBuddies.Items[0];
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i: integer;
begin
if ym.Logined and (lvBuddies.SelCount begin
MessageDlg('Please select one or mutli buddy in buddy list view at first.', mtWarning, [mbOK], 0);
exit;
end;
if MessageDlg('Are you sure delete?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
ym.DeleteBuddy(lvBuddies.Selected.Caption, lvBuddies.Selected.SubItems[0]);
end;
procedure TForm1.btnLogoutClick(Sender: TObject);
begin
ym.Logout;
end;
procedure TForm1.ymBuddyAddYouRequest(Sender: TObject; const Buddy: string;
RequestMessage: string);
begin
ym.AddBuddy(Buddy, 'TestGroup', 'I am Bricksoft YAHOO robot.');
end;
procedure TForm1.ymBuddyAddedYou(Sender: TObject; const Buddy: string);
var
item: TListItem;
bud: TYAHOOBuddy;
i: integer;
bExist: boolean;
begin
if ym.Buddies.ParamByNameExisted(Buddy) then
begin
bExist := false;
bud := TYAHOOBuddy(ym.Buddies.ParamByName(Buddy).AsObject);
for i := 0 to lvBuddies.Items.Count - 1 do
if lvBuddies.Items[i].Caption = Buddy then
begin
lvBuddies.Items[i].SubItems[1] := YAHOOStatusAsString(bud.Status);
bExist := true;
end;
if not bExist then
begin
item := lvBuddies.Items.Add;
item.Caption := bud.ID;
item.SubItems.Add(bud.Group);
item.SubItems.Add(YAHOOStatusAsString(bud.Status));
end;
end;
end;
procedure TForm1.ymDeletedBuddy(Sender: TObject; const Buddy,
Group: string);
var
i: integer;
begin
for i := 0 to lvBuddies.Items.Count - 1 do
if (CompareText(Buddy, lvBuddies.Items[i].Caption) = 0)
and (CompareText(Group, lvBuddies.Items[i].SubItems[0]) = 0) then
begin
lvBuddies.Items.Delete(i);
break;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
b: string;
begin
if ym.Logined and InputQuery('Add buddy...', 'YAHOO ID:', b) then
ym.AddBuddy(b);
end;
procedure TForm1.ymBuddyStatusChanged(Sender: TObject; const Buddy: string;
const Status: Integer);
var
i: integer;
bud: TYAHOOBuddy;
begin
for i := 0 to lvBuddies.Items.Count - 1 do
if CompareText(Buddy, lvBuddies.Items[i].Caption) = 0 then
begin
bud := TYAHOOBuddy(ym.Buddies.ParamByName(Buddy).AsObject);
lvBuddies.Items[i].SubItems[1] := YAHOOStatusAsString(bud.Status);
end;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
ym.ChangeStatus(YAHOO_STATUS_BUSY);
end;
end.