Skip to content

Instantly share code, notes, and snippets.

@sharifulin
Last active October 24, 2016 09:43
Show Gist options
  • Save sharifulin/f9cdc86efa589a7e9999eb93b29d4046 to your computer and use it in GitHub Desktop.
Save sharifulin/f9cdc86efa589a7e9999eb93b29d4046 to your computer and use it in GitHub Desktop.
Usage intercom (api_key)
# Пример модуля
use Mojo::JSON;
use Mojo::UserAgent;
has json => sub { Mojo::JSON->new };
has ua => sub { Mojo::UserAgent->new };
use constant DEBUG => $ENV{APPFOLLOW_ANALYTICS_DEBUG} || 1;
has is_debug => DEBUG;
# $self->_intercom_send($user, 'slack-intergration-started', $params);
sub _intercom_send {
my $self = shift;
my $user = shift || die qq(Can't find user for intercom event);
my $name = shift || return;
my $params = shift;
my $res = $self->ua->post(
$self->_intercom_req('/events'),
json => {
event_name => $name,
created_at => time,
user_id => $user->{id},
$params && %$params ? (metadata => $params) : (),
}
)->res->json;
warn qq(SEND Intercom EVENT "$name" for user "$user->{id}". Result is @{[ $res ? 'ERROR' : 'OK' ]}.\n) if $self->is_debug;
warn $self->dumper($res) if $res->{error} || $res->{errors};
$res;
}
sub _intercom_req {
my $self = shift;
my $conf = $self->conf->{intercom};
my $m = shift || return;
my $h = shift || {};
my $url = $conf->{api_url} . $m;
my $basic_auth = join ':', @$conf{qw(app_id api_key)};
$url =~ s{(://)}{$1$basic_auth@};
($url, {'Accept' => 'application/json', %$h});
}
# Пример скрипта
warn qq(User "$user->{id}"...\n);
my $created = int $self->u(iso2time => $user->{created});
my $res = $ua->post(r('/users'), json => {
user_id => $user->{id},
name => $user->{name},
email => $user->{email},
created_at => $created,
signed_up_at => $created,
custom_attributes => $attrs,
})->res->json;
die Dumper $res if $res->{error} || $res->{errors};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment