Trigger ID not found with Slack slash command? - php

My goal is to be able to use a slash command to open a dialog and process the feedback into a database. I am trying to get the dialog to open but am getting an error regarding the slash command where it says "trigger_id" not found.
My app is set up with an API and the proper OAuth.
I added a slash command to my app with the url of my php page (domain.com/slash.php)
The slash command is set up with the code below.
When I run it from my slack, I get the output of
'{"ok":false,"error":"invalid_arguments","response_metadata":{"messages":["[ERROR] missing required field: trigger_id"]}}'
I have tried some debugging and output the trigger_id to the screen and find that the trigger_id is indeed null. What am I missing to pass this?
I admit that I am new to the slack realm. I have followed (I think) the documentation from the slack site on setting up the app correctly.
Am I missing something with my slack app setup or something in my code that is causing this error message?
Thank you in advance for your time!
<?
$command = $_POST['command'];
$text = $_POST['text'];
$token = $_POST['token'];
$cn = $_POST['channel_id'];
$user_id = $_POST['user_id'];
$triggerid = $_POST['trigger_id'];
// define the dialog for the user (from Slack documentation example)
$dialog = [
'callback_id' => 'validres-3100',
'title' => 'Test',
'submit_label' => 'Submit',
'elements' => [
[
'type' => 'text',
'label' => 'Test Field 1',
'name' => 'field_1'
],
[
'type' => 'text',
'label' => 'Test Field 2',
'name' => 'field_2'
]
]
];
// define POST query parameters
$query = [
'token' => '<my api auth code>',
'dialog' => json_encode($dialog),
'trigger_id' => $triggerid
];
// define the curl request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://slack.com/api/dialog.open');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/x-www-form-urlencoded'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// set the POST query parameters
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($query));
// execute curl request
$response = curl_exec($ch);
// close
curl_close($ch);
var_export($response);
?>

To open dialog box in slack you can use this api "https://slack.com/api/views.open".
With api you need to send trigger id which is valid only for 3 seconds.
Your url will looks like :
"https://slack.com/api/views.open?trigger_id=" + "xxxx.xxxxxxxxx.xxxxxxxxxxxx" + "&view=your data".
with this request you need to send token with your post request like :-
(URL,"POST", { "token" ,"xoxb-xxxxxx-xxxxx-xxxxxxx"});
Need to add view.open API in your slack app also for this use following step:
Use "Bot User OAuth Access Token" , In "OAuth and permissions Tab" Format is xoxb-xxxxx-xxxxx-xxxx. And then add scope "views:open" and reinstall your app in slack. And then try to get open view dialog.
Hope this will be helpful.

Related

Telegram API: popup on telegram “Open this link?” How to avoid?

