Telegram bot keyboard - laravel

I create a telegram bot keaboard with laravel. There is a part of code.
$available_buttons =ReportToAbonent::select("report_id")
->join("telegram.reports", "report_to_abonent.report_id", "=", "reports.id")
->where("abonent_id", "=", $abonent[0]->id)
->where("active","=","1")
->get();
$keyboard = array();
foreach ($available_buttons as $value)
{
$keyboard[] = array($value->report_id);
}
$reply_markup = \Telegram::replyKeyboardMarkup([
'keyboard' => $keyboard,
'resize_keyboard' => true,
'one_time_keyboard' => false
]);
If I print
$keyboard`, its structure looks like if `$keyboard = [
['7', '8', '9'],
['4', '5', '6'],
['1', '2', '3'],
['0']
];
But in first case no keyboard in telegram client. In the second case it is present. What can I do wrong.

You have wrong format in inline keyboard, please check out API reference.
And you can refer to this sample request:
Contains only one button: (Parameters and Response)
Contains 2x3 buttons:

This is Two examples of each keyboard:
Normal Keboard:
const opts = {
reply_markup: JSON.stringify({
keyboard: [
['Normal'],
],
resize_keyboard: true,
one_time_keyboard: true,
}),
};
Inline Keboard:
const inlineopts = {
reply_markup: {
inline_keyboard: [
[{
text: 'Yahoo',
url: 'www.yahoo.com',
}],
[{
text: 'Google',
url: 'www.google.com',
}],
],
},
};

Related

How tom send email using Mandrill in React.js App?

I am new to mandrill and am using mandrill to send emails in my React application. Also, I've registered my domain in mandril. I've read the documentation but I can't find it.
You can use this code as reference:
import { useEffect} from 'react'
const Checkout = () => {
useEffect(() => {
const requestOptions: RequestInit | undefined = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
key: 'YOUR_API_KEY',
message: {
text: 'Example text content',
subject: 'example subject',
from_email: 'example#gmail.com',
from_name: 'Example name',
to: [
{
email: 'example#target.com',
name: 'Example target name',
},
],
},
}),
}
fetch('https://mandrillapp.com/api/1.0/messages/send.json', requestOptions).then(
async (response) => {
const res = await response.json()
console.log(res)
}
)
}, [])
return (
<h1>Hello</h1>
)
}
export default Checkout

store results from method in data form

I am going to preface this with I have NOT done this in a LONG time and my mind is mud. So no picking on me just remind me what I am doing wrong or not remembering.
NOTE: This is VueJS 3 / Tailwind 3 inside a Laravel 9 Jetstream Project
I have a method...
locatorButtonPressed() {
navigator.geolocation.getCurrentPosition(
position => {
console.log(position.coords.latitude);
console.log(position.coords.longitude);
},
error => {
console.log(error.message);
},
)
},
and I have a form data
data() {
return {
form: this.$inertia.form({
name: '',
email: '',
password: '',
password_confirmation: '',
birthdate: '',
user_latitude: '',
user_longitude: '',
user_city: '',
user_region: '',
user_country: '',
location: '',
terms: false,
})
}
}
I want to store position.coords.latitude and position.coords.longitude from the method in the form Data under user_latitude and user_longitude respectfully.
What am I forgetting???
The data properties can be accessed via this.PROPERTY_NAME. For example, to set form.user_latitude, use this.form.user_latitude = newValue.
export default {
methods: {
locatorButtonPressed() {
navigator.geolocation.getCurrentPosition(
position => {
this.form.user_latitude = position.coords.latitude;
this.form.user_longitude = position.coords.longitude;
})
},
}
}
demo

How can I pass a javascript callback to a PHP construct in Laravel-Charts

