Page 1 of 1

ViciDial and vTiger v6/v7 integration

PostPosted: Mon Nov 26, 2018 3:03 pm
by josecapurro
Hello there!

In case somebody find it useful, this is a quick script to interact with vTiger version 6 and 7 via Webservice.

For example, now i am using it to create new leads in vTiger for each incoming call. There is no checks if the leads exists or not, nor any kind of authentication, so be adviced to not expose it to the public Internet or any untrusted network.

Requisites:
1. Have a working vTiger version 6 or 7.
2. Have a webserver capable of executing Perl scripts. In my case, i have a ViciBox 8.1.2 webserver.

Installation:
1. Copy the script to the webserver. If you are using ViciBox, copy it to /srv/www/cgi-bin.
2. Edit the script and change the following variables:
1. $username: Username in vTiger.
2. $key: This is the accessKey for the user in vTiger. It is available in the user preferences page, on the section User Advanced Options.
3. $host: URL of the webserver where vTiger is located. IE.: https://vtiger.example.com
4. $endpoint: The path to the vTiger webservice endpoint. IE.: /vtigercrm/webservice.php

ViciDial configuration:

This particular configuration creates a lead in vTiger for each incoming call you dispose.
All this steps must be executed from the ViciDial administration interface.

1. Go to Inbound > Show In-Groups.
2. Open the In-Group you want to configure.
3. Edit the Dispo Call URL. Set it to ALT.
4. Submit the changes.
5. Open again the In-Group you want to configure.
6. Go to Dispo Call URL and click the link Alternate Dispo URL Defined.
7. Configure the Dispo Call URL with this parameters:
1. Rank: 1.
2. Statuses: ---ALL---
3. Description: Put a description for the action here.
4. URL: The URL from which the script is accesible. In my case, i installed the script in my ViciDial webserver, with an IP address of 10.0.0.1.
So, this field should be VARhttp://10.0.0.1/cgi-bin/rest-vtiger. ... PerlScript
8. Submit the form.
9. Set the URL to ACTIVE: 1.

With this, you will be creating a new Lead in vTiger each time you dispo a call. Of course, you can configure the Statuses field to create a lead only if the agent
dispositions a call with a specific status, like SALE.

As you can see in the URL, i am populating the lead in vTiger with data from the agent screen console:
- First: First name. --A--first_name--B--
- Last: Last name. --A--last_name--B--
- Phone: Phone number. --A--phone_number--B--
- Email: Email. --A--email--B--

You need to set some vTiger-specific fields, too:
- assigned_user_id: The user ID to which the lead will be asigned in vTiger. By default, the user ID for admin is 19.
- leadsource: This field indicates from which origin were the lead created. You can put anything here, like ViciDial.


Code: Select all
#!/usr/bin/env perl

use CGI;
use LWP::UserAgent;
use JSON;
use Digest::MD5 qw(md5_hex);
use Switch;
use strict;
use warnings;

my $key = 'sad98sad02';
my $username = 'admin';
my $host = 'https://vtiger.example.com';
my $endpoint = '/vtigercrm/webservice.php';

sub api_query {
    my $client = LWP::UserAgent->new;
    my $query_host = $host;
    my $method = $_[0];
    my $endpoint = $_[1];
    my $parameters = $_[2];
    my $query_url = $query_host.$endpoint;
    if ($method eq 'GET') {
        my $req = HTTP::Request->new(GET => $query_url);
        $req->header('content-type' => 'application/json');
        $req->header('Accept' => 'application/json');
        my $resp = $client->request($req);
        my $message = $resp->decoded_content;
        my $res = decode_json($message);
        return $res;
    }
    else {
        my $req = HTTP::Request->new(POST => $query_url);
        $req->header('content-type' => 'application/x-www-form-urlencoded');
        $req->content($parameters);
        my $resp = $client->request($req);
        my $message = $resp->decoded_content;
        my $res = decode_json($message);
        return $res;
    }
}

sub get_challenge {
    my $user = $_[0];
    my $endpoint = "$endpoint?operation=getchallenge&username=$user";
    my $method = 'GET';
    my $res = api_query($method, $endpoint);
    $res = $res->{'result'}->{'token'};
    return $res;
}