This is my first post. Hope I get help here. Thanks for reading.
Short version:
When i send a link to my bot, Telegram show a popup "Open this link .... ?" before open the link.
I want to avoid that. Any ideas?
See also questions
Force closure of the popup on telegram “Open this link?”
Is there a way to send links with telegram bot and show no alert on tap/click?
Long Version:
I have a telegram bot, which I'm sending a message from a Raspberry pi
via PHP. Background is some status notification on my smart home.
Please see code below.
I'm send a telegram message with a link attached with an inline keyboard, so that I can provide a certain responds to my smart home.
In other questions I saw that this is connected to the "parse_mode" html. I tried different modes, however the result is always the same.
Also checked the telegram api documentation for help.
https://core.telegram.org/bots/api#formatting-options
https://core.telegram.org/bots/api#sendmessage
As this is just for myself and running only locally, I don't care about cosmetics or security.
I would appreciate any help or new ideas.
Here is my code for reference
function telegram($message,$maschine) {
if (!isset($maschine)) {
echo "no Maschine for telegram";
exit;
}
if (!isset($message)) {
echo "no Message for telegram"; exit;
}
$website = "https://api.telegram.org/bot" . botToken;
$Keyboard = [
'inline_keyboard' =>
[
[
[
'text' => "test",
'url' => '192.168.1.1/test.php,
]
]
]
];
$encodedKeyboard = json_encode($Keyboard);
$params = [
'chat_id'=>chatId,
'text'=> $message,
'reply_markup' => #$encodedKeyboard,
'one_time_keyboard' => true,
'parse_mode'=> 'html'
];
$ch = curl_init($website . '/sendMessage');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($params));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$jsonresult = json_decode(curl_exec($ch), true);
curl_close($ch);
if ($jsonresult['ok']==false) {
echo "Telegram Error Code: " . $jsonresult['error_code'] . " - ". $jsonresult['description'] . "<br>";
} else {
echo "Telegram message send<br>";
}
}
I more or less found the answer now to my own question:
I haven't found a solution for the inline_keyboard.
If you comment the keyboard feature out and only use the link in the "text" paramater --> Then telegram is not asking for a extra confirmation to open the link.
See upated code below.
So issue solved. At least for me.
$params = [
'chat_id'=>chatId,
'text'=> 'open this link: 192.168.1.1/test.php',
//'reply_markup' => #$encodedKeyboard,
//'one_time_keyboard' => $setting,
'parse_mode'=> 'Markdown'
];

Slack Dialog gives an error

I am working on a small slack app development. I stuck in one situation. I am using slack dialog to get data from the user and when user enter data
and click on submit button I get an alert message. I don't know what
is it and why it gives an alert. What to do with this? Please note I get
payload response in my interactive component script. And respond to the server with 200. Here is my Response code :
if($type == "dialog_submission")
{
http_response_code(200);
return json_encode(array(
'status' => 200,
'message' => 'ok'
));
$ch = curl_init("https://slack.com/api/chat.postMessage");
$dataSet = http_build_query([
"token" => $authToken,
"channel" => $data['channel']['name'],
"text" => "123",
]);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataSet);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
}
Screenshot of alert message
Try something like:
if($type == "dialog_submission") {
return json_encode(array(
'status' => 200,
'message' => ''
));
}
You need to return an empty response to the Slack Dialog, or you will get the error
We had some trouble connecting. Try again?
So you must somewhere have an echo that returns something to the Slack dialog causing the error. You need to remove those. Like the echo $result; at the end.
This error will also occur if your script has a run-time error, since it will then create an automatic response like "error in test.php line 101....". To check for that make sure you have error logging activated and check if there are any errors in the logfile.
You activate error logging by putting these commands at the beginning of your script:
ini_set("log_errors", 1);
ini_set("error_log", "php-errors.log");
You can however return validation errors to Slack, but those must be in a specific format. See this documentation for details.

How to output the contents of file_get_contents

