【PHP Telegram Bot】Day09 - 用 PHP 主动接收和发送讯息吧!

前置作业

  1. 复制程序码

    还记得前天最後建立的资料夹吗,把它用 VS code 打开,再建立一个 php 档案,把昨天文章的官方程序贴进去
    https://ithelp.ithome.com.tw/upload/images/20210918/20132916BJsiUaURyF.png

  2. 修改 API_TOKEN

    把这边的 token 换成你自己的

    如果忘记的话可以看看 Day04 - Telegram 机器人的设定 有说在哪可以查看
    https://ithelp.ithome.com.tw/upload/images/20210918/201329166fMVRhsLsF.png

  3. 把底下注解掉

    这坨东东暂时用不到,先注解掉(选起来按 Ctrl + /),或是你想删掉也可以
    https://ithelp.ithome.com.tw/upload/images/20210918/20132916wdcAjjmgy7.png


用 GET 发送讯息

在 146 行按 Enter 就会多出空白行,然後刚好 142 行就是 apiRequest,直接复制到 146 行来改,跟 Day03 时一样,把你的 uid 还有你想发送的讯息填进去,忘记 uid 的话可以透过 @userinfobot 取得
https://ithelp.ithome.com.tw/upload/images/20210918/20132916uAo9aueNKV.png
按下右上角的执行程序按纽
https://ithelp.ithome.com.tw/upload/images/20210918/20132916JgwXaf682L.png
之後你就会发现喷出一坨错误XD
https://ithelp.ithome.com.tw/upload/images/20210918/201329169fE1Aen2mT.png

Fatal error: Uncaught Error: Call to undefined function curl_init() in D:\_code\php\ironman2021_by_miku3920\long_polling.php:86
Stack trace:
#0 D:\_code\php\ironman2021_by_miku3920\long_polling.php(146): apiRequest('sendMessage', Array)
#1 {main}
  thrown in D:\_code\php\ironman2021_by_miku3920\long_polling.php on line 86

他说他找不到 curl 这个名称的函式,这是因为 curl 是在 php 的扩充里才有的功能,预设是没有这个功能的,我们要先到 php.ini 设定启用後,才可以使用


设定 php.ini

看不到副档名的话,到上方 检视 的右边有个 副档名,打勾就行了
https://ithelp.ithome.com.tw/upload/images/20210918/20132916Wr1rqOel4Q.png
打开在 Day06 时安装 php 的资料夹,可以发现里面是没有 php.ini 档案的,但是有两个跟他很像的 php.ini-development php.ini-production,这两个档案最主要的差别在於 development 版显示的错误会比较多
https://ithelp.ithome.com.tw/upload/images/20210918/20132916y6okw5BjCF.png
我们把 php.ini-development 复制一份,并且重新命名成 php.ini
https://ithelp.ithome.com.tw/upload/images/20210918/20132916RLNwl2FjEQ.png
点两下开启之後,找到 extension=curl(建议用 Ctrl + f 搜寻),把左边的分号 ; 删掉,这样就启用 curl 了,记得储存档案
https://ithelp.ithome.com.tw/upload/images/20210918/20132916RFNHgvVURF.png
我们再回来执行一次程序,结果喷出更大一坨的错误!
https://ithelp.ithome.com.tw/upload/images/20210918/201329166c71Z1g0wf.png

