Discord API / Restcord : Create private channel - php

I'm trying to create private channels on Discord using Restcord, a PHP library that is very closely mapped to the Discord API.
Currently, I have managed to create voice channels, but all users on the server are able to see and join the channels. How can I make the channels so they are only available when invited ?
My current test code is :
$discord = new \RestCord\DiscordClient(['token' => config('services.discord.bot_token')]);
$channel = $discord->guild->createGuildChannel([
'guild.id' => config('services.discord.guild_id'),
'name' => 'lobby_' . uniqid(),
'type' => 2, // Voice
'permission_overwrites' => [
],
]);
$invitation = $discord->channel->createChannelInvite([
'channel.id' => $channel->id,
]);
return "https://discordapp.com/invite/{$invitation->code}";
Thanks in advance, any help is much appreciated, either with Restcord or directly using the API.

You need to use the permission_overwrites field. This fields accepts arrays of the overwrite object kind.
Example:
$discord = new \RestCord\DiscordClient(['token' => <token>]);
$channel = $discord->guild->createGuildChannel([
'guild.id' => config(<your guild ID>),
'name' => '<channel name>',
'type' => 2, // Voice
'permission_overwrites' => [
'id' => <role OR user id>,
'type' => 'role' (if id is role) OR 'user' (if id is single user),
'allow' => <permission ID for allowed permissions>,
'deny' => <permissions ID for denied permissions>
]
]);
Note that if you'd leave "allow" and "deny" as 0 (default value), the role/user with id 'id' would have the same permissions as otherwise defined server-wide. To get the permissions ID for a certain set of permissions, use a Discord Permission Calculator like this one (here the ID is called permission number).
Hope it helps!

Related

Realtime voice call with 2 person using nexmo/vonage