I'm trying to figure out how to get a php file (html, css and javascript) and load it into the content section below.
The following is the original...
function wpse_124979_add_help_tabs() {
if ($screen = get_current_screen()) {
$help_tabs = $screen->get_help_tabs();
$screen->remove_help_tabs();
$screen->add_help_tab(array(
'id' => 'my_help_tab',
'title' => 'Help',
'content' => 'HTML CONTENT',
I have tried the following but fails. I added a file_get_contents (first line), and then tried pull it in with 'content' => $adminhelp,
The following is with my amended code...
$adminhelp = file_get_contents('admin-help.php');
function wpse_124979_add_help_tabs() {
if ($screen = get_current_screen()) {
$help_tabs = $screen->get_help_tabs();
$screen->remove_help_tabs();
$screen->add_help_tab(array(
'id' => 'my_help_tab',
'title' => 'WTV Help',
'content' => $adminhelp,
Any ideas what's wrong?
If you want the output of the PHP file to be saved as $adminhelp use:
$adminhelp = file_get_contents('http://YOUR_DOMAIN/admin-help.php');
Right now you're loading the source code of admin-help.php into $adminhelp.
Another example for getting the output of a webpage is cURL:
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "http://YOUR_DOMAIN/admin-help.php");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$adminhelp = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
If this doesn't answer your question, please include the error message that you're receiving.

Send SMS Message using PHP

I have developed a system which will send SMS to the customer after making insert to the database
but the system which I worked on is public ie. when my client asked me to buy the system he must insert his SMS API configuration to send a message to his customers
when I searched on the internet I found that every API have a differnt way to send SMS message
I have account in click tell API but when I send message by curl there is nothing happen
the code is
$url = 'https://platform.clickatell.com/messages/http/send?' . http_build_query(
[
'apiKey' => 'oGjnzDdSRhqdhhgnjFj3ZmzYA==',
// 'api_secret' => '4c98619ffb9af51585',
'to' => '94233698311783',
'content' => 'Hello from Netcom'
]
);
echo($url);echo('<br/>');
echo'https://platform.clickatell.com/messages/http/send?apiKey=oGkmzDdSRgekvjFjaZnzYA==&to=905373545631&content=Test+message+text"';
$ch = curl_init($url);
echo($ch);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo $response;
Where my code is wrong and what is the way to send SMS dynamically depending on the client's API info
For quick fix and testing add this in your curl request and check response.(Because url is https)
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
But it is recomonded to downloading a CA root certificate bundle at the curl website and saving it on your server which protect your site
I am not aware of Clickatell, but Aws SNS is quite simple,
$payload = array(
'Message' => $message,
'PhoneNumber' => $mobile,
'MessageAttributes' => $msgAttributes
);
$result = $sns->publish($payload);

Send parameters to a URL and get output from that page

I have 2 pages say abc.php and def.php. When abc.php sends 2 values [id and name] to def.php, it shows a message "Value received". Now how can I send those 2 values to def.php without using form in abc.php and get the "Value received" message from def.php? I can't use form because when user frequently visits the abc.php file, the script should automatically work and get the message "Value received" from def.php. Please see my example code:
abc.php:
<?php
$id="123";
$name="blahblah";
//need to send the value to def.php & get value from that page
// echo $value=Print the "Value received" msg from def.php;
?>
def.php:
<?php
$id=$_GET['id'];
$name=$_GET['name'];
if(!is_null($id)&&!is_null($name))
{ echo "Value received";}
else{echo "Not ok";}
?>
Is there any kind heart who can help me solve the issue?
First make up your mind : do you want GET or POST parameters.
Your script currently expects them to be GET parameters, so you can simply call it (provided that URL wrappers are enabled anyway) using :
$f = file_get_contents('http://your.domain/def.php?id=123&name=blahblah');
To use the curl examples posted here in other answers you'll have to alter your script to use $_POST instead of $_GET.
You can try without cURL (I havent tried though):
Copy pasted from : POSTing data without cURL extension
// Your POST data
$data = http_build_query(array(
'param1' => 'data1',
'param2' => 'data2'
));
// Create HTTP stream context
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => $data
)
));
// Make POST request
$response = file_get_contents('http://example.com', false, $context);
Taken from the examples page of php.net:
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "example.com/abc.php");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
Edit: To send parameters
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( tch, CURLOPT_POSTFIELDS, array('var1=foo', 'var2=bar'));
use CURL or Zend_Http_Client.
<?php
$method = 'GET'; //change to 'POST' for post method
$url = 'http://localhost/browse/';
$data = array(
'manufacturer' => 'kraft',
'packaging_type' => 'bag'
);
if ($method == 'POST'){
//Make POST request
$data = http_build_query($data);
$context = stream_context_create(array(
'http' => array(
'method' => "$method",
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => $data)
)
);
$response = file_get_contents($url, false, $context);
}
else {
// Make GET request
$data = http_build_query($data, '', '&');
$response = file_get_contents($url."?".$data, false);
}
echo $response;
?>
get inspired by trix's answer, I decided to extend that code to cater for both GET and POST method.

Categories