Change Slack Bot Icon from Post PHP - php

We post a message to a slack channel every time a customer does a specific task. We want to change the bot Icon based on what is being posted in the channel.
Is this possible?
public static function send_to_slack($message,$title = null){
$body = array();
$body['text'] = '';
$body['icon_url'] = '';
if(!empty($title)) $body['text'] .= "*$title*\n";
$body['text'] .= $message;
$iconURL = "https://img.icons8.com/emoji/96/000000/penguin--v2.png";
$body['icon_url'] .= $iconURL;
$json = json_encode($body);
//Test Slack Channel
$slack = "SLACKURL"
$response = wp_remote_post( $slack, array(
'method' => 'POST',
'body' => $json,
)
);
if ( is_wp_error( $response ) ) {
return true;
} else {
return true;
}
}

From Slack:
You cannot override the default channel (chosen by the user who installed your app), username, or icon when you're using Incoming Webhooks to post messages. Instead, these values will always inherit from the associated Slack app configuration.
*** UPDATE
I just ran into this situation. My old app was using the old web hooks. I had to create a new integration and ran into this issue. The simple solution is to install the slack app called "Incoming Webhooks". It's made by slack. It will allow you to generate an older style webhook that will allow you to post to any channel.

Found the correct way. Make sure you enable chat:write.customize in Slack oAuth Scope.
public static function send_to_slack($message,$title = null){
$ch = curl_init("https://slack.com/api/chat.postMessage");
$data = http_build_query([
"token" => "BOT-TOKEN",
"channel" => "CHANNELID", //"#mychannel",
"text" => $message, //"Hello, Foo-Bar channel message.",
"username" => "MySlackBot",
"icon_url" => $iconURL
]);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}

Related

Why is HubSpot Autogenerating Forms?

I am submitting a contact form to HubSpot using their api (https://developers.hubspot.com/docs/methods/forms/submit_form), which is working out great, except that every time I do it, HubSpot autogenerates a new form in its site under Marketing -> Forms, with a name like #form_5dd7ee368739f. It says that this is a non-Hubspot form and gives this explanation:
What is a non-HubSpot form
Non-HubSpot forms are HTML forms on your
website that weren't created in HubSpot. Based on your settings,
data for these forms is automatically collected in HubSpot. Learn
more.
"Learn more" isn't a link; I can't click on it. The submission of the api request is recorded both in this new form that it autogenerated each time the form is submitted, as well as in the form that I built in HubSpot that supposed to handle this request. Here is my code:
<?php
// wp-config.php
define('HUBSPOT_PORTAL_ID', getenv('hubspot_portal_id'));
define('HUBSPOT_CONTACT_FORM_GUID', getenv('hubspot_contact_form_guid'));
define('HUBSPOT_CONTACT_FORM_ENDPOINT', "https://forms.hubspot.com/uploads/form/v2/".HUBSPOT_PORTAL_ID."/{form_guid}");
?>
<?php
// hubspot.php
function hubspot_form_submit($page_url, $page_name, $endpoint, $data) {
$hs_context = array(
'ipAddress' => $_SERVER['REMOTE_ADDR'],
'pageUrl' => $page_url,
'pageName' => $page_name,
);
if (array_key_exists('hubspotutk', $_COOKIE)) {
$hs_context['hutk'] = $_COOKIE['hubspotutk'];
}
$data['hs_context'] = $hs_context;
$data_string = "";
foreach ($data as $key => $value) {
if (is_string($value)) {
$value = urlencode($value);
}
else if (is_array($value)) {
$value = json_encode($value);
}
$data_string = $data_string.$key."=".$value."&";
}
$data = rtrim($data_string, "&");
$ch = #curl_init();
#curl_setopt($ch, CURLOPT_POST, true);
#curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
#curl_setopt($ch, CURLOPT_URL, $endpoint);
#curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded'
));
#curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = #curl_exec($ch);
if(curl_error($ch)) {
$result = curl_error($curl);
}
$status_code = #curl_getinfo($ch, CURLINFO_HTTP_CODE);
#curl_close($ch);
return $result;
}
?>
<?php
// contact.php
require_once("inc/hubspot.php");
$hubspot_form_submission = hubspot_form_submit(
"https://www.example.com/contact/",
"Contact",
str_replace("{form_guid}", HUBSPOT_CONTACT_FORM_GUID, HUBSPOT_CONTACT_FORM_ENDPOINT),
array(
"firstname" => $form->data["first_name"],
"lastname" => $form->data["last_name"],
"email" => $form->data["email"],
"phone" => $form->data["phone"],
"preferred_contact_method" => $form->data["contact_method"],
"message" => $form->data["comments"],
)
);
?>
Anyone know how I can prevent HubSpot from autogenerating these forms? Otherwise my forms box will quickly become filled up with hundreds of autogenerated forms that I will keep having to delete. Something to note: the actual form that I created for this purpose is located within a folder, whereas the autogenerated forms are always located outside of any folders, if that makes any difference.
Found the problem: the client had enabled the use of Non-HubSpot forms in their settings, so all that is required to fix it is just to turn this functionality off. The documentation is here:
https://knowledge.hubspot.com/forms/use-non-hubspot-forms
The setting is located in
Settings -> Marketing -> Forms -> Non-HubSpot Forms

How to use JSON and HTTP POST in Laravel to work with a ticketing API

I'm trying to implement a JSON payload to create a ticket using HTTP POST and this API(https://github.com/osTicket/osTicket/blob/develop/setup/doc/api.md) in my PHP application (using Laravel framework) and I'm at an utter loss as to how to start attacking this problem. any insight?
I currently have a form built with laravel that will collect user data through a form. My goal is to take the data from said form, populate the fields on a JSON payload (the one I have below is from the API's github page) and then submit the ticket to the system when the "submit form" button is hit. Literally any help at all or advice would be greatly appreciated
This is what the JSON payload would look like but I also don't know where in laravel to put it/how to format it.
{
"alert": true,
"autorespond": true,
"source": "API",
"name": "Angry User",
"email": "randoemail#gmail.com",
"phone": "3333333333",
"subject": "Testing API",
"ip": "123.211.233.122",
"message": "data:text/html,MESSAGE <b>HERE</b>",
}
You can try it like so (No other stuff required):
Create an osTicket lib somewhere (I've placed it in app/Library)
namespace App\Library;
class osTicket {
protected $url;
protected $apikey;
protected $topicid;
public function __construct($apikey = null, $url = null, $ip = null) {
$this->url = env('TICKET_URL', $url);
$this->apikey = env('TICKET_APIKEY', $apikey);
$this->ip = env('TICKET_IP', $ip);
}
public function createTicket($name, $email, $phone, $subject, $message) {
$data = array(
'name' => $name,
'email' => $email,
'phone' => $phone,
'subject' => $subject,
'message' => $message,
'ip' => $this->ip,
[whatever other stuff you've configured in your OSticket, e.g. product_id, list_id, etc. - just pass appropriate variable]
);
function_exists('curl_version') or die('CURL support required');
function_exists('json_encode') or die('JSON support required');
set_time_limit(30);
$ch = curl_init();
//not everything below is needed. depends on you
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_USERAGENT, 'osTicket API Client v1.8');
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect:', 'X-API-Key: '.$this->apikey));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $result;
}
}
Setup your variables in .env:
TICKET_URL=http://xxxxxxxx.com/api/http.php/tickets.json
TICKET_APIKEY={api_key_here}
TICKET_IP={your_ip}
Then pass along form post data to your Controller like so:
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Library\osTicket;
use Auth;
use User;
class WhateverController extends Controller {
public function addticket(Request $request) {
$user = User::find(Auth::id());
$ticket = new osTicket;
$ticket->createTicket($user->name, $user->email, $user->phone, $request->subject, $request->message);
if($result === true) {
return back();
} else {
return back()->with('status', $result);
}
}
}
What you're looking to do should work by going through two steps
Build a PHP dict/array with the desired values, then (depending on the HTTP library - see below) convert that to JSON. Something like this in your controller method:
$dict['alert'] = $request->input('alert');
$dict['autorespond'] = $request->input('autorespond');
...
$json = json_encode($dict);
Alternaively if there is no content that should not be posted to the API you can do:
$json = json_encode($request->all());
See: https://laravel.com/docs/5.4/requests and http://us1.php.net/manual/en/function.json-encode.php
Once you have your json you need an http library to perform the request. Laravel recommends Guzzle so you would do something like:
$client = new GuzzleHttp\Client(['base_uri' => 'https://github.com.com/whatever/']);
$response = $client->post('the/api/endpoint', ['json' => $request->all()]);
Note that you don't need to json_encode the content here as Guzzle will do that for you.
See: http://docs.guzzlephp.org/en/latest/quickstart.html
From there handle the data however you need.

How to update segments and groups of an existing subscriber in MailChimp API 3

Iv'e written a method to subscribe users to MailChimp. Whats 'special' about it is that it automatically subscribe the users to groups within the list, and segments within the list, based on the users' cart items, wishlist items, and the item and / or category that he has subscribed from.
The integration with MailChimp is straight forward - I get the data > send curl > get response > handle response.
I'm looking for a way to constant update the users' groups and segments, based on their actions in my store.
Now, the only accepted statuses MailChimp can get are 'subscribed', 'pending', and 'clean'. All of them aren't updating, only inserting new subscribers. If the email is already subscribed, nothing is being updated, not even data that is different than what the subscriber has in its profile in my MailChimp lists.
Here's my code for reference:
protected static function subscribeToMailchimp($email, $fullname)
{
$params = EkerbaseJoomla::getPluginParams('system', 'ekerbaseusers');
$interests = self::getUserInterestsObject();
$apikey = $params->mailChimpApiKey;
$listId = $params->mailChimpListId;
$interestCategoryId = $params->mailChimpInterestCategoryId;
$auth = base64_encode( 'user:' . $apikey );
$apiUrl = 'https://'.substr($params->mailChimpApiKey, -3).'.api.mailchimp.com/3.0/lists/'.$listId;
$possibleGroups = json_decode(file_get_contents($apiUrl . '/interest-categories/' . $interestCategoryId . '/interests?apikey=' . $apikey))->interests;
$segments = json_decode(file_get_contents($apiUrl . '/segments?apikey=' . $apikey))->segments;
$data = [
'apikey' => $apikey,
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' =>
[
'FNAME' => $fullname
]
];
if( ! empty($interests->categories) ) {
$data['interests'] = [];
foreach( $possibleGroups as $group ) {
if( in_array($group->name, $interests->categories) ) {
$data['interests'][$group->id] = true;
}
}
}
if( ! empty($interests->items) ) {
$data['segments'] = [];
foreach( $segments as $segment ) {
if( in_array($segment->name, $interests->items) ) {
$data['segments'][$segment->id] = true;
}
}
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl . '/members/');
curl_setopt($ch, CURLOPT_HTTPHEADER,
[
'Content-Type: application/json',
'Authorization: Basic '.$auth
]
);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/3.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$result = curl_exec($ch);
$response = json_decode($result);
switch( $response->status ) {
case 'subscribed':
$responseMessage = JText::_('EKERBASE_SUCCESS_NEWSLETTER');
$responseStatus = 'success';
$responseResult = 'mailchimp subscribing succeeded';
break;
default:
$responseStatus = 'error';
$responseMessage = $response->title;
$responseResult = 'mailchimp subscribing failed';
if( $response->title === 'Member Exists' ) {
$responseMessage = JText::_('EKERBASE_NEWSLETTER_ALREADY_SUBSCRIBER');
}
break;
}
return EkerbaseAjax::buildJsonResponse($responseMessage, $responseStatus, $responseResult);
}
If your integration is adding entirely new subscriber as expected, and the issue appears isolated to cases where the method is updating an existing sub's record, the issue may pertain to the HTTP method, and/or the api endpoint.
As v3 of MailChimp's API only allows subscribers to be initialized when using the POST method(which looks like it may be hard coded into cURL here), and is likely why entirely new subscribers are being added without issue.
This said, when wanting to add or update new subscribers using PUT would be recommended, and is specified in their docs.
Add or update a list member
more on http methods and their API
Additionally, along with this alternate method usage, to ensure existing subscribers are updated, you'll also need to append the MD5 hash of the lower case version of their email to to the endpoint. This only needs to be done for existing subs.
e.g. /members/{lowercase_email_MD5_hash}
Which should be provided in the response if you're first checking with MailChimp whether or not a subscriber exist, if you'd like to recycle that.

Can't add thumbnail/image to Facebook App link

I've tried to add a thumbnail to the facebook app link, but can't even find documentation about it. Is it possible?
The current code (PHP/Laravel) gives me a working link, which looks like this: https: // fb.me/1234567890. It writes the app name as well when posted on Facebook, but with no image/thumbnail. I've tried putting an "image" or "thumbnail" parameter in http_build_query, but with no luck.
$url = "https://graph.facebook.com/v2.6/app/app_link_hosts";
$ch = curl_init($url);
# create form post data
$metadata = "?item=" . $request->itemid;
$deepLinkURL = "APP://" . $metadata;
//echo $deepLinkURL;
$androidArray = json_encode(array(array("url" => $deepLinkURL,
"package" => "com.app.package",
"app_name" => "APPNAME")
)
);
$iosArray = json_encode(array(array("url" => $deepLinkURL,
"app_store_id" => 45345345,
"app_name" => "APPNAME")
)
);
$webFallbackArray = json_encode(array("should_fallback" => false));
$formQuery = http_build_query(array("access_token" => "1234567890|XXXXXXXXXXXXXXXX",
"name" => "APPNAME",
"android" => $androidArray,
"ios" => $iosArray,
"thumbnail" => "http://i.imgur.com/upnywSR.jpg",
"web" => $webFallbackArray)
);
$path = base_path() . "/vendor/phpunit/phpunit/build/ca.pem";
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_CAINFO, $path);
# options
curl_setopt($ch, CURLOPT_POST, true); //1
curl_setopt($ch, CURLOPT_POSTFIELDS, $formQuery);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
# get response
$resultStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$jsonResponse = json_decode(curl_exec($ch), true);
curl_close($ch);
# decode response from facebook
$appLinkId = "";
# get appLinkId
foreach ($jsonResponse as $key => $val) {
# get status
if($key == "id") {
$appLinkId = $val;
}
}
# if response is good, need to request canonical URL from appLinkId
$errorMessage = "";
$canonicalUrl = "";
if(!empty($appLinkId)) {
# create another instance of cURL to get the appLink object from facebook using the ID generated by the previous post request
$getAppLinkUrl = "https://graph.facebook.com/" . $appLinkId;
$ch2 = curl_init($getAppLinkUrl);
# cURL options
$queryString = http_build_query(array("access_token" => "206722406330430|XRV38UNZsFfRNNF1EkfikzDWkpk",
"fields" => "canonical_url",
"pretty" => true)
);
/////////////////////
$path = base_path() . "/vendor/phpunit/phpunit/build/ca.pem";
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch2, CURLOPT_CAINFO, $path);
/////////////////
curl_setopt($ch2, CURLOPT_URL, $getAppLinkUrl . "?" . $queryString);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
# get response
// $urlResponseJson = curl_exec($ch2);
$urlJsonResponse = json_decode(curl_exec($ch2), true);
curl_close($ch2);
# decode response from facebook
# parse response to get canonical URL
foreach ($urlJsonResponse as $key => $val) {
# get canonical URL
if($key == "canonical_url") {
$canonicalUrl = $val;
}
}
# check for result
if(empty($canonicalUrl)) {
$errorMessage = "Unable to retreive URL.";
}
} else {
$errorMessage = "Unable to publish appLink.";
}
# encode response back to your app
if(empty($errorMessage)) {
$response = json_encode(array("result" => "success",
"canonical_url" => $canonicalUrl));
} else {
$response = json_encode(array("result" => "failed",
"errorMessage" => $errorMessage));
}
return $response;
I've tried to add a thumbnail to the facebook app link, but can't even find documentation about it. Is it possible?
No.
As https://developers.facebook.com/docs/applinks/hosting-api says,
If your application doesn't have a website for content you want to share to Facebook, you don't have public web URLs which you can annotate to support App Links. For these types of apps, Facebook provides an App Links Hosting API that will host App Links for you.
So if you have public web URLs that you want to share, then you should rather annotate those with the meta tags for App Links – then it will take the thumbnail you specified for those URLs via og:image.
If that is not an option, then you could still try and specify a thumbnail when you share the canonical URL of the App Link object, f.e. via the Feed dialog.