is it possible to do realtime voice call using nexmo/vonage with PHP or Javascript via web browser?
i used library called nexmo/laravel.
This sample code that i used:
$nexmo = Nexmo::calls()->create([
'to' => [[
'type' => 'phone',
'number' => '855969818674'
]],
'from' => [
'type' => 'phone',
'number' => '63282711511'
],
'answer_url' => ['https://gist.githubusercontent.com/jazz7381/d245a8f54ed318ac2cb68152929ec118/raw/6a63a20d7b1b288a84830800ab1813ebb7bac70c/ncco.json'],
'event_url' => [backpack_url('call/event')]
]);
with that code i can send text-to-speech, but how can i do realtime voice conversation person to person?
From the code you shared above, it looks like you might not have instantiated a client instance of the Nexmo PHP SDK, which is necessary to do so. You make an outbound call with an instantiated and authenticated client.
For example, first instantiate a client with the following, supplying the file path to your private key file, your application ID, your API key and your API secret. You can obtain all of those from the Dashboard
$basic = new \Nexmo\Client\Credentials\Basic('key', 'secret');
$keypair = new \Nexmo\Client\Credentials\Keypair(
file_get_contents((NEXMO_APPLICATION_PRIVATE_KEY_PATH),
NEXMO_APPLICATION_ID
);
$client = new \Nexmo\Client(new \Nexmo\Client\Credentials\Container($basic, $keypair));
Then, once you have a credentialed client, you can then invoke the $client->calls() methods on it. For example:
$client->calls()->create([
'to' => [[
'type' => 'phone',
'number' => '14843331234'
]],
'from' => [
'type' => 'phone',
'number' => '14843335555'
],
'answer_url' => ['https://example.com/answer'],
'event_url' => ['https://example.com/event'],
]);
You can find more information on using the PHP SDK on GitHub. You can also find code snippets, tutorials, and more instructions on our developer portal.

Onesignal Badge count increment is not working

In Onesignal api, I have added isIos => true,ios_badgeType => Increase,ios_badgeCount => 1,content_available => true in the field array.
But the badge count always remains as 1, it's not increasing with multiple messages.
This is my payload details : $fields = array( 'app_id' => "xxxxxx", 'included_segments' => array('All'), 'data' => array( "notification_type" => "update" ), 'contents' => $content, 'subtitle' => $subtitle, 'headings' => $heading, 'isIos' => true, 'ios_badgeType' => "Increase", 'ios_badgeCount' => 1, 'content_available' => true );
You must add Notification Extension in order to get badge auto update. Please follow the link to setup.
Also, need to create app group and assign both bundle id to that particular group. Please see the section -
In order for your application to be able to let push notifications
increment/decrement the badge count, you need to set up an App Group
for your application.
See here
Please follow https://documentation.onesignal.com/docs/ios-sdk-app-groups-setup in detail.
The name of your app group should be
group.{your_bundle_id}.onesignal
So for example, if your application's bundle identifier is
com.test.app, your app group name should be
group.com.test.app.onesignal.
Assign the group to both
target.
Open your Info.plist file and add a new OneSignal_app_groups_key as a String type.
Enter the group name you checked in the last step as it's value.
Make sure to do the same for the Info.plist under the OneSignalNotificationServiceExtension folder.

How to populate `identifier` and `providers` in Firebase custom authentication?

I'm authenticating my users on my web service and then creating Firebase custom token via php-jwt:
// Requires: composer require firebase/php-jwt
use Firebase\JWT\JWT;
// Get your service account's email address and private key from the JSON key file
$service_account_email = ...;
$private_key = ...;
function create_custom_token($uid, $is_premium_account) {
global $service_account_email, $private_key;
$now_seconds = time();
$payload = array(
"iss" => $service_account_email,
"sub" => $service_account_email,
"aud" => "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit",
"iat" => $now_seconds,
"exp" => $now_seconds+(60*60), // Maximum expiration time is one hour
"uid" => $uid,
"claims" => array(
"premium_account" => $is_premium_account
)
);
return JWT::encode($payload, $private_key, "RS256");
}
But the users that I authenticate this way, don't show the administrator-friendly "Identifier" and "Providers" fields in the "Authentication" panel in the Firebase Console:
The first two are users that I authenticated via this custom authentication process, and the last one is a user that I authenticated directly via Google.
How can I populate the "Identifier" and the "Providers" fields for users created via custom authentication?
The "Providers" column will only display an icon if the information attached to a user matches one or more of the the given providers in the "Sign-In Methods" section (https://console.firebase.google.com/project/_/authentication/providers).
Custom providers don't have a distinct icon, and Firebase wouldn't know what to display in the "Identifier" column (the UID is already in its own column at the end).
However, you do have some control for the display of the columns by creating them in advance (meaning: before signing them in for the first time), or by updating the user information after the user entry has been created.
I prepared an example showing which combination of fields leads to which display:
Please note:
The display name has no effect: if it is the only data provided, the user is considered anonymous.
Email + Password match the "Email/Password" Provider
Phone Numbers will alway match the "Phone" provider
The icons for a matched provider will be displayed in the column, even if a provider has been disabled.
Emails and Phone numbers have to be unique. If your application allows multiple users with the same email address/phone number, you will get into trouble, if you just want to see more information about the users of your Firebase project.
You can create and update users via the Firebase Auth REST API, but I would suggest to use one of the official Firebase Admin SDKs SDK to do it - in case you want to stick to PHP, I happen to know an unofficial one: kreait/firebase-php (Documentation) (Disclaimer: I'm the maintainer of the PHP SDK :) ).
On a non-technical note: I wouldn't bother too much with the user list in the Firebase Web Console: use the Firebase CLI tool or one of the official (or unofficial ;) ) Admin SDKs to create an overview that meets your needs.
You mentioned in the Bounty Annotation that you asked this in the Firebase Slack Community without an answer - you can find me and other PHP developers in the #php channel. I enabled notifications for the channel, so please feel free to join if you have further questions.
FYI, this is the code I wrote with the PHP SDK to create the data for the screenshot above:
<?php
declare(strict_types=1);
use Kreait\Firebase;
use Kreait\Firebase\Util\JSON;
require_once __DIR__.'/vendor/autoload.php';
$serviceAccount = Firebase\ServiceAccount::fromJsonFile(__DIR__.'/service_account.json');
$firebase = (new Firebase\Factory())
->withServiceAccount($serviceAccount)
->create();
$auth = $firebase->getAuth();
// Remove all users
foreach ($auth->listUsers() as $user) {
$auth->deleteUser($user->uid);
}
// Simulate custom auth
$ct = $auth->createCustomToken('a-custom-auth');
$r = $auth->getApiClient()->exchangeCustomTokenForIdAndRefreshToken($ct);
echo JSON::prettyPrint($auth->getUser('a-custom-auth'));
echo JSON::prettyPrint($auth->createUser([
'uid' => 'displayname-only',
'displayName' => 'Jérôme Gamez',
]));
echo JSON::prettyPrint($auth->createUser([
'uid' => 'email-only',
'email' => 'jerome#example.org',
]));
echo JSON::prettyPrint($auth->createUser([
'uid' => 'email-and-password',
'email' => 'jerome#example.com',
'password' => 'password'
]));
echo JSON::prettyPrint($auth->createUser([
'uid' => 'phone-only',
'phoneNumber' => '+49-123-1234567',
]));
echo JSON::prettyPrint($auth->createUser([
'uid' => 'email+name+phone',
'email' => 'jerome#example.net',
'displayName' => 'Jérôme Gamez',
'phoneNumber' => '+49-123-7654321',
]));
echo JSON::prettyPrint($auth->createUser([
'uid' => 'email+name+password+phone',
'email' => 'jerome#example.de',
'displayName' => 'Jérôme Gamez',
'password' => 'example123',
'phoneNumber' => '+49-321-7654321',
]));

Create campaign with dynamic segment using MailChimp API V3.0

Using MailChimp API V3.0 to create a campaign.
I want to create a campaign that sends to users with a specific interest. It looks like this is possible in the docs, but I've tried every permutation I can think of. I can create the campaign fine as long as I leave out the segment_ops member. Does anyone have an example of PHP code that will do it?
It seems that interests are handled strangely since you don't include the interest-category when setting a users interests via the API. I'm not sure how this affects campaign creation.
I've gotten this to work, API definition can be found here https://us1.api.mailchimp.com/schema/3.0/Segments/Merge/InterestSegment.json
Interests have to be grouped under interest categories (called 'Groups' in some parts of the UI).
Here is the JSON for the segment_opts member of the recipients array:
"segment_opts": {
"match": "any",
"conditions": [{
"condition_type": "Interests",
"field": "interests-31f7aec0ec",
"op": "interestcontains",
"value": ["a9014571b8", "5e824ac953"]
}]
}
Here is the PHP array version with comments. The 'match' member refers to the the rules in the array of 'conditions'. The segment can match any, all, or none of the conditions. This example has only one condition, but others can be added as additional arrays in the 'conditions' array:
$segment_opts = array(
'match' => 'any', // or 'all' or 'none'
'conditions' => array (
array(
'condition_type' => 'Interests', // note capital I
'field' => 'interests-31f7aec0ec', // ID of interest category
// This ID is tricky: it is
// the string "interests-" +
// the ID of interest category
// that you get from MailChimp
// API (31f7aec0ec)
'op' => 'interestcontains', // or interestcontainsall, interestcontainsnone
'value' => array (
'a9014571b8', // ID of interest in that category
'5e824ac953' // ID of another interest in that category
)
)
)
);
You may also send to a Saved segment. The gotcha on this is that the segment_id has to be int. I was saving this value in a db as varchar, and it would not work unless cast to int.
(i am using use \DrewM\MailChimp\MailChimp;)
$segment_id = (int) $methodThatGetsMySegmentID;
$campaign = $MailChimp->post("campaigns", [
'type' => 'regular',
'recipients' => array(
'list_id' => 'abc123yourListID',
'segment_opts' => array(
'saved_segment_id' => $segment_id,
),
),
'settings' => array(
'subject_line' => 'A New Article was Posted',
'from_name' => 'From Name',
'reply_to' => 'info#example.com',
'title' => 'New Article Notification'
)
]);

Is it possible to add a subdomain to Route53 using the AWS PHP SDK?

I am working on a project where we will be creating both subdomains as well as domains in Route53. We are hoping that there is a way to do this programmatically. The SDK for PHP documentation seems a little light, but it appears that createHostedZone can be used to create a domain or subdomain record and that changeResourceRecordSets can be used to create the DNS records necessary. Does anyone have examples of how to actually accomplish this?
Yes, this is possible using the changeResourceRecordSets call, as you already indicated. But it is a bit clumsy since you have to structure it like a batch even if you're changing/creating only one record, and even creations are changes. Here is a full example, without a credentials method:
<?php
// Include the SDK using the Composer autoloader
require 'vendor/autoload.php';
use Aws\Route53\Route53Client;
use Aws\Common\Credentials\Credentials;
$client = Route53Client::factory(array(
'credentials' => $credentials
));
$result = $client->changeResourceRecordSets(array(
// HostedZoneId is required
'HostedZoneId' => 'Z2ABCD1234EFGH',
// ChangeBatch is required
'ChangeBatch' => array(
'Comment' => 'string',
// Changes is required
'Changes' => array(
array(
// Action is required
'Action' => 'CREATE',
// ResourceRecordSet is required
'ResourceRecordSet' => array(
// Name is required
'Name' => 'myserver.mydomain.com.',
// Type is required
'Type' => 'A',
'TTL' => 600,
'ResourceRecords' => array(
array(
// Value is required
'Value' => '12.34.56.78',
),
),
),
),
),
),
));
The documentation of this method can be found here. You'll want to take very careful note of the required fields as well as the possible values for others. For instance, the name field must be a FQDN ending with a dot (.).
Also worth noting: You get no response back from the API after this call by default, i.e. there is no confirmation or transaction id. (Though it definitely gives errors back if something is wrong.) So that means that if you want your code to be bulletproof, you should write a Guzzle response handler AND you may want to wait a few seconds and then run a check that the new/changed record indeed exists.
Hope this helps!
Yes, I done using changeResourceRecordSets method.
<?php
require 'vendor/autoload.php';
use Aws\Route53\Route53Client;
use Aws\Exception\CredentialsException;
use Aws\Route53\Exception\Route53Exception;
//To build connection
try {
$client = Route53Client::factory(array(
'region' => 'string', //eg . us-east-1
'version' => 'date', // eg. latest or 2013-04-01
'credentials' => [
'key' => 'XXXXXXXXXXXXXXXXXXX', // eg. VSDFAJH6KXE7TXXXXXXXXXX
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXX', //eg. XYZrnl/ejPEKyiME4dff45Pds54dfgr5XXXXXX
]
));
} catch (Exception $e) {
echo $e->getMessage();
}
/* Create sub domain */
try {
$dns = 'yourdomainname.com';
$HostedZoneId = 'XXXXXXXXXXXX'; // eg. A4Z9SD7DRE84I ( like 13 digit )
$name = 'test.yourdomainname.com.'; //eg. subdomain name you want to create
$ip = 'XX.XXXX.XX.XXX'; // aws domain Server ip address
$ttl = 300;
$recordType = 'CNAME';
$ResourceRecordsValue = array('Value' => $ip);
$client->changeResourceRecordSets([
'ChangeBatch' => [
'Changes' => [
[
'Action' => 'CREATE',
"ResourceRecordSet" => [
'Name' => $name,
'Type' => $recordType,
'TTL' => $ttl,
'ResourceRecords' => [
$ResourceRecordsValue
]
]
]
]
],
'HostedZoneId' => $HostedZoneId
]);
}
If you get any error please check into server error.log file. If you get error from SDK library then there is might PHP version not supported.
if you run this code from your local machine then you might get "SignatureDoesNotMatch" error then Make sure run this code into same (AWS)server environment.

Categories