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

any example for sendMessage #27

Closed
merlinkory opened this issue Jan 29, 2019 · 6 comments
Closed

any example for sendMessage #27

merlinkory opened this issue Jan 29, 2019 · 6 comments

Comments

@merlinkory
Copy link

Could you give some example for sendMessage?

following is not work

$chat_id=-1001190992756;
$msg = (object)['hello'];
 $client->query(json_encode( ['@type' => 'sendMessage', 'chat_id' => $chat_id, 'input_message_content' => $msg, ), 10);

i see:

[ 1][t 0][1548776841.107118130][ClientJson.cpp:71] Failed to parse [request:{\0042@extra\0042:1.01234,\0042@type\0042:\0042sendMessage\0042,\0042chat_id\0042:-1001190992756,\0042input_message_content\0042:{\00420\0042:\0042hello\0042}}] [Error : 400 : Can't find field "@type"]

@maxvgi
Copy link
Collaborator

maxvgi commented Jan 30, 2019

It is not trivial, but it is covered in tdlib documentation. We have to add it to our documentation

$chat_id=-1001190992756;
$message_text = 'hello';
$print_timeout = 1;

$client->query(json_encode([
    '@type'=>'openChat',
    'chat_id'=>$chat_id
]));

$client->query(json_encode([
    '@type'   => 'sendChatAction',
    'chat_id' => $chat_id,
    'action'  => [
        '@type' => 'chatActionTyping'
    ]
]));

sleep(1);

$client->query(json_encode([
    '@type'   => 'sendChatAction',
    'chat_id' => $chat_id,
    'action'  => [
        '@type' => 'chatActionCancel'
    ]
]));


$res=$client->query(json_encode([
    '@type'=>'sendMessage',
    'chat_id'=>$chat_id,
    'reply_to_message_id'=>0,
    'disable_notification'=>false,
    'from_background'=>false,
    'input_message_content'=>[
        '@type'=>'inputMessageText',
        'text'=>[
            '@type'=>'formattedText',
            'text'=>$message_text,
            'entities'=>[]
        ],
        'disable_web_page_preview'=>false,
        'clear_draft'=>false
    ]
]));

$client->query(json_encode([
    '@type'   => 'closeChat',
    'chat_id' => $chat_id
]));

if(!empty($res['last_message']['sendingState']) && $res['last_message']['sendingState']['@type']==='messageSendingStateFailed') {
    throw new Exception('Message sending failed');
}

@merlinkory
Copy link
Author

merlinkory commented Jan 30, 2019

@maxvgi Thank you very much for example! =)
Could you share some link, wheere it covered in tdlib documentation? I was seeking, but no result.

And do you know, why after call

$q = $client->query(json_encode([
    '@type'=>'openChat',
    'chat_id'=>$chat_id
]));
var_dump($q);

Always getting answer:

string(73) "{"@type":"error","code":3,"message":"Chat not found","@extra":1681692777}" ?

but send message work - ok :)

And as i understand "sendMessage" impossible without "openChat", right?

@merlinkory
Copy link
Author

merlinkory commented Jan 30, 2019

@maxvgi i found out: if i not open chat on the my phone (current telegram app) and don't send any in chat (via phone), I can't send Message via TDLib.
I think it is related with:

$q = $client->query(json_encode([
    '@type'=>'openChat',
    'chat_id'=>$chat_id
]));
var_dump($q);

because it doesn't work.

What do you think?

@merlinkory
Copy link
Author

@yaroslavche , @maxvgi I think i found ansewer for my question tdlib/td#88
May be you can help me:
How i can get all my peers, include chats and contacts (if i wan't use tdlib local database) when startup my app?
thank you

@yaroslavche
Copy link
Owner

yaroslavche commented Feb 2, 2019

@merlinkory as levlam says simply use use_message_database or use_chat_info_database (with creating chat, f.e. createSecretChat) parameters in $client->setTdlibParameters($tdlibParams);.
But I beleive that finding the chat on startup (after tdlib parameters query) would help:

$chatName = 'telegram';
$result = $client->query(json_encode(['@type' => 'searchPublicChat', 'username' => $chatName]), 3);

UPD. Oh, I understand the question. I think (but not sure) - you need use td databases (set in tdlib parameters) and client will do all work for you =)

@merlinkory
Copy link
Author

Ok.
Thank you for your help :)

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

No branches or pull requests

3 participants