Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to set param for direct message #282

Closed
pspatel opened this issue Nov 28, 2019 · 22 comments · Fixed by #284
Closed

Not able to set param for direct message #282

pspatel opened this issue Nov 28, 2019 · 22 comments · Fixed by #284
Assignees
Labels

Comments

@pspatel
Copy link

pspatel commented Nov 28, 2019

Hello There,

I have integrated this in my web app and now wants to use direct message but I am able to send because getting error.

Array to string conversion

so it means either I am using and sending parameters wrong in postDm method or something need to change in tmhOAuth.php which we are using for sending request.

Please let me know how I can create params and can use direct message apis.

Here is the code sample which I am following ->


$twitterConfigs=[
'consumer_key' => 'my_consumer_key',
'consumer_secret' => 'my_consumer_secret',
'token'     => 'my_oauth_token',
'secret'     => 'my_oauth_token_secret',
];
Twitter::reconfig($twitterConfigs);

$recipient_id = 1234567890; 
$pm_mesaage = urlencode('Hello World');


$params = ['type'=>'message_create', 
			 'message_create' => [ 
			 	'target' => ['recipient_id' => $recipient_id],
			 	'message_data' => ["text" => $pm_mesaage]
			 ]
			];
try
{
	$response = Twitter::postDm($params);
	print_r($response);
}
catch (\Exception $e)
{
	$errors = Twitter::error();
	print_r($errors);
}
@atymic
Copy link
Owner

atymic commented Nov 29, 2019

Hmm. Are you able to post the full error (ie. what line the error is occuring on?
I'll investigate further soon.

@atymic atymic self-assigned this Nov 29, 2019
@atymic atymic added the bug label Nov 29, 2019
@pspatel
Copy link
Author

pspatel commented Nov 29, 2019

Yes, the error is occurring on line number 362 in tmhOAuth/tmhOAuth.php file. I think when we are sending the params as per the twitter APIs not able to use because an error occurred in the below code in prepare_params()function.

      if (is_array($v))
        $v = implode(',', $v);

I hope it will help to debug and investigate.

@atymic
Copy link
Owner

atymic commented Dec 1, 2019

Hey @pspatel

I've pushed up a fix in this PR:
#284

Would you be able to test and double check it's working correctly now (as we don't have any automated unit/integration tests)

You can do this by switching your version constraint to dev-fix-create-dm-issue
https://packagist.org/packages/thujohn/twitter#dev-fix-create-dm-issue

Thanks!

@pspatel
Copy link
Author

pspatel commented Dec 2, 2019

Hi @atymic

I have tested as per your suggestion and have checked your changes in tmhOAuth.php file but you only changed in one place for checking string or not.

image

As per above screenshot I think need to change in full function and also may be in other related function for $v variable.

I hope it will help you to look into it.

Thanks

@atymic
Copy link
Owner

atymic commented Dec 2, 2019

Oops :)

Looks like I missed some of the other occurrences, which look more complex.
We're moving away from the library in the future hopefully, as it's not very good these days (or maintained).

I'll have a look and see if we can patch it.
I'll need to set up a test account and try the calls because we don't have any tests :(

@pspatel
Copy link
Author

pspatel commented Dec 2, 2019

Yes, it little bit complex and need to change on many places. Let me know if I can help you in testing or in any way, so that it will work smoothly for messaging APIs.

@KarolJosef
Copy link

Resolveram o problema?

@sykezz
Copy link
Contributor

sykezz commented Dec 12, 2019

I'm on the same boat as well. I think we also need to handle the request with Content-Type: application/json, according to the docs. At the moment, I don't think tmhOAuth is able to do that since it doesn't handle json and all requests are with Content-Type: application/x-www-form-urlencoded.

@sykezz
Copy link
Contributor

sykezz commented Dec 15, 2019

So I tried to get this working on tmhOAuth with json, but it got a bit more complex and I ended up using twitteroauth instead 🤣

@atymic
Copy link
Owner

atymic commented Dec 16, 2019

@sykezz Can you send a link to your fork with the changes? :)
Thanks

@sykezz
Copy link
Contributor

sykezz commented Dec 16, 2019

@atymic The one I did on tmhOAuth? Sure, I just pushed the commit here. It works, but I was only just trying to get the json working with postDm() method. A bit more work need to be done to get everything else working. Try this:

$recipient = ""
$data = [
    'event' => [
        'type' => 'message_create',
        'message_create' => [
            'target' => [
                'recipient_id' => $recipient
            ],
            'message_data' => [
                'text' => 'Happy New Year!'
            ]
        ]
    ]
];

try
{
    $response = Twitter::post('direct_messages/events/new', $data);
}
catch (Exception $e)
{
    dd(Twitter::logs());
}

dd($response);

On twitteroauth, it's simple as:

use Abraham\TwitterOAuth\TwitterOAuth;
$connection = new TwitterOAuth(config('ttwitter.CONSUMER_KEY'), config('ttwitter.CONSUMER_SECRET'), config('ttwitter.ACCESS_TOKEN'), config('ttwitter.ACCESS_TOKEN_SECRET'));
$result = $connection->post('direct_messages/events/new', $data, true);

@giennjoro
Copy link

anyone got a fix to this error? I am still getting it

@giennjoro
Copy link

@sykezz I tried that and it works perfectly.

@andywise0
Copy link

@giennjoro what did you try that worked (what version?)?
I'm getting the " Array to string conversion" error when I try to send a DM & I'm using what I assume to be the latest version - I just did a "composer update" and it reported Updating thujohn/twitter (2.2.12 => 2.2.13) .... but I still get the error
Do I need to download a specific patch (and if so please advise how)

@sykezz
Copy link
Contributor

sykezz commented Jun 18, 2020

@andywise0 I believe he tried atymic/tmhOAuth#3.

@andywise0
Copy link

@sykezz Thanks... From what I can see that's a patch waiting to be merged
Is there a way to pull that down and test it? (without setting up a dev environment for the package)

@sykezz
Copy link
Contributor

sykezz commented Jun 20, 2020

@andywise0 You can change your composer.json to load the package from Github from the PR branch if you want.
Or you can pull the package to local (packages directory) and then load it, so you could switch branches and do changes easily.

    "autoload": {
        "classmap": [
            "packages/sykezz/tmhOAuth"
        ]
    },

@reliq
Copy link
Collaborator

reliq commented Jun 21, 2020

This has been fixed with version 2.2.14.

Sample usage:

$recipientId = xxxxxxxxxxxx; 
$pmMesage = 'Hello World';
$params = [
    'type' => 'message_create',
     'message_create' => [
         'target' => ['recipient_id' => $recipientId],
         'message_data' => ["text" => $pmMesage],
     ],
];
$response = $twitter->postDm($params);

@atymic
Copy link
Owner

atymic commented Jun 21, 2020

Awesome job @reliq! 🎉

@andywise0
Copy link

@sykezz thanks (I have now used version 2.2.14 as its been released)
@reliq thanks - although I did have to do "composer config minimum-stability dev" to install the latest thujohn/twitter ... (I'm no expert) but I think it needs a dev version of atymic/tmhoauth
I'm happy as I can now send a DM !! Thanks all.

@atymic
Copy link
Owner

atymic commented Jun 24, 2020

Should be fixed now, thanks @reliq :D

@stratgt10
Copy link

Nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants