How to set telegram bot commands with different scope in php - php

I want to set bot commands with different scope for some commands
I want
/help /list_hospitals - to be visible to all members of group
/Edit_hospitals - to be visible to admins only
I figured out a solution
i thought at first that setmycommands change command list alltogether but i found it change list for a given scope so my final solution looks like this
$link = 'https://api.telegram.org:443/bot'.$token.'';
//Command list for admins
$commands =json_encode(
array(
array('command'=>'edit',
'description'=> 'edit server hospitals '
),
array('command'=>'help',
'description'=> 'show how to use bot'
),
array('command'=> 'list',
'description'=>'list all hospitals
with a given name'
)
),true);
echo $commands;
$parameter = array(
'commands'=> $commands,
'scope'=>'{"type":"all_chat_administrators"}'
);
$request_url = $link.'/setMyCommands?'
.http_build_query($parameter);
echo file_get_contents($request_url);
// Command list for group members
$commands =json_encode(
array(
array('command'=>'help',
'description'=> 'show how to use bot'
),
array('command'=> 'list',
'description'=>'list all hospitals
with a given name'
)
),true);
echo $commands;
$parameter = array(
'commands'=> $commands
);
$request_url = $link.'/setMyCommands?'
.http_build_query($parameter);
echo file_get_contents($request_url);

Related

Running classes to make excel report for multiple items out of array and loop

I have most of the foundation here to take a hardcoded array, run a sql command for each id in the array in a loop, and then create data for each that would eventually come out as an excel report for each array record.
So I'll be calling makeReportFile.php which will in turn call on testReports.php. At this point, I want to loop on the array so that the key/number is plugged into my sql query and the name of the array element is used afterwards for filenames and email subjects, if that makes sense.
What I'm looking for once this runs is for one folder called 'excel/Reports' that would have 12 excel reports named 'First Address.xlsx', 'Second Address.xlsx', etc. My issue here is that I can easily run this separately with each as its own file but I want to run it this way so that if I add or remove elements from the array I just change one area of code.
How can I properly complete and loop here in order to achieve what I'm looking for?
testReports.php
class testReports {
static function getData(){
$items = array(
1452 => 'First Address',
1462 => 'Second Address',
1432 => 'Third Address',
1352 => 'Fourth Address',
1152 => 'Fifth Address',
1682 => 'Sixth Address',
1982 => 'Sevent Address',
1342 => 'Eighth Address',
1902 => 'Ninth Address',
1572 => 'Tenth Address',
1252 => 'Eleventh Address',
1842 => 'Twelvth Address',
);
$sql = "
select *
from tableTwo
where number = ?
";
//This should search by 1452,1462,1432, and so on
foreach($item in $items){
$results = DB::connection('odbc')->select($sql);
}
return $results;
}
public static function makeName(){
$currentTime = time();
return $name . "Report";
}
//This should return "First Address Report", "Second Address Report" and so on
}
makeReportFile.php
class makeReportFile
{
public function handle() {
$data = testingReports::getData();
$filename = testingReports::makeName();
$filepath = 'excel/reports';
$fileWriter->store('xlsx', storage_path($filepath));
$body = "Attached is the" . $name . "report";
Mail::raw($body , function ($message) use ($emails,$filepath,$filename) {
$message->subject($name . "Report");
});
}
}
UPDATE:
Now that I look at it, the array would probably need to be in the first class/file and loop around each, then pass the array values into the methods for the next class file

How to return a variable operator like $option[];

Obviously, I can't do this, but is there some way to achieve what I am trying to? I only found can not do's online, but no potential workarounds.
Here is what I am trying to do.
Currently I get the following error... "Cannot use [] for reading"
For my theme, I have a framework and the fields from that framework are built using an array that I create.
It looks something like this (minus the 300+ lines of code that I actually use)...
$options[] =
array(
'title' => 'This Field Tab Title',
'name' => 'this-field-tab-slug',
'fields' =>
array(
// ----------------------------------------------------------------------
// This Field Option Name
// ----------------------------------------------------------------------
array(
'type' => 'this_field_type',
'id' => 'this_field_types_id',
),
// ----------------------------------------------------------------------
// This Field Option Name
// ----------------------------------------------------------------------
array(
'type' => 'this_field_type',
'id' => 'this_field_types_id',
),
// ----------------------------------------------------------------------
// This Field Option Name
// ----------------------------------------------------------------------
array(
'type' => 'this_field_type',
'id' => 'this_field_types_id',
),
),
);
I am running a grouped field type, so my output has many options/fields within this grouped field/area which can then be added again and again as many times as the user needs. Then I am repeating that whole process/code again but for other taxonomies of the user's site.
So for example, the whole process above applies to post types, categories, tags, archived, etc. etc. So instead of having thousands of lines of repetitive codes, I'm trying to create my own function and pass the variables to that function.
But for the function, I find I can't return $options[];
Here is a screenshot of what I mean by the grouped field that can be added as many times as the user needs.
And here's an example of the function I am trying to create!
public static function layout_settings_config($title_name = '', $title_slug = '', $title_id = '', $query = '') {
$title_name = 'Post Type';
$title_slug = 'post-type';
$title_id = 'post_type';
$query = 'post_types';
$options[] =
array(
// all the config array codes in here...
);
return $options ??? $options[]; doesn't work.
}
Is this possible to achieve what I am trying to a different way? I'm still a little new to creating my own functions and OOP, but nothing I find online for this specific issue with a workaround.
Thanks!
$options[] is not object but it is an operation like function.
You should return $options instead.
and, by the way, when you say $options[] = something. it actually insert something inside an array called $option. so effectively you have to access your options like this.
$option[0]->title.
So I suggest Instead of making it complex like this. simply say
$option = something.

Can't update subscriber information Aweber API PHP

I'm trying to update Aweber subscriber information, specifically the custom fields and I'm using Aweber API but it's not working and probably I'm not writing correctly the code:
require_once('../AweberAPI/aweber_api/aweber_api.php');
include("../config.php");
$email=$_POST["email"];
$threefears=$_POST["3fears"];
$handlefears=$_POST["handlefears"];
$threeactions=$_POST["3actions"];
$changelife=$_POST["changelife"];
$consumerKey = '';
$consumerSecret = '';
$accessKey = '***'; # put your credentials here
$accessSecret = '***'; # put your credentials here
$account_id = ''; # put the Account ID here
$list_id = ''; # put the List ID here
$aweber = new AWeberAPI($consumerKey, $consumerSecret);
try {
$custom_field->name = 'Favorite Color';
$custom_field->save();
$params = array('email' => '$email');
$found_subscribers = $account->findSubscribers($params);
foreach($found_subscribers as $subscriber) {
$subscriber->custom_fields = array(
'Top 3 biggest fears related to dating' => '$threefears',
'How would the person you most admire handle these fears' => '$handlefears',
'What are 3 actions you can take today to act more like the person you most admire' => '$threeactions',
'How will taking these actions change your attitude towards dating and your life' => '$changelife',
);
$subscriber->save();
}
}
The custom fields you are submitting must already exist on your list before you can submit them via the API. This can be done within your aweber control panel using this process: https://help.aweber.com/hc/en-us/articles/204027516-How-Do-I-Create-Custom-Fields-
So if you created a custom field named 'age' then your code would look something like this (assuming an existing $subscriber object):
$fields = array(
'age' => '21',
);
$subscriber->custom_fields = $fields;
$subscriber->save();
or
$subscriber['custom_fields']['age'] = '21';
$subscriber->save();
I guess that instead of writing the values you are writing $threefears, $handlefears etc as text.
In your example you are putting variables as '$variable' instead of just $variable. That would write variable name instead of variable content.
so, instead of
$subscriber->custom_fields = array(
'Top 3 biggest fears related to dating' => '$threefears',
'How would the person you most admire handle these fears' => '$handlefears',
'What are 3 actions you can take today to act more like the person you most admire' => '$threeactions',
'How will taking these actions change your attitude towards dating and your life' => '$changelife',
);
try
$subscriber->custom_fields = array(
'Top 3 biggest fears related to dating' => $threefears,
'How would the person you most admire handle these fears' => $handlefears,
'What are 3 actions you can take today to act more like the person you most admire' => $threeactions,
'How will taking these actions change your attitude towards dating and your life' => $changelife
);
Note that even stackoverflow is correctly hightlighting variable names now.
And for Pete's sake, make the custom field's names shorter :) Most certainly there is a limit on the post you can make. Having such long variable name makes for less space in variable value, per post.
Oh and delete
$custom_field->name = 'Favorite Color';
$custom_field->save();
And modify from
$params = array('email' => '$email');
to
$params = array('email' => $email);
or to
$params = array('email' => $email, 'status' => 'subscribed');
It is correct that the custom fields you are submitting must already exist on your list before you can submit them via the API. This can be done within your aweber control panel using this process: https://help.aweber.com/hc/en-us/articles/204027516-How-Do-I-Create-Custom-Fields-
after creating a custom field named 'age' , the php code will be like this
$fields = array(
'age' => '21',
);
$subscriber->custom_fields = $fields;
$subscriber->save();

Print out a SQL single query (Yii 1.x)

I have a massive query that is generated using CDbCriteria as shown below:-
$schema = Yii::app()->db->schema;
$builder = $schema->commandBuilder;
// how to echo out this query?
$command = $builder->createFindCommand($schema->getTable('myuser'), $criteria);
$results = $command->queryAll();
I know I can use the 'logging' feature of Yii to view the query, is it possible to just echo out this single query (as opposed to having Yii show me tons of other queries that are being run on the page).
you can print query built by query builder by using $command->text.
In your example code will be:
$schema = Yii::app()->db->schema;
$builder = $schema->commandBuilder;
$criteria = new CDbCriteria();
$command = $builder->createFindCommand($schema->getTable('name_of_table'), $criteria);
$results = $command->text;
echo $results;
$command->text will return your complete query text
Add this in your config file. You can see the query and other details
at the bottom of the page.
'db'=>array(
'enableProfiling'=>true,
'enableParamLogging' => true,
),
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
…
array(
'class'=>'CProfileLogRoute',
'levels'=>'profile',
'enabled'=>true,
),
),
),

Order button not working for CGridView when using custom column - yii

I've recently added a custom column to my CGridView using the yii's get method to create a virtual attribute. The virtual attribute that I've created looks like this and works as expected.
public function getNumIndv()
{
// gets the id of the current list
$list_id = $this->id;
// returns the number of recipients with that list id
return $count = Recipient::model()->count(array("condition"=>"list_id = $list_id"));
}
From there in my GridView I added the custom column "numindv" like so
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'paylist-grid',
'dataProvider'=>$dataProvider,
'filter'=>$model,
'columns'=>array(
'name',
'numindv',
'balance',
'due_date',
/*
'status',
*/
array(
'class'=>'CButtonColumn',
'template'=>'{Manage}',
'buttons'=>array
(
'Manage' => array
(
'label'=>'Manage',
'url'=>'Yii::app()->createUrl("recipient/index", array("id"=>$data->id))',
),
)
),
),
)); ?>
This works as expected - almost. When the user views the form they see three columns "name, balance, and due-date" not lit up. As well, if you click on them they light up and are sorted.
for numindv however, the column is automatically lit up and does not allow individuals to click on it to change the order. Is there something simple that I'm missing here? Why is my grid view treating my virtual column different from the rest?
You cant use sorting in CActiveDataProvider by custom column.
You can make numindv column header link by using this approach:
$sort = new CSort();
$sort->attributes = array(
'numindv' => 'numindv',
'*' // all other columns
);
$dataProvider->sort = $sort;
But you still do not get the desired result, because when you click on the this sorting link it name will be added in SQL query:
SELECT * FROM `recipient` `t` ORDER BY `t`.`numindv`...
But your table is no column numindv.
I advise you to use a CArrayDataProvider.
UPD:
You can set sql rules for this sorting:
$sort->attributes = array(
'numindv' => array(
'asc'=>"numindv asc",
'desc'=>"numindv desc",
),
'*' // all other columns
);
And in Recipient::search() method you should add this line:
$criteria->select .= ', (/*any SQL request to retrieve the numindv value*/) as numindv';

Categories