【问题标题】:Telegram Bot Welcome greetings message电报机器人欢迎问候消息
【发布时间】:2018-02-07 16:50:55
【问题描述】:

“如何在 Telegram 中使用 Bot 发送欢迎问候消息”?实际上我在电报中创建了新的机器人。现在我想,当新用户启动我的机器人时,我的机器人向他发送欢迎问候消息?是否可以使用“getupdates”方法或者我应该使用“webhooks”?请指导我。

我已经创建了一个像@mak_tech_bot 这样的机器人。并与我的其他电报帐户一起加入,但它不会发送任何欢迎消息。我也使用/命令。

我也在 localhost 中尝试了一个示例

<?php
ini_set('error_reporting',E_ALL);
$botToken = "TOKEN";
$website = "https://api.telegram.org/bot".$botToken;

$update = file_get_contents('php://input');
$update = json_decode($update,TRUE);

$chatId = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];

switch($message){
    case "/test":
        sendMessage($chatId,"test123");
        break;
    case "/hi":
        sendMessage($chatId,"Hello123");
        break;
    default:
        sendMessage($chatId,"default");
}

function sendMessage($chatId,$message){
    $url = $GLOBALS[website]."/sendMessage?chat_id=".$chatId."$text=".urlencode($message);
    file_get_contents($url);
}
?>

【问题讨论】:

    标签: telegram telegram-bot php-telegram-bot


    【解决方案1】:

    当您点击START按钮时,您将向机器人发送/start命令,只需将case '/start':添加到您的代码中即可发送问候消息。

    【讨论】:

    • 谢谢,但是没有 webhook 方法是否可行,我想在我的 localhost 中测试它,有可能吗?
    • 没有 webhook 也是可以的。但是你必须使用getUpdates。您需要有一些机制来了解是否与您的机器人进行了任何交互。使用 webhooks Telegram 将在可用时立即提供更新。使用 getUpdates 您必须实现自己的轮询机制。
    猜你喜欢
    • 2015-10-02
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 2021-02-22
    • 2018-04-21
    • 2022-01-01
    • 1970-01-01
    相关资源
    最近更新 更多