How to setup ios platform through code with apple push notification p12 certificate file?

I want to create an app with ios platform set up from the .p12 file. How do I do that?
This is the method for creating app:
class AppHandler
{
public $USER_AUTH_KEY = 'Insert your key here';
public function create($name, $apns_p12 = null, $apns_p12_password = null, $gcm_key = null, $android_gcm_sender_id = null)
{
$fields = array(
'name' => $name,
'apns_p12' => $apns_p12,
'apns_p12_password' => $apns_p12_password,
'gcm_key' => $gcm_key,
'android_gcm_sender_id' => $android_gcm_sender_id
);
$fields = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/apps");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
"Authorization: Basic " . $this->USER_AUTH_KEY));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
try {
$response = curl_exec($ch);
if (!$response) {
throw new Exception("App wasn't created");
}
} catch (Exception $e) {
echo 'Error: ', $e->getMessage(), "\n";
} finally {
curl_close($ch);
}
$response = json_decode($response, true);
$return = array(
'id' => $response['id'],
'basic_auth_key' => $response['basic_auth_key']
);
return $return;
}
...
And this is the method with 2 ways of getting the insides of .p12 file:
public function getP12($pkcs12, $password = NULL): string
{
/*
// Way 1:
$pkcs12 = file_get_contents($pkcs12);
$encoded = base64_encode($pkcs12);
return $encoded;
*/
// Way 2:
$cert_store = file_get_contents($pkcs12);
if (!$cert_store) {
echo "Error: can't read file.\n";
exit;
}
$pkcs12Read = openssl_pkcs12_read($cert_store, $cert_info, $password);
if ($pkcs12Read) {
$result = base64_encode($cert_info['cert']);
return $result;
} else {
echo "Error: can't read cert.\n";
exit;
}
}
According to onesignal's doc I have to send apns_p12 as my apple push notification p12 certificate file, converted to a string and Base64 encoded.
And I do that this way:
$obj = new AppHandler();
$response = $obj->create('TestName', $obj->getP12('cert.p12', 'password'), 'password')
It creates an app with given name, however, the platform is not set up.
What do you mean by "the platform is not set up"? What error are you getting and where?
By the way, I finally gave up trying code the intricacies of APNS programming and instead went with AWS' Simple Notification Service: https://aws.amazon.com/sns. It handles both Apple and Google notifications by using the API to set up topics and subscribers, plus you can send up to 1 million notifications per month free.
Ok, I got it. I simply needed to add apns_env parameter:
$fields = array(
'name' => $name,
'apns_env' => $apns_env,
'apns_p12' => $apns_p12,
'apns_p12_password' => $apns_p12_password,
'gcm_key' => $gcm_key,
'android_gcm_sender_id' => $android_gcm_sender_id
);
And I should've taken insides of the file and converted them to a string and Base64 encoded like that:
public function getP12($pkcs12): string
{
$apns_12 = base64_encode(file_get_contents($pkcs12));
return $apns_12;
}

Categories