Rocket.Chat SaaS
Simple Telegram Bot:
follow these instructions to get a simple Telegram Bot setup

Getting Started

  1. 1.
    Open telegram chat in your browser or mobile phone
  2. 2.
    Add the user BotFather
  3. 3.
    Click start
  4. 4.
    Click /newbot
  5. 5.
    Follow the Instructions
instructions

Getting the Token

  1. 1.
    Wait for the Token to be displayed referred to as [myauthorization-token]
  2. 2.
    Go to the Administration Portal of Rocket.Chat and the Integrations Page
  3. 3.
    Create a new Incoming Webhook following these instructions
    • Enable script
    • Paste the following

Incoming Webhook for Rocket.Chat

1
class Script {
2
addQuotePrefix(str) {
3
let tmp = str.split('\n'),
4
res = [];
5
6
for (const frag of tmp) {
7
res.push(`> ${frag}`);
8
}
9
10
return res.join('\n');
11
}
12
13
process_incoming_request({ request }) {
14
// UNCOMMENT THE BELOW LINE TO DEBUG IF NEEDED.
15
// console.log(request.content);
16
if ('edited_message' in request.content) {
17
request.content.message = request.content.edited_message;
18
}
19
let from = request.content.message.from;
20
let who = from.username
21
let icon_url = '/avatar/' + from.username + '.jpg'
22
if(!who) {
23
if (from.first_name && from.last_name) {
24
who = `${from.first_name} ${from.last_name}`
25
} else if (from.first_name) {
26
who = from.first_name
27
} else {
28
who = from.last_name
29
}
30
icon_url = `/avatar/${request.content.message.from.first_name}.jpg`
31
}
32
let body = request.content.message.text
33
34
if(!body) {
35
if(request.content.message.hasOwnProperty("sticker") && request.content.message.sticker.emoji) {
36
// It's a sticker
37
body = request.content.message.sticker.emoji
38
} else {
39
return {}
40
}
41
}
42
43
if(request.content.message.reply_to_message) {
44
var quotedMessage =
45
"*" +
46
request.content.message.reply_to_message.from.username +
47
"*\n" +
48
request.content.message.reply_to_message.text;
49
50
quotedMessage = this.addQuotePrefix(quotedMessage);
51
body = quotedMessage + '\n' + body;
52
}
53
54
return {
55
content: {
56
username: who,
57
icon_url: icon_url,
58
text: body
59
}
60
};
61
}
62
}
Copied!

Letting Telegram Know About Rocket.Chat's WebHook

  1. 1.
    Copy incoming webhook URL from Rocket.Chat
  2. 2.
    Change following URL with your token and Incoming webhookURL and execute in regular browser https://api.telegram.org/bot[myauthorization-token]/setwebhook?url=[Incoming_Webhook_Link_from_Rocket.Chat]
  3. 3.
    Receive the message { "ok": true, "result": true, "description": "Webhook successfully set" } (or similar)
  4. 4.
    Test your incoming Webhook by sending a telegram message to the bot. It should be posted in the channel/user you specified in the incoming webhook. Check Rocket.Chat's Logs and write down chat_id (or [chat-id])

Create Outgoing WebHook in Rocket.Chat

  • Create outgoing webhook and specify channel with the following url: https://api.telegram.org/bot[myauthorization-token]/sendMessage?chat_id=[chat-id]
  • chat-id should be your group id in Telegram, You can get it buy adding @RawDataBot to your group
1
Message
2
├ message_id: 338
3
├ from
4
┊ ├ id: *****
5
┊ ├ is_bot: false
6
┊ ├ first_name: 사이드
7
┊ ├ username: ******
8
┊ └ language_code: en
9
├ chat
10
┊ ├ id: -1001118554477 // This is Your Group id
11
┊ ├ title: Test Group
12
┊ └ type: supergroup
13
├ date: 1544948900
14
└ text: A
Copied!
  • Paste the Outgoing Webhook below.
  • Enable listening at the Bot with /privacy and to disable (example)
  • Add Bot to telegram group and utilize nice cross platform communication.
1
class Script {
2
prepare_outgoing_request({ request }) {
3
if (request.data.bot) {
4
//Don't repost messages from the bot.
5
return { };
6
} else {
7
return {
8
url: request.url + '&parse_mode=HTML' + '&text=' + encodeURIComponent('<b>' + request.data.user_name+ '</b>: ' + request.data.text),
9
method: 'GET'
10
};
11
}
12
}
13
}
Copied!

Conclusion

Enjoy! Please note, this was a community submission and is very simple. If you feel there are improvements which can be made, please feel free to submit improvements via pull requests!!
final product
Last modified 1mo ago