下面这个代码,是我看视频峰哥telegram bot教学里学会的,
然后自己综合了一下峰哥的代码,
搞了一个会自动进退群打招呼,指定内容回复的机器人。
也尝试做了两个机器人
@ssrubot 奶子鸭
@zhutibot 主题酱
function doPost(e){
var body = JSON.parse(e.postData.contents);
body.message.chat.id = body.message.chat.id + '';
var payload = preparePayload(body);
var data = {
"method": "post",
"contentType": 'application/json',
"payload": JSON.stringify(payload),
}
UrlFetchApp.fetch("https://api.telegram.org/bot934292303:AAFE-SL4sBRCuZ76D0POdcPUM1ZvnefCKRM/", data);
}
function getName(user) {
var name = user.first_name;
if (user.last_name) {
name += " " + user.last_name;
}
return name;
}
function escapeMarkDown(toEscapeMsg) {
var escapedMsg = toEscapeMsg
.replace(/_/g, "\\_")
.replace(/\*/g, "\\*")
.replace(/\[/g, "\\[")
.replace(/`/g, "\\`");
return escapedMsg;
}
function getMentionName(user) {
var username = user.username;
var mentionName = "";
var name = getName(user);
if (!name) {
name = "神秘人";
}
// if (!username) {
mentionName = getMarkDownUserUrl(escapeMarkDown(name), user.id);
// } else {
// mentionName = "" + escapeMarkDown(name) + " + ")";
// }
return mentionName;
}
function getMarkDownUserUrl(userName, userId) {
return "" + userName + "";
}
function preparePayload(body){
var payload;
if (body.message.new_chat_member) {
payload = {
"method": "sendMessage",
"chat_id": body.message.chat.id,
"parse_mode": "Markdown",
"disable_web_page_preview": true,
}
payload.text = "亲亲你好鸭~" + getMentionName(body.message.new_chat_member) + "n欢迎来喝酸奶*";
if (body.message.left_chat_member) {
payload = {
"method": "sendMessage",
"chat_id": body.message.chat.id,
"parse_mode": "Markdown",
"disable_web_page_preview": true,
}
payload.text = getMentionName(body.message.left_chat_member) + "滚犊子!n要是有冤情,就去找管理员爸爸饶了你~";
return payload;
}
body.message.text = body.message.text.toLowerCase();
body.message.text = body.message.text.replace(/@ssrubot/g, '');
var paras = body.message.text.trim().split(" ");
// remove empty strings
paras = paras.filter(function(para){
if (para){
return true;
}
});
if (body.message.text){
payload = {
"method": "sendMessage",
"chat_id": body.message.chat.id,
"parse_mode": "Markdown",
"disable_web_page_preview": true,
}
var str = body.message.text;
var n = str.search("鸭子");
if(n == -1)
{
n = str.search("奶子");
}
if(n>=0){
payload.reply_to_message_id = body.message.message_id;
payload.text = "干嘛鸭?";
}
if(body.message.text.indexOf("/tg") === 0){
payload.text = "tg主题机器人";
return payload;
}
if(body.message.text.indexOf("/ssr") === 0){
payload.text = "点击进入===>>>酸奶官网n"
var inlineKeyboardMarkup = {};
inlineKeyboardMarkup.inline_keyboard = [];
var keyboardRow = [];
var keyboardButton1 = {
text: "酸奶2.0NSFW",
url: "https://t.me/ssruSSR";
};
var keyboardButton2 = {
text: "酸奶3.0(禁黄)",
url: "https://t.me/mysuannai";
};
var keyboardRow2 = [];
var keyboardButton3 = {
text: "pharos lite",
url: "https://apps.apple.com/us/app/pharos-pro/id1456610173";
};
var keyboardButton4 = {
text: "potatso lite",
url: "https://apps.apple.com/us/app/potatso-lite/id1239860606";
};
keyboardRow.push(keyboardButton1);
keyboardRow.push(keyboardButton2);
keyboardRow2.push(keyboardButton3);
keyboardRow2.push(keyboardButton4);
inlineKeyboardMarkup.inline_keyboard.push(keyboardRow);
inlineKeyboardMarkup.inline_keyboard.push(keyboardRow2);
payload.reply_markup = inlineKeyboardMarkup;
return payload;
}
}
return payload
}
最后,新建部署
复制一串script开头的网址,
打开https://www.url-encode-decode.com/
粘贴进去 转换 网址 结尾为exec的 复制。
再用新网页 打开下面链接
https://api.telegram.org/bot12345678:AAGSU-xxxxxxxxxxcccccccddddddd/setWebhook?url=......exec那串网址
网页显示{"ok":true,"result":true,"description":"Webhook was set"}
就可以了