PHP Warning:  PHP Startup: Unable to load dynamic library 'curl' (tried: C:\php\ext\curl (?历??唳?摰?璅∠???, C:\php\ext\php_curl.dll (?历??唳?摰?璅∠???) in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library 'curl' (tried: C:\php\ext\curl (?历??唳?摰?璅∠???, C:\php\ext\php_curl.dll (?历??唳?摰?璅∠???) in Unknown on line 0
PHP Fatal error:  Uncaught Error: Call to undefined function curl_init() in D:\_code\php\ironman2021_by_miku3920\long_polling.php:86
Stack trace:
#0 D:\_code\php\ironman2021_by_miku3920\long_polling.php(146): apiRequest('sendMessage', Array)
#1 {main}
  thrown in D:\_code\php\ironman2021_by_miku3920\long_polling.php on line 86

Fatal error: Uncaught Error: Call to undefined function curl_init() in D:\_code\php\ironman2021_by_miku3920\long_polling.php:86
Stack trace:
#0 D:\_code\php\ironman2021_by_miku3920\long_polling.php(146): apiRequest('sendMessage', Array)
#1 {main}
  thrown in D:\_code\php\ironman2021_by_miku3920\long_polling.php on line 86

这是因为刚刚少做了一件事,我们没告诉它扩充功能的资料夹是哪个

php.ini 找出 extension_dir = "ext"(建议用 Ctrl + f 搜寻),然後把左边的分号 ; 删掉,按一下储存
https://ithelp.ithome.com.tw/upload/images/20210918/20132916UVOzXp6YEv.png
我们再再回来执行一次程序,错误明显变小一坨了很多(欸不是,还是不能执行啊
https://ithelp.ithome.com.tw/upload/images/20210918/201329163rW8l2KJ9R.png

Curl returned error 60: SSL certificate problem: self signed certificate in certificate chain

这边是因为缺少了一个叫做 cacert.pem 的档案


下载 cacert.pem 并设定

那这个档案要去哪里找呢?其实丢到 google 第一个搜寻结果就是了

答案就在 curl 官网:https://curl.se/docs/caextract.html

点这里下载
https://ithelp.ithome.com.tw/upload/images/20210918/20132916sIcbEgltZn.png
下载下来之後贴到 php-7.4.23/extras/ssl 这个资料夹里
https://ithelp.ithome.com.tw/upload/images/20210918/201329160Cuu3qwbfp.png
然後又又要再设定一次 php.ini(相信我这是最後一次)

找出 curl.cainfo(建议用 Ctrl + f 搜寻),再次把左边的分号 ; 删掉,然後把刚刚的完整路径贴上,记得储存~
https://ithelp.ithome.com.tw/upload/images/20210918/20132916GY51Lddz0n.png


用 GET 发送讯息 Revenge

再次挑战执行程序!
https://ithelp.ithome.com.tw/upload/images/20210918/20132916qONpVuW1jt.png
这次什麽都没出现,但是 TG 「叮咚」了一声
https://ithelp.ithome.com.tw/upload/images/20210918/20132916qz1LwDVggM.png
恭喜你成功用 PHP 发送讯息啦~


用 POST 发送讯息

要改成 POST 很简单,只要将 apiRequest 改成 apiRequestJson 就行啦
https://ithelp.ithome.com.tw/upload/images/20210918/20132916MMhB3zDAjT.png
按下执行,又「叮咚」了一声~
https://ithelp.ithome.com.tw/upload/images/20210918/20132916lx7MLEBSdD.png


用 GET 接收讯息

接收讯息要用到 getUpdates 这个 API Method
https://ithelp.ithome.com.tw/upload/images/20210918/2013291636FJP65nzG.png
我们可以看到这个方法(Method)的参数(Parameter)全都是选填(Optional),这表示我们可以完全不填参数

sendMessage 方法改成 getUpdates,还有把 array() 里的参数全删除
https://ithelp.ithome.com.tw/upload/images/20210918/20132916kVDqOCzKKC.png
然後还要再加上 print_r() 这个函式,因为要把 apiRequest 函式的返回值(TG 服务器的回应(response))印出来,这样你才看的到。

至於为何不使用 echo 呢?这个就留给你自己试试
https://ithelp.ithome.com.tw/upload/images/20210918/20132916h0P9mIQAMe.png
按下执行後你可能会发现什麽都没有,只有出现 Array(),那是因为你的机器人 24 小时内没收到过任何讯息
https://ithelp.ithome.com.tw/upload/images/20210918/20132916SgGZTjgDeI.png
你只要先发讯息再执行程序就行了
https://ithelp.ithome.com.tw/upload/images/20210918/20132916mV2tTd1ZQf.png
现在你就能看到刚刚发给机器人的讯息了
https://ithelp.ithome.com.tw/upload/images/20210918/201329167fLKQ9wWIE.png


用 POST 接收讯息

最後的最後,来考考你要怎麽把它改成 POST,在底下留言你的答案吧!


<<:  人脸辨识的流程--人脸侦测

>>:  简单建立一个银行系统

【Day 25】建立 EKS on Outpost 的步骤(下)

tags: 铁人赛 AWS Outposts EKS Kubernetes 前情提要 昨天把 EKS...

Day 27: 暴力破解 WPA/WPA2 加密 wifi 密码

Day 27: 暴力破解 WPA/WPA2 加密 wifi 密码 tags: Others 自我挑战...

谁温暖了资安部-终-8-1(老王与Blue)

此篇接续 第12届-iT邦帮忙铁人赛-谁温暖了资安部-8(现况) 气死我了,那个Allen 竟然不...

DAY 26 『 AVPlayerViewController - 播放影片 』

今天要分享的是,如何用 AVPlayerViewController 显示影片 成品: {%yout...

[Day 29] 建立对外沟通的 API Server,谈谈 Ktor 框架

能够存取资料库了,有时我们可能会需要将资料库的内容,以 API 的形式,传输给其他的开发者。 这时候...