很久没上知乎,忽然发现另有个专栏…反省 以是时隔n个月我来更新一下了
近来对COC(克苏鲁的召唤)感爱好,想写个小玩具实现线上玩耍。然后我就对着规矩书开始敲代码,敲着敲着小伙伴过来了,说“你整这些多麻烦,要不我直接拉个骰娘(Bot)进群?”
我:。。。
是啊我干啥不魔改下
群聊机器人
呢?
以上是闲聊,下面就进入正题开始搭Telegram Bot,至于为啥不选QQ/微信的机器人呢?
由于Telegram Bot的创建
非常简单
,Bot的API
非常全面
,而且
完全免费
。
QQ/微信的机器人用起来限定太多啦…
前期预备:
- telegram账号
- 谷歌账号
- 一丢丢的编程底子
- 酸酸乳或别的魔术小道具(大陆)
搭建步骤:
我们使用/new bot指令 ,并根据指示为你的Bot取个昵称以及用户名(Bot的用户名必须以bot末端)
然后你就创建好了你的第一个Telegram Bot!恭喜,是不是特殊简单?
此时BotFather会给你一个非常非常重要的工具——Bot token,留意万万要保管好它哦!
https://core.telegram.org/bots/api
core.telegram.org
↑来,点进去,然后你就能看见密密麻麻Telegram官方公布支持其Bot的全部API
别慌!
我们需要用的方法只有一点点,别急着关文章,我们继续往下操作…
实在这时我们的Bot已经可以取得用户发的讯息了,我们先向Bot任意发点什么工具(固然此时它还不会回答),然后在browser中输入:
https://api.telegram.org/bot(token)/getUpdates
(token)请换成上面或取的token
你就能看见自己和Bot的对话记载,比方:
{ ok: true, result: [ { update_id: xxx, message: { message_id: 2, from: { id: xxx, is_bot: false, first_name: "xxx", username: "xxx", language_code: "zh-hans" }, chat: { id: xxx, first_name: "xxx", username: "xxx", type: "private" }, date: xxx, text: "Hello" } } ] }
这种方法虽然可以或取讯息,但是要做到Bot随时使用,频繁调用 getUpdates 方法会非常浪费资源。因此我们使用另一种方法设置
Webhook
,即向 Telegram 设定一组 callback url,只要有效户向你的Bot发送讯息,Telegram 就会把用户讯息连同元数据 传送到你设定的 url 。
打开browser,输入 http://script.google.com 新建脚本
我们直接使用gs开发就可以啦,谷歌的服务器自然是有SSL证书的。
关于Telegram Bot所支持的程序语言,虽然官方手册的案例上并没有gs,但我们打开 GitHub ,搜索telegram-bot-gscript,还是能找到案例的哦。
function
doPost
(
e
){
var
estringa
=
JSON
.
parse
(
e
.
postData
.
contents
);
var
payload
=
identificar
(
estringa
);
var
data
=
{
"method"
:
"post"
,
"payload"
:
payload
}
UrlFetchApp
.
fetch
(
"https://api.telegram.org/botYOUR-API-HERE/"
,
data
);
}
function
identificar
(
e
){
if
(
e
.
message
.
text
){
var
mensaje
=
{
"method"
:
"sendMessage"
,
"chat_id"
:
e
.
message
.
chat
.
id
,
"text"
:
e
.
message
.
text
,
}
}
else
if
(
e
.
message
.
sticker
){
var
mensaje
=
{
"method"
:
"sendSticker"
,
"chat_id"
:
e
.
message
.
chat
.
id
,
"sticker"
:
e
.
message
.
sticker
.
file_id
}
}
else
if
(
e
.
message
.
photo
){
var
array
=
e
.
message
.
photo
;
var
text
=
array
[
1
];
var
mensaje
=
{
"method"
:
"sendPhoto"
,
"chat_id"
:
e
.
message
.
chat
.
id
,
"photo"
:
text
.
file_id
}
}
else
{
var
mensaje
=
{
"method"
:
"sendMessage"
,
"chat_id"
:
e
.
message
.
chat
.
id
,
"text"
:
"Try other stuff"
}
}
return
mensaje
}
我们把案例中的代码复制到上面新建的脚本当中,保存并点击左上角的公布-摆设为网络应用,把权限改为任何人甚至匿名,确定。
你就得到了具有SSL凭据的callback url啦!
{"ok":true,"result":true,"description":"Webhook was set"}
恭喜!Bot搭建完成。
再次打开Bot的聊天界面,你会发现它已经会鹦鹉学舌啦!
更多的功能扩展就等诸位自己使用API去开发啦,只有想不到没有Bot实现不了的功能哦~
-end-
telegram怎么创建机器人
将机器人连接到 Telegram
Connect a bot to Telegram
本文内容
可设置机器人,使其与使用 Telegram 消息通报应用的用户进行通讯。
You can configure your bot to communicate with people using the Telegram messaging app.
提示
有关说明每个通道支持的功能的表,请参阅 通道参考 一文。
For tables describing which features are supported on each channel, see the channels reference div.
访问 Bot Father 来新建一个 Telegram 机器人
Visit the Bot Father to create a new Telegram bot
使用 Bot Father 创建新的 Telegram 机器人 。
Create a new Telegram bot using the Bot Father.
创建新的 Telegram 机器人
Create a new Telegram bot
要创建新的 Telegram 机器人,请发送死令
/newbot
。
To create a new Telegram bot, send command
/newbot
.
指定易记名称
Specify a friendly name
为 Telegram 机器人提供易记名称。
Give the Telegram bot a friendly name.
指定用户名
Specify a username
为 Telegram 机器人提供唯一用户名。
Give the Telegram bot a unique username.
复制访问令牌
Copy the access token
复制 Telegram 机器人的访问令牌。
Copy the Telegram bot’s access token.
输入 Telegram 机器人的访问令牌
Enter the Telegram bot’s access token
转到 Azure 流派中机器人的“通道”部分,单击“Telegram”按钮。
Go to your bot’s
Channels
section in the Azure portal and click the
Telegram
button.
备注
假如已将机器人连接到 Telegram,Azure 流派 UI 看起来会稍有差别。
The Azure portal UI will look slightly different if you have already connected your bot to Telegram.
将之前复制的令牌粘贴到“访问令牌”字段中,然后单击“保存” 。
Paste the token you copied previously into the
Access Token
field and click
Save
.
机器人如今已成功设置为与 Telegram 中的用户通讯。
Your bot is now successfully configured to communicate with users in Telegram.
今日看到一个电报群里说 使用BOT来签到,很好奇就想学学,来吧 !
创建一个BOT!!!
1、先搜索BotFather
我这里搜索到好几个没有一个可用的,输入/help 返回俄文错误信息。
后来在官网找到了 https://telegram.me/BotFather 直接点击不用搜索,靠谱,管用。
用带官网小图标的靠谱
2、使用 newbot创建
使用newbot 创建,然后提示名称背面参加Bot要么_bot
假如回答 sorry 说明 没有创建成功,不然ok!
最常见的是名称已存在!
TOKEN 肯定要葆护好!以后接口访问都要用到!
3、使用mybots察看你自己的机器人,我这里就一个。
4、点机按钮,先后有设置选项
5、API Token 可以重新天生 token和察看token
6、 Edit Bot 可以编辑 名称、描述、关于、图片(头像)、下令
名称:不说了,不重复任意改。
描述:这个描述是聊天的时间,在头部表现的。
关于:在个人信息页面表现,发送分享链接的时间表现。
图片:直接复制,要么拖进来,那个附件图标点击都可以!!
添加下令:格式 xxx-xxxxx 前下令-后描述。 这个下令由于没有代码,以是没有相应,以后详解。
7、Bot settings 设置– 这组默认就行
inline Mode 内联模式 默认是封闭,启用以后就可以在任何聊天地区 跟机器人互动。
详见 https://core.telegram.org/bots/inline
Allow Groups 容许添加到组 默认是开启的,没啥可说的。
Group Privacy 组策略,默认是开启的, 这样机器人不需要担当全部人发的消息,不需要回答全部人发的消息,只需要/开头的。
可以配合 设置机器人为管理员来实现。
Payments 关于支付,没搞过不多说。
Domain 域名空间,用默认就行,想研究的 https://core.telegram.org/widgets/login
8、Delete Bot 和 Payments 不做介绍了。
注:创建一个机器人,7步 8 步可以用默认。
最重要的是下令的相应,下令要想有相应就需要有服务支持。一样平常使用谷歌js开发服务,创建脚本服务,返回需要的内容,还可以使用数据库等。别的,还可以用java等语言编写。以后逐步介绍。
本文网址:
http://www.1cm8858.com/d/2020631192_9074_1747820313/home
Ten articles before and after
军用电报密码对照表大全(第4页)-电报telegram技巧分享
国内大陆号码成功注册telegram/电报攻略Googlevoice图文教程-电报telegram技巧分享
telegram[tg教学]改变介面语言(繁简、广东话日文)-电报telegram技巧分享
菲律宾telegram群有哪些?交流群-电报telegram技巧分享
用这个提示在中国使用telegram国内方法-电报telegram技巧分享
@广东人标准中文电码(ChineseCommercialCode)简介、用途及查询-电报telegram技巧分享
如何加入电报群组或查找telegram群组?一个可以搜索/上各种频道机器人的工具-电报telegram技巧分享
中国+86手机成功注册telegram/电报攻略(、tg)登录收不到验证码怎么办?-电报telegram技巧分享