I am trying to add callback data to reply_markup.
This is my code:
$option[] = array("test");
$replyMarkup = array('keyboard'=>$option,'one_time_keyboard'=>false,'resize_keyboard'=>true,'selective'=>true);
$encodedMarkup = json_encode($replyMarkup,true);
This code sends TEST to button and a call back to server TEST string for case
But I want use TEST string to show user and call back to server by KEY
This code does not work for me:
$option[] = array("text"=>"test","call_back"=>"key");
It looks you try to use ReplyKeyboardMarkup. It defines a keyboard with templates of messages which an user can send by tapping on a button.
But you want to get specific key so take a look at InlineKeyboardMarkup for this.
$options[][] = array('text' => 'Your text', 'callback_data' => 'test-data');
$replyMarkup = array('inline_keyboard' => $options);
$encodedMarkup = json_encode($replyMarkup, true);
When an user presses the button, your bot will receive a special update, CallbackQuery.
Related
I'm working on a telegram bot shop via https://github.com/php-telegram-bot/core in laravel.
In this app , each Product has many images that path of them is stored on th DB.
Now I want when send details of a product , images of that shown one by one that user can navigate them via a prev and next inline keyboard. like this picture :
For that after show all products in the shop as a inline query and after use choose one of them , On Chosen inline result Command, I get a product Id and fetch first image of that from DB like this :
class ChoseninlineresultCommand extends SystemCommand
{
public function execute ()
{
$chosenInlineResult = $this->getChosenInlineResult();
$chosenInlineResultId = $product_id = $chosenInlineResult->getResultId();
$chat_id = $chosenInlineResult->getFrom()->getId();
$product = Product::findOrFail($product_id);
$picture = $product->images->first();
$keyboard = KeyboardController::showProductKeyboard($product_id, $chat_id);
$result = view('show-product', compact('product','picture'))->render();
Request::sendMessage([
'chat_id' => $chat_id,
'text' => $result,
'reply_markup' => $keyboard,
'parse_mode' => 'HTML'
]);
}
}
Also showProductKeyboard of KeyboardController is like this :
static public function showProductKeyboard ($product_id, $user_id)
{
$inlineKeyboard = new InlineKeyboard([]);
$inlineKeyboard->addRow(new InlineKeyboardButton([
'text' => ' previous picture ⬅️️',
'callback_data' => 'prev_pic'
]), new InlineKeyboardButton([
'text' => '➡️ next picture ',
'callback_data' => 'next_pic'
]));
return $inlineKeyboard;
}
And finally show-product blade is simple as :
🛍️ <b>{{$product->title}}</b>
✅ <b>{{str_limit(strip_tags($product->desc), 50)}}</b>
💵 <b>{{number_format($product->price)}}</b> dollor
Picture
Problem is that I do not know how can I implement what I want .
You can't change the product image by callback query, you can change only the text by method editMessageText https://core.telegram.org/bots/api#editmessagetext
The solution can be deleting the message and send a new message with next picture.
And you should set more data in callback_data. For example showproduct_<id next product>
tl;dr; Trying to link the 'save and return' button when editing/deleting a course to my local plugins index.php instead of moodles default redirect for these features, moodle allready has a returnTo query parameter so i was thinking if that could be used somehow.
Hey
I am creating a local plugin that has a administration panel, where you can access CRUD on all courses in the system as seen in the picture below:
The problem now is that whenever I click edit, I get into the course edit page of course, but when I return from there I click "save and return" I would like to get back to my own admin page instead of the course page or category manage page.
The code I have right now looks like this:
//edit
$edit_course_moodle_url = new moodle_url('/course/edit.php', array('id' => $course->id, 'returnto' => 'local/adminpanel/index.php'));
$edit_course_url = $edit_course_moodle_url->get_path() . '?id=' . $edit_course_moodle_url->get_param('id') . '&returnto=' . $edit_course_moodle_url->get_param('returnto');
//delete
$delete_course_moodle_url = new moodle_url('/course/delete.php', array('id' => $course->id, 'returnto' => 'local/adminpanel/index.php'));
$delete_course_url = $delete_course_moodle_url->get_path() . '?id=' . $delete_course_moodle_url->get_param('id') . '&returnto=' . $delete_course_moodle_url->get_param('returnto');
As you can see I use the "returnto" query parameter, normally moodle has a "catmanage" as "returnto" that returns you to the category management page, where moodle has its own CRUD for categories and courses. So my question is, can I create my own alias for a link and use it like moodle uses the catmanage link, but for my admin page instead.
Thanks a lot ! :)
EDIT:
Change code to the following:
if (empty($CFG->loginhttps)) {
$securewwwroot = $CFG->wwwroot;
} else {
$securewwwroot = str_replace('http:','https:',$CFG->wwwroot);
}
$returnurl = new moodle_url($securewwwroot . '/local/adminpanel/index.php');
$edit_course_moodle_url = new moodle_url($securewwwroot . '/course/edit.php', array(
'id' => $course->id,
'sesskey' => sesskey(),
'returnto' => 'url',
'returnurl' => $returnurl->out(false))
);
$edit_course_url = $edit_course_moodle_url->out();
But it looks like moodle took away the button from edit course called "save and return" now it only has "save and display" or "Cancel" , both of which brings me back to the course, sad times :(
According to the code I can see in course/edit.php, you should use the following URL arguments:
returnto: 'url'
returnurl: The url
sesskey: sesskey()
In code that gives us:
$returnurl = new moodle_url('/local/plugin/page.php');
$editurl = new moodle_url('/course/edit.php', array(
'id' => 2,
'sesskey' => sesskey(),
'returnto' => 'url',
'returnurl' => $url->out(false)
));
echo $editurl->out();
The page course/delete.php does not seem to support those arguments. But it's probably easier for your plugin to delete the course by itself, it's as simple as calling delete_course($courseid);.
I am working in Magento, and i have developed a module to send text messages to customers. In the settings of the module, the admin can set the message that will be sent to the customer. I'm trying to add a features that will allow the replacement of texts with data from my database.
for example, i currently have the following code that fetches the saved settings for the body of the text message:
$body = $settings['sms_notification_message'];
The message that is fetched looks like this:
Dear {{firstname}},
your order ({{ordernumber}}) has been shipped.
tracking#: {(trackingnumber}}
Thanks for your business!
{{storename}}
The goal is to have the module replace the variables in "{{ }}" with the customer and store information.
Unfortunately, i'm unable to figure out how to make it replace the information before sending the message. It is currently being send as is.
The easiest way to do it would be to use str_replace, like so:
// Set up the message
$message = <<< MESSAGE
Dear {{firstname}},
your order ({{ordernumber}}) has been shipped.
tracking#: {{trackingnumber}}
Thanks for your business!
{{storename}}
MESSAGE;
// Assign the values in an associative array
$values = [
'firstname' => 'firstnamevalue',
'ordernumber' => 'ordernumbervalue',
'trackingnumber' => 'trackingnumbervalue',
'storename' => 'storenamevalue'
];
// Create arrays $target indicating the value to change
$targets = [];
foreach ($values as $k => $v) {
$targets[] = '{{'.$k.'}}';
}
// Use str_replace to perform the substitution
echo str_replace($targets,$values,$message);
I'm using this lib to send two different messages with different collapse keys, but on my device I'm receiving the first and then the second is coming over the first.
I would like to have the two separately in the Android notification header on device.
For the record I'm using this Phonegap plugin to receive the push notification.
Here is my code:
$gcmApiKey = 'api key here';
$deviceRegistrationId = 'device regid here';
$numberOfRetryAttempts = 5;
$collapseKey = '1';
$payloadData = ['title' => 'First Message Title', 'message' => 'First message'];
$sender = new Sender($gcmApiKey);
$message = new Message($collapseKey, $payloadData);
$result = $sender->send($message, $deviceRegistrationId, $numberOfRetryAttempts);
// Sending Second message
$collapseKey = '2';
$payloadData = ['title' => 'Second Message Title', 'message' => 'Second Message'];
$sender = new Sender($gcmApiKey);
$message = new Message($collapseKey, $payloadData);
$result = $sender->send($message, $deviceRegistrationId, $numberOfRetryAttempts);
If I understand you right, your problem is that the first notification is replaced by the second after it was shown.
If that is the case, your mistake is not on the PHP-side here, but in your Java-code.
If you show a notification you call this method:
NotificationManager.notify(int id, Notification notification)
Most likely, you are setting the id parameter to the same value each time you call this method.
The effect of the id is that the system will only show one notification with the same ID - the newest. A typical use-case to use the same id as before would be to update a previous notification.
If you want to display multiple notifications, you need to set a different id each time. You could use a random number or better yet use a previously defined ID of your content.
The GCM collapse key has a different effect:
When you define a collapse key, when multiple messages are queued up in the GCM servers for the same user, only the last one with any given collapse key is delivered.
That means, for example, if your phone was off, you would only receive one message with the same collapse key. It doesn't do anything if your phone receives the first notification before you send the second.
To set it with your PhoneGap plugin
The plugin has a really messy documentation but if we look into the source code, we'll find this undocumented feature:
int notId = 0;
try {
notId = Integer.parseInt(extras.getString("notId"));
}
mNotificationManager.notify((String) appName, notId, mBuilder.build());
That means, if you change your payload to, for example:
$payloadData = ['title' => 'First Message Title', 'message' => 'First message', 'notId' => mt_rand()];
your notifications won't replace each other.
I have a template file that I want to print a the cck user reference form field on.
Is this possible? Note, this is not the field value, but the field form. Also asking for just the field, not creating a form first and then using drupal_get_form()
Thanks!
Edit: If this is not possible that's fine too, I just would like to know
Edit2: i just need the autocomplete mechanism so I can grab the uid in js from searching for the username
If you just need autocomplete mechanism I would suject you to use jQuery autocomplete plugin - http://docs.jquery.com/Plugins/autocomplete.
you can just output in the template something like that:
print '<input type="text" id="user-autocomplete">';
Then in javascript code
$(document).ready('function(){
$('#user-autocomplete').autocomplete(some-ajax-url-here)
}');
you also will need to create an ajax callback page somewhere in your module:
function YOUR_MODULE_NAME_menu(){
$items = array();
$items['user-autocomplete-ajax-page'] = array(
'title' => 'AJAX:get user',
'page callback' => 'get_user'
);
}
function get_user(){
$sql = 'SELECT uid, name FROM {users} WHERE name LIKE ("%s")';
$result = db_query($sql,$_GET['request']);
$res_str = '';
while($object = db_fetch_object($result)){
$res_str .= $object->name.' ['.$object->uid."]\n";
}
print $res_str;
}
I didn't test the code, but I guess it should work, may be with some minor changes.