object_story_spec spec must be an associative array - facebook

I wanna create a creative with api. When i post object_story_spec parameter, i got this error 'Creative spec must be an associative array (optionally json-encoded)'
this is my json value it is valid.
{ "page_id" : "103830656322074", "link_data": { "call_to_action": {"type":"LEARN_MORE","value":{"link":"facebook.com/"}}, "caption": "Reklam #1", "name": "Reklam #1", "link": "facebook.com/", "message": "facebook.com/" }}
developers.facebook.com/docs/marketing-api/reference/ad-creative#Creating

you should url enocde the $object_story_spec before passing into the creative like below.
$object_story_spec = urlencode($object_story_spec);
$creative = new AdCreative(null, 'ad_Acount_id');
$creative->setData(array(
AdCreativeFields::NAME => 'Sample Creative',
AdCreativeFields::OBJECT_STORY_SPEC => $object_story_spec,
));

It should be like something.
object_story_spec={
"page_id": "<PAGE_ID>",
"video_data": {
"call_to_action": {"type":"LIKE_PAGE","value":{"page":"<PAGE_ID>"}},
"description": "try it out",
"image_url": "<THUMBNAIL_URL>",
"video_id": "<VIDEO_ID>"
}
}
Or
$object_story_spec = new ObjectStorySpec();
$object_story_spec->setData(array(
ObjectStorySpecFields::PAGE_ID => <PAGE_ID>,
ObjectStorySpecFields::LINK_DATA => <LINK_DATA>,
));
$creative = new AdCreative(null, 'ad_Acount_id');
$creative->setData(array(
AdCreativeFields::NAME => 'Sample Creative',
AdCreativeFields::OBJECT_STORY_SPEC => $object_story_spec,
));

Just cast the targeting field to string, For example, if you are using requests:
import requests
params = {
'name': 'My Ad Set',
'optimization_goal': 'LINK_CLICKS',
'billing_event': 'IMPRESSIONS',
'bid_amount': 2,
'daily_budget': 100,
'campaign_id': campaign_id,
"targeting": str({
"age_max": 65,
"age_min": 18,
"flexible_spec": [..]
}),
'start_time': datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ"),
'status': 'PAUSED',
'access_token': access_token,
}
response = requests.post(
url=f'https://graph.facebook.com/v11.0/act_{ad_account_id}/adsets',
params=params
)

Related

Grouping Hash Tags with same hashTag name