sub login {
    my $user = $_[0];
    my $userkey = $_[1];
    my $userchallenge = get_challenge($user);
    my $accesskey = md5_hex($userchallenge.$userkey);
    my $endpoint = "$endpoint?operation=login";
    my $parameters = '&username='.$user.'&accessKey='.$accesskey;
    my $method = 'POST';
    my $res = api_query($method, $endpoint, $parameters);
    $res = $res->{'result'}->{'sessionName'};
    return $res;
}

sub logout {
    my $sessionname = $_[0];
    my $endpoint = "$endpoint?operation=logout";
    my $parameters = '&sessionName='.$sessionname;
    my $method = 'POST';
    my $res = api_query($method, $endpoint, $parameters);
    return $res;
}

sub createobject {
    my $session = $_[0];
    my $module = $_[1];
    my $data = $_[2];
    my $endpoint = "$endpoint?operation=create";
    my $parameters = '&sessionName='.$session.'&elementType='.$module.'&element='.$data;
    my $method = 'POST';
    my $res = api_query($method, $endpoint, $parameters);
    return $res;
}

sub retrieveobject {
    my $session = $_[0];
    my $objectid = $_[1];
    my $endpoint = $endpoint.'?operation=retrieve&sessionName='.$session.'&id='.$objectid;
    print $endpoint;
    my $method = 'GET';
    my $res = api_query($method, $endpoint);
    return $res;
}

sub reviseobject {
    my $session = $_[0];
    my $data = $_[1];
    my $endpoint = "$endpoint?operation=revise";
    my $parameters = '&sessionName='.$session.'&element='.$data;
    my $method = 'POST';
    my $res = api_query($method, $endpoint, $parameters);
    return $res;
}

sub deleteobject {
    my $session = $_[0];
    my $objectid = $_[1];
    my $endpoint = "$endpoint?operation=delete";
    my $parameters = '&sessionName='.$session.'&id='.$objectid;
    my $method = 'POST';
    my $res = api_query($method, $endpoint, $parameters);
    return $res;
}

sub query {
    my $session = $_[0];
    my $query = $_[1];
    my $endpoint = $endpoint.'?operation=query&sessionName='.$session.'&query='.$query;
    print $endpoint;
    my $method = 'GET';
    my $res = api_query($method, $endpoint);
    return $res;
}

sub cgicmd {
    my $parameters = CGI->new;
    my $command = $parameters->param('command');
    my $session = login($username, $key);
    my $res;
    switch ($command) {
        case "query" {
            my $query = $parameters->param('query');
            $res = query($session, $query);
       
        }
        case "create" {
            my $firstname = $parameters->param('firstname');
            my $lastname = $parameters->param('lastname');
            my $phone = $parameters->param('phone');
            my $email = $parameters->param('email');
            my $uid = $parameters->param('assigned_user_id');
            my $leadsource = $parameters->param('leadsource');
            my $lead_data = "{";
            $lead_data = $lead_data."\"firstname\":\"$firstname\",";
            $lead_data = $lead_data."\"lastname\":\"$lastname\",";
            $lead_data = $lead_data."\"phone\":\"$phone\",";
            $lead_data = $lead_data."\"email\":\"$email\",";
            $lead_data = $lead_data."\"assigned_user_id\":\"$uid\",";
            $lead_data = $lead_data."\"leadsource\":\"$leadsource\"";
            $lead_data = $lead_data."}";
            createobject($session,'Leads',$lead_data);
            $res = "create\n\n";
        }
        case "retrieve" {
            my $id = $parameters->param('id');
            $res = retrieveobject($session, $id);
        }
        case "revise" {
            $res = "revise\n\n";
        }
        case "delete" {
            my $id = $parameters->param('id');
            deleteobject($session, $id);
            $res = "$id\n\n";
        }
    }
    print "Content-type: text/html\n\n";
    print $res;
    logout($session);
}

cgicmd();



I am planning to support ViciDial authentication with this in the coming days.

If you think of any other feature, please let me know.

This is like a proof of concept, so use it with care.

Re: ViciDial and vTiger v6/v7 integration

PostPosted: Tue Feb 19, 2019 10:27 am
by omarrodriguezt
Thank you!

Re: ViciDial and vTiger v6/v7 integration

PostPosted: Tue Feb 19, 2019 2:39 pm
by dspaan
:D :D Thanks for sharing!