I am using the laravel-charts package in Laravel 7. I added the datalabels plugin for chartjs into the Chart object like this:
$this->options = [
'responsive' => true,
'maintainAspectRatio' => false,
'legend' => [ 'display' => false ],
'plugins' => [
'datalabels' => [
'color' => 'white',
'weight' => 'bold',
'font' => ['size' => 14],
'formatter' => ''
]
]
In another version when I was using vue.js and vue-chartjs, I was able to format the lable using this:
plugins: {
datalabels: {
formatter: function(value, context) {
return '$' + Number(value).toLocaleString();
},
}
}
As you can see, the javascript is passed as a PHP array. I cannot figure out how to pass that formatter to my laravel-charts version.
Any help is greatly appreciated.
Laravel Charts plugins option has to be a string that's representing a plain Javascript object. I couldn't find any actual documentation, but you can read a related issue here "How to use ChartsJs plugin Datalabels js?".
You'll have to pass it like this:
$chart = new ChartTest;
$chart->labels(['One Thousand', 'Two Thousand', 'Three Thousand', 'Four Thousand']);
$chart->dataset('My dataset', 'bar', [1000, 2000, 3000, 4000]);
$chart->options([
// The whole plugins element is a string representing a JS object with plugins options
'plugins' => "{
datalabels: {
color: 'red',
font: {
size: 14,
weight: 'bold'
},
formatter: (value) => `\\$\${value}`
}
}"
]);
return view('chart', ['chart' => $chart]);
Will apply chartjs-plugin-datalabels options:
PS: The weight property has to be inside the font object like in my example.

How to search through JSON response with Cypress assertions

Considering the below API response I would like to assert the exact location of a certain value in a JSON structure. In my case the name of pikachu within forms:
"abilities": [
{
"ability": {
"name": "lightning-rod",
"url": "https://pokeapi.co/api/v2/ability/31/"
},
"is_hidden": true,
"slot": 3
},
{
"ability": {
"name": "static",
"url": "https://pokeapi.co/api/v2/ability/9/"
},
"is_hidden": false,
"slot": 1
}
],
"base_experience": 112,
"forms": [
{
"name": "pikachu",
"url": "https://pokeapi.co/api/v2/pokemon-form/25/"
}]
I would like to extend below code snippet to not scan the entire body as a whole as there are a lot of name's in the response, but rather go via forms to exactly pinpoint it:
describe('API Testing with Cypress', () => {
var baseURL = "https://pokeapi.co/api/v2/pokemon"
beforeEach(() => {
cy.request(baseURL+"/25").as('pikachu');
});
it('Validate the pokemon\'s name', () => {
cy.get('#pikachu')
.its('body')
.should('include', { name: 'pikachu' })
.should('not.include', { name: 'johndoe' });
});
Many thanks in advance!
Getting to 'forms' is just a matter of chaining another its(), but the 'include' selector seems to require an exact match on the object in the array.
So this works
it("Validate the pokemon's name", () => {
cy.get("#pikachu")
.its("body")
.its('forms')
.should('include', {
name: 'pikachu',
url: 'https://pokeapi.co/api/v2/pokemon-form/25/'
})
})
or if you just have the name,
it("Validate the pokemon's name", () => {
cy.get("#pikachu")
.its("body")
.its('forms')
.should(items => {
expect(items.map(i => i.name)).to.include('pikachu')
})
})
and you can assert the negative,
.should(items => {
expect(items.map(i => i.name)).to.not.include('johndoe')
})
Can you try the below code and see if it helps with your expectation. From the response you could get the name as below;
describe('API Testing with Cypress', () => {
var baseURL = "https://pokeapi.co/api/v2/pokemon"
beforeEach(() => {
cy.request(baseURL+"/25").as('pikachu');
});
it('Validate the pokemon\'s name', () => {
cy.get('#pikachu').then((response)=>{
const ability_name = response.body.name;
expect(ability_name).to.eq("pikachu");
})
});
})

What is Expect function to validate schema in JEST and Supertest?

like in chakram testing expect(WallObject).to.have.schema(expectedSchema). Similarly which function is there in Jest? I am using jest with supertest.
There is nothing available in JEST to test schema directly. I have achieved this with the help of AJV. Using AJV i am comparing schema with response and then using Jest expect checking value is true or not. like
const Ajv = require('ajv');
const ajv = new Ajv({
allErrors: true,
format: 'full',
useDefaults: true,
coerceTypes: 'array',
errorDataPath: 'property',
sourceCode: false,
});
const validateParams = (params, schema) => {
const validate = ajv.compile(schema);
const isValidParams = validate(params);
return isValidParams;
};
const result = validateParams(res.body, {
type: 'array',
items: {
type: 'object',
properties: {
id: {
type: 'integer',
},
email: {
type: 'string',
},
}
}
});
expect(result).toBe(true);
done();

Resources