I am finding a problem in group hashtags: Any Solution?
here is my query statement: any help.
def trending_tags(user)
#tags= HashTag.joins(:vibe_hash_tags).limit(10).where('hash_tags.created_at BETWEEN ? AND ?', 1.months.ago, Time.now).select("hash_tags.id, hash_tags.address as country, count(vibe_hash_tags.id) as vibes_hash_tags,COUNT(hash_tags.id)as hash_tag_count")
.group('COUNT(hash_tags.name as hash_tag_name)')
render :status => 200, :json => {:success => true, :result => { :trending_hash_tags => #tags}}
end
I would want to group it to this
{
"success": true,
"result": {
"trending_hash_tags": [
{
"id": 83,
"country": "TV",
"vibes_hash_tags": 2,
"hash_tag_name": "money",
"hash_tag_count": 2
},
{
"id": 84,
"country": "TV",
"vibes_hash_tags": 3,
"hash_tag_name": "grace",
"hash_tag_count": 3
}
]
}
}
I have been able to find a solution my question for grouping Hash Tags.
def trending_tags
#hash_tags= HashTag
.select('hash_tags.name AS hash_tag_name, COUNT(hash_tags
.id) AS hash_tag_count, hash_tags.address AS country, count(vibe_hash_tags.id) AS vibes_hash_tags')
.joins(:vibe_hash_tags)
.order('hash_tag_count DESC')
.group('hash_tags
.name, hash_tags.address').limit(10).where('hash_tags.created_at BETWEEN ? AND ?', 5.days.ago, Time.now)
render :status => 200, :json => {:success => true, :result => { :trending_hash_tags => #hash_tags}}
end

Datepicker change format ufter updating

I am using the bootstrap datepicker in my view and every time I call the update method, the date sets fine but the date picker suddenly reverts from dd/mm/yyyy format to mm/dd/yyyy format.
Controller
public function actionDias(){
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
if (isset($_POST['anio']) and isset($_POST['cuatrimestre'])) {
$cuatrimestre = $_POST['cuatrimestre'];
$anio = $_POST['anio'];
$fechaMin = CampusActividadDia::find()->where(['cuatrimestre' => $cuatrimestre])->andWhere(['anio' => $anio])->min('fecha');
$fecha_desde = date("d/m/Y", strtotime($fechaMin));
$fechaMax = CampusActividadDia::find()->where(['cuatrimestre' => $cuatrimestre])->andWhere(['anio' => $anio])->max('fecha');
$fecha_hasta = date("d/m/Y", strtotime($fechaMax));
return ([
'fecha_desde' => $fecha_desde,
'fecha_hasta' => $fecha_hasta
]);
} else return null;
}
View
<div id="dias" class="form-group hidden">
<label class="control-label">Rango de Fechas:</label>
<?php
echo DatePicker::widget([
'model' => $model,
'attribute' => 'fecha_desde',
'attribute2' => 'fecha_hasta',
'options' => ['id'=>'fecha_desde','disabled' => ($model->fecha_desde == "")],
'options2' => ['id'=>'fecha_hasta','disabled' => ($model->fecha_desde == "")],
'type' => DatePicker::TYPE_RANGE,
'separator' => 'hasta',
'pluginOptions' => [
'autoclose' => true,
'format' => 'dd/mm/yyyy'
]
]); ?>
</div>
Js
$.ajax({
url: URL_BASE + "/campus/dias",
type: "post",
dataType: "json",
data: {
cuatrimestre: this.value,
anio: $('#anio').val()
},
success: function (res) {
const fecha_desde = res.fecha_desde;
const fecha_hasta = res.fecha_hasta
$(fechaDesdeRef).kvDatepicker('update',new Date(fecha_desde));
$(fechaHastaRef).kvDatepicker('update', new Date(fecha_hasta));
$(fechaDesdeRef).prop('disabled', false);
$(fechaHastaRef).prop('disabled', false);
},
error: function (xhr, ajaxOptions, thrownError) {
alert("ERROR " + xhr.status);
},
});
I've also tried to change date format both ways setting the global default format and locally but without success, like the following:
$.fn.kvDatepicker.defaults.format = 'dd/mm/yyyy'
$(datePickerRef).kvDatepicker({
formatDate: 'd/m/Y',
date:'d/m/Y'
})
I've also read this issue, but there isn't a conclution about it:
https://github.com/uxsolutions/bootstrap-datepicker/issues/1775
Sounds like that your DatePicker is overwriting pluginOptions when model is loaded. Might need to config DatePicker or overwrite it to stop missing format
config/main.php
'modules' => [
'datecontrol' => [
'class' => 'kartik\datecontrol\Module',
'displaySettings' => [
'date' => 'php:d-M-Y',
'time' => 'php:H:i:s A',
'datetime' => 'php:d-m-Y H:i:s A',
],
'saveSettings' => [
'date' => 'php:Y-m-d',
'time' => 'php:H:i:s',
'datetime' => 'php:Y-m-d H:i:s',
],
// automatically use kartikwidgets for each of the above formats
'autoWidget' => true,
],
],
'components' => [
//http://webtips.krajee.com/override-bootstrap-css-js-yii-2-0-widgets/
'assetManager' => [
'bundles' => [
'yii\bootstrap4\BootstrapAsset' => [
'sourcePath' => '#npm/bootstrap/dist'
],
'yii\bootstrap4\BootstrapPluginAsset' => [
'sourcePath' => '#npm/bootstrap/dist'
],
'#frontend\modules\invoice\assets\CoreCustomCssJsAsset'=>[
'sourcePath' => '#frontend/modules/invoice/assets'
],
],
composer.json
"require": {
"php": ">=7.4.9",
"guzzlehttp/guzzle":"*",
"yiisoft/yii2": "~2.0.41.1",
"yiisoft/yii2-bootstrap": "~2.0.9",
"yiisoft/yii2-bootstrap4": "^2.0.8",
"bower-asset/bootstrap": "~3.4.1",
"bower-asset/ladda": "*",
"bower-asset/font-awesome": "~4.7.0.0",
"npm-asset/jquery": "^2.2",
"vlucas/phpdotenv": "*",
"insolita/yii2-migration-generator": "~3.1",
"ifsnop/mysqldump-php": "*",
"warrence/yii2-kartikgii": "dev-master",
"kartik-v/yii2-bootstrap4-dropdown": "#dev",
"kartik-v/bootstrap-fileinput": "dev-master",
"kartik-v/yii2-editable": "#dev",
"kartik-v/yii2-grid":"#dev",
"kartik-v/yii2-widget-timepicker": "#dev",
"kartik-v/yii2-date-range": "*",
"kartik-v/yii2-social": "#dev",
"kartik-v/yii2-dynagrid": "dev-master",
"kartik-v/yii2-tree-manager": "#dev",
"kartik-v/yii2-mpdf":"dev-master",
"kartik-v/bootstrap-star-rating": "#dev",
"kartik-v/yii2-slider": "dev-master",
"kartik-v/yii2-number" : "#dev",
"kartik-v/yii2-editors": "#dev",
"kartik-v/yii2-validators": "dev-master",
},
"require-dev": {
"yiisoft/yii2-debug": "~2.1.0",
"yiisoft/yii2-gii": "~2.1.0",
"yiisoft/yii2-faker": "~2.0.0",
"codeception/codeception": "^4.0",
"codeception/verify": "~0.5.0 || ~1.1.0"
},
"config": {
"process-timeout": 1800
},
"fxp-asset": {
"installer-paths": {
"npm-asset-library": "vendor/npm-asset",
"bower-asset-library": "vendor/bower-asset"
}
},
These settings have worked for me. You might considering modifying your AppAsset.php as follows to incorporate bootstrap 4.
<?php
namespace frontend\assets;
use yii\web\AssetBundle;
use yii\bootstrap4\BootstrapAsset;
use yii\web\YiiAsset;
use yii\web\JqueryAsset;
/**
* Main frontend application asset bundle.
*/
class AppAsset extends AssetBundle
{
public $sourcePath = '#app/assets/app';
public $baseUrl = '#app';
public $css = [
'css/site.css',
'//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css',
'//use.fontawesome.com/releases/v5.3.1/css/all.css',
'//cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/all.min.css'
];
public $js = [
'//kit.fontawesome.com/85ba10e8d4.js',
];
public $depends = [
BootstrapAsset::class,
YiiAsset::class,
JqueryAsset::class,
];
}

How to recreate perl's array of hashes in golang?

In perl I can push $hashref into #array and have this data for next foreach and possible encode_json (HTTP POST).
I can't figure out how to recreate the same login in golang?
$VAR1 = [
{
'address' => 'test.com',
'id' => 101,
'hostgroups' => [
zero
'one',
'or many'
],
'host_name' => 'test.com',
'alias' => 'test.com',
'template' => 'generic',
'file_id' => 'etc/config'
},
{
'address' => 'test2.com',
'id' => 102,
'hostgroups' => [
zero
'one',
'or many'
],
'host_name' => 'test2.com',
'alias' => 'test2.com',
'template' => 'generic',
'file_id' => 'etc/config'
},
(..)
var array = []map[string]interface{}{
{"address": "test.com", "hostgroups": []string{"zero", "one", "or many"}, "id": 101},
{"address": "test2.com", "hostgroups": []string{"zero", "one", "or many"}, "id": 102},
}
This is the answer.
type host map[string]interface{}
var hosts []host
h := host{
"id": id,
"file_id": "etc/config/hosts.cfg",
"host_name": host_name,
"alias": host_name,
"address": host_name,
"hostgroups": hg,
"template": "generic-host",
}
hosts = append(hosts, h)

Perl Rest Web Service Parameters

I am trying to call REST web service as follows:
my $client = REST::Client->new();
$client->setHost("http://myhost.com");
$client->POST('/xx/yy/Submit',
$params,
{'Content-type' => 'application/json'}
);
my $response = $client->responseContent();
I do not how to create parameter list, api definition is as follows:
{
"Credential": {
"Username": "String",
"Password": "String"
},
"DataCoding": "Default",
"Header": {
"From": "String",
"ValidityPeriod": 0
},
"Message": "String",
"To": ["String"]
}
I tried following but did not work:
my %params = (Credential => {
Username => $username,
Password => $password
},
DataCoding => 'Default',
Header => {
From => $from,
ValidityPeriod => 0
},
Message => "Test",
To => ['90535xxxx','90542xxxxx']
);
$client->POST('/v1/xml/syncreply/Submit',
encode_json(\%params),
{'Content-type' => 'application/json',
'Accept' => 'application/json'
}
);
It gives following error:
<ErrorCode>SerializationException</ErrorCode>
Error: System.Runtime.Serialization.SerializationException: There was an error deserializing the object of type Barabut.Gw.Submit. The data at the root level is invalid. Line 1, position 1. ---&gt; System.Xml.XmlException: The data at the root level is invalid. Line 1, position 1.
I wrote end-point address incorrect, it worked at the format I wrote, sorry for this situation.

MongoDB $addToSet not writing to database

I am not sure why but I am having a really hard time getting the $addToSet working for my sub array items.
This is what it should look like:
{
"items": [
{
"id": "510bca8138fc5d6e38000000",
"quantity": "1"
},
{
"id": "51011a8138fc5d6348000000",
"quantity": "1"
}
],
"session": "1359948849.291898629576",
"status": "cart"
}
However, it seems to only allow the first:
{
"items": [
{
"id": "510bca8138fc5d6e38000000",
"quantity": "1"
}
],
"session": "1359948849.291898629576",
"status": "cart"
}
and it just won't insert another sub array.
My code:
$document = $collection->findOne(array('session' => $_SESSION["redi-Shop"]));
//print_r($document);
if (null !== $document) {
$collection->update(
array('session' => $_SESSION["redi-Shop"]),
array(
'$addToSet' => array(
'items' => $_POST['item']
),
));
print_r($_POST['item']);
}
else
{
$collection->insert(
array('session' => $_SESSION["redi-Shop"],
'status' => "cart",
'items' => $_POST['item'])
);
}
I changed the $addToSet to $Push and